Skip to content
Advertisement

numpy.AxisError: axis 2 is out of bounds for array of dimension 0

I’m creating a program the requires a black and white image and two arrays containing the X and Y coordinates of both black and white pixels respectively. I have a program that utilizes OpenCV and binary thresholding to create a black and white image (code source). Hear is the code I have in full so far.

JavaScript

The problem I have comes up at these lines

JavaScript

when the program is run the error numpy.AxisError: axis 2 is out of bounds for array of dimension 0 is shown.

What does this error mean and how can I fix the program?

Advertisement

Answer

Replace those two lines of code with the following lines:

JavaScript

What’s happening here is that the blackAndWhiteImage is a binary image. Thus, it contains only one channel. Therefore, axis=2 is not valid for single channeled image and also you were comparing the image value to [0, 0, 0] or [255, 255, 255] that is not possible as the image contain values equal to 0 or 255 only.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement