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
  • 1 Try printing image.shape and image.dtype after your read(). – Mark Setchell Commented Mar 6 at 22:24
  • 1 No... image.dtype. And try printing the two values immediately before the greyscale conversion. – Mark Setchell Commented Mar 7 at 0:16
  • 1 minimal reproducible example required. – Christoph Rackwitz Commented Mar 7 at 0:52
  • 1 Mmmm... sounds like your installation might be broken. Try redefining your image immediately prior to greyscale conversion with image = np.zeros((480,640,3), dtype=np.uint8) – Mark Setchell Commented Mar 7 at 0:53
  • 1 nothing is broken. the shape and dtype are expected. the transpose method call refers to axes that DO NOT EXIST. – Christoph Rackwitz Commented Mar 7 at 0:53
 |  Show 9 more comments

1 Answer 1

Reset to default 3

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.

本文标签: pythonValueError axes don39t match array when trying to transposeStack Overflow