python - OpenCV findContours() complains if used with black-white image -


i want perform edge detection following code. error because of image color depth. error makes in eyes no sense, convert image gray-scale image, , in subsequent step black , white image, working correctly. when call findcontours error.

import cv2  def bw_scale(file_name, tresh_min, tresh_max):     image = cv2.imread(file_name)     image = cv2.cvtcolor(image, cv2.color_rgb2gray)     #(thresh, im_bw) = cv2.threshold(image, tresh_min, tresh_max, cv2.thresh_binary | cv2.thresh_otsu)     (thresh, im_bw) = cv2.threshold(image, tresh_min, tresh_max, 0)      cv2.imwrite('bw_'+file_name, im_bw)     return (thresh, im_bw)  def edge_detect(file_name, tresh_min, tresh_max):     (thresh, im_bw) = bw_scale(file_name, tresh_min, tresh_max)     contours, hierarchy = cv2.findcontours(thresh, cv2.retr_tree, cv2.chain_approx_simple)   if __name__ == '__main__':   edge_detect('test.jpg', 128, 255) 

i error:

dgrat@linux-v3pk:~> python aoi.py opencv error: unsupported format or combination of formats ([start]findcontours support 8uc1 , 32sc1 images) in cvstartfindcontours, file /home/abuild/rpmbuild/build/opencv-2.4.9/modules/imgproc/src/contours.cpp, line 196 traceback (most recent call last):   file "aoi.py", line 25, in <module>     edge_detect('test.jpg', 128, 255)   file "aoi.py", line 19, in edge_detect     contours, hierarchy = cv2.findcontours(thresh, cv2.retr_tree, cv2.chain_approx_simple) cv2.error: /home/abuild/rpmbuild/build/opencv-2.4.9/modules/imgproc/src/contours.cpp:196: error: (-210) [start]findcontours support 8uc1 , 32sc1 images in function cvstartfindcontours 

the problem in code you're misusing return values of cv2.threshold().

cv2.threshold returns 2 parameters:

  • retval

    is used when thresholding using otsu method (returning optimal threshold value) otherwise returns same threshold value passed function, 128.0 in case.

  • dst

    is thresholded result image

in code thresh float not mat.

change:

contours, hierarchy = cv2.findcontours(thresh, cv2.retr_tree, cv2.chain_approx_simple)

to

contours, hierarchy = cv2.findcontours(im_bw, cv2.retr_tree, cv2.chain_approx_simple)

edit

below find refactored , simplified version of original code using following test image.

enter image description here

import cv2  def edge_detect(file_name, tresh_min, tresh_max):     image = cv2.imread(file_name)     im_bw = cv2.cvtcolor(image, cv2.color_rgb2gray)      (thresh, im_bw) = cv2.threshold(im_bw, tresh_min, tresh_max, 0)     cv2.imwrite('bw_'+file_name, im_bw)      contours, hierarchy = cv2.findcontours(im_bw, cv2.retr_tree, cv2.chain_approx_simple)     cv2.drawcontours(image, contours, -1, (0,255,0), 3)     cv2.imwrite('cnt_'+file_name, image)  if __name__ == '__main__':   edge_detect('test.jpg', 128, 255) 

this produces following bw_test.jpg

enter image description here

with following contours highlighted in cnt_test.jpg

enter image description here


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -