2

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

007
  • 63
  • 1
  • 1
  • 9
  • 1
    Not without a [mcve], unfortunately. – Wayne Werner Dec 21 '16 at 07:33
  • What's a "rare" error? – Right leg Dec 21 '16 at 08:04
  • following the below link, I added cv2.IMREAD_GRAYSCALE and the error >Assertion failed (L.channels() == 1 && I.channels() == 1) in connectedComponents_sub1 >has resolved. `image = cv.imread("/home/snoopy/Desktop/Leaf-201612/cropped-image/cropped_125254_ver2.bmp",cv2.IMREAD_GRAYSCALE)` did the job. [link](http://answers.opencv.org/question/96641/problem->using-connectedcomponents-in-opencv-3-using-visual-studio-2012/) Thanks anyway-Wayne Werner – 007 Dec 21 '16 at 08:36

1 Answers1

12

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

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • 1
    Yes, that turned out to be the cause..fixed with `cv2.IMREAD_GRAYSCALE` thanks Humam! – 007 Dec 21 '16 at 08:41