I'm fetching a frame correctly from webcam:
success, image = self.video.read()
But when I try to change the mode of my frame to grayscale using the following code:
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
I get the following error:
im = im[..., ::-1].transpose((0, 3, 1, 2)) # BGR to RGB, BHWC to BCHW, (n, 3, h, w)
ValueError: axes don't match array
What seems to be the problem ?
========UPDATE=========== Here is my code:
class xyx(object):
def __init__(self):
...
def __del__(self):
self.video.release()
def get_frame(self):
success, image = self.video.read()
print(type(image))
#clean
image = np.zeros((480,640,3),
dtype=np.uint8)
image = get_grayscale(image)
...
#...
x = ...
#...
return ...
And for the functions:
def get_grayscale(img):
return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
def remove_noise(img):
return cv2.medianBlur(img,5)
def thresholdImg(img):
return cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
The error is not deep in the code; without converting to grayscale it works. The whole problem is in the conversion from BGR to grayscale.
You didn't get that error while trying to cvtColor
. You got that error while trying to transpose.
Your image
has shape (480, 640, 3)
.
Your attempt to transpose
refers to a 4th dimension, 3
, which does not exist.
Hence the ValueError
.