OpenCV Error: Assertion failed (L.channels() == 1

2019-09-20 15:02发布

问题:

I got the following error in OpenCV (python) and have googled a lot but have not been able to resolve.

I would be grateful if anyone could provide me with some clue.

OpenCV Error: Assertion failed (L.channels() == 1 && I.channels() == 1) in connectedComponents_sub1, file /home/snoopy/opencv- 3.1.0/modules/imgproc/src/connectedcomponents.cpp, line 341 Traceback (most recent call last): File "test.py", line 30, in plant = analyzeplant.analyzeSideView(plant) File "/home/snoopy/Desktop/Leaf-201612/my-work- editing/ripps/src/analyzePlant.py", line 229, in analyzeSideView plant_img = self.__extractPlantArea(plant_img) File "/home/snoopy/Desktop/Leaf-201612/my-work- editing/ripps/src/analyzePlant.py", line 16, in __extractPlantArea output = cv2.connectedComponentsWithStats(plant, 4, cv2.CV_32S) cv2.error: /home/snoopy/opencv- 3.1.0/modules/imgproc/src/connectedcomponents.cpp:341: error: (-215) > L.channels() == 1 && I.channels() == 1 in function connectedComponents_sub1

回答1:

Let us analyze it:

Assertion failed (L.channels() == 1 && I.channels() == 1)

The images that you are passing to some function should be 1 channel (gray not color).

__extractPlantArea(plant_img)

That happened in your code exactly at the function called __extractPlantArea.

cv2.connectedComponentsWithStats

While you are calling the OpenCV function called connectedComponentsWithStats.

Conclusion:

Do not pass colorful (BGR) image to connectedComponentsWithStats