Ask Your Question
0

How to find contour of black regions in white background ?

asked 2020-03-02 04:21:49 -0600

kanankhan gravatar image

I have image like this, I want to find contours of black regions. How can I do it ?
I use following code

                 im = cv2.imread(img)
                    plt.imshow(im)
                    print(type(im))
                    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
                    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
                    thresh = cv2.threshold(im, 60, 255, cv2.THRESH_BINARY_INV)
                    cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

How ever it gives an error like "TypeError: Expected Ptr<cv::umat> for argument 'image' ". Can you help me solve it ? Thnaks image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-03-02 04:58:24 -0600

berak gravatar image

updated 2020-03-02 11:36:38 -0600

please take a look at the docs, cv2.threshold returns a ret, image tuple, you have to use it like:

[edit] correct invocation, see comments below:

gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
value, thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY_INV)
# opencv 4.x:
cnts, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# opencv 3.x:
# im2, cnts, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
edit flag offensive delete link more

Comments

Thanks, It solved for that line but for the next line -" error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\contours.cpp:197: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl' ". I think I should change the mode of image, if it is right could you tell me to what ? thanks

kanankhan gravatar imagekanankhan ( 2020-03-02 05:51:39 -0600 )edit

ah, easy: cv.threshold(gray,...) not cv.threshold(im,...)

then:

 >>>  help(cv.findContours)

for the next problem (return values from that), i can already see it, hehehehe ;)

berak gravatar imageberak ( 2020-03-02 06:01:38 -0600 )edit
1

Thanks, it solved my problem, I also laugh because it is first time that I use OPENCV xD

kanankhan gravatar imagekanankhan ( 2020-03-02 06:08:07 -0600 )edit
1

@berak. It is not gray. It should be cv.threshold(blurred,...) not cv.threshold(gray,...). The reasoning is why. You needed to get rid of noises.before threshold.

supra56 gravatar imagesupra56 ( 2020-03-02 07:52:01 -0600 )edit
1

^^ sure, right. at least NOT the color img ...

berak gravatar imageberak ( 2020-03-02 08:20:17 -0600 )edit

Thanks, I have one more problem, I want to get coordinates of the centers of the regions those I found. what can I use ? I am sorry today I started to use opencv thats why I dont have much experience in this field. Thanks in advance

kanankhan gravatar imagekanankhan ( 2020-03-02 10:57:22 -0600 )edit

centers of the regions

  • either the center of the bounding box (geometric center)
  • or from moments() (center of mass)
  • even from summing up all points of the contour, and divide by N (another way to calculate center of mass)

we have nice tutorials here

berak gravatar imageberak ( 2020-03-02 11:14:25 -0600 )edit

Thanks I used moments like this:

              for c in range(0,len(cnts)):
                    cnt = cnts[c]
                    M = cv2.moments(cnt)

                    cx = int(M['m10']/M['m00'])
                    cy = int(M['m01']/M['m00'])

And it worked now I need to crop this region from image how can I do it using Opencv or I should use PIL library or it is just nmpy slicing ? which one will be easy ? Thanks

kanankhan gravatar imagekanankhan ( 2020-03-03 02:01:24 -0600 )edit

I used OpenCV, but no third party.

supra56 gravatar imagesupra56 ( 2020-03-03 08:05:57 -0600 )edit

Thanks, I used numpy slicing as suggested in OpenCV, my program , runs for the first image and it works perfectle as I expected but when it moves the second image it gives an error like: " error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite' " could please help me how should I deal with it ? thanks

kanankhan gravatar imagekanankhan ( 2020-03-03 14:44:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-03-02 04:21:49 -0600

Seen: 10,229 times

Last updated: Mar 02 '20