admin管理员组文章数量:1410730
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.
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.
Share Improve this question edited Mar 7 at 19:56 Mohammed Baashar asked Mar 6 at 22:13 Mohammed BaasharMohammed Baashar 5554 gold badges10 silver badges24 bronze badges 14 | Show 9 more comments1 Answer
Reset to default 3You 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
.
本文标签: pythonValueError axes don39t match array when trying to transposeStack Overflow
版权声明:本文标题:python - ValueError: axes don't match array when trying to transpose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744947140a2633851.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
image.shape
andimage.dtype
after yourread()
. – Mark Setchell Commented Mar 6 at 22:24image.dtype
. And try printing the two values immediately before the greyscale conversion. – Mark Setchell Commented Mar 7 at 0:16image = np.zeros((480,640,3), dtype=np.uint8)
– Mark Setchell Commented Mar 7 at 0:53