Ask Your Question
1

cv.findContours is connecting unconnected regions

asked 2020-08-12 22:19:58 -0600

rafaveguim gravatar image

Original image:

image description

Code:

contours, hierarchy = cv.findContours(blobs, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(blobs, contours, -1, (0,255,0), 2)

Result:

image description

Why unconnected regions become part of the same contour, as in the example above? Is this the expected behaviour? What can I do to obtain separate contours for these regions?

edit retag flag offensive close merge delete

Comments

please upload the original image

sturkmen gravatar imagesturkmen ( 2020-08-13 06:45:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2020-08-13 06:47:44 -0600

updated 2020-08-13 09:45:23 -0600

see documentation

Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero pixels remain 0's, so the image is treated as binary

be sure you binarized your image

Mat test = imread("test.png", 0);
Mat test1 = imread("test.png", 0);
blur(test, test,Size(19,19));
test = test / 50;

std::vector<std::vector<Point> > contours;

findContours(test, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);

drawContours(test, contours, -1, Scalar(255), 1);

imshow("test", test+test1);

image description

image description

edit flag offensive delete link more

Comments

1

can i get this code in python?

tester gravatar imagetester ( 2020-08-14 00:18:19 -0600 )edit

@tester i get slight difference in result image.i think test = test/50 not giving same result in numpy

import cv2
import numpy as np


test = cv2.imread("e:/test.png",0)
test1 = cv2.imread("e:/test.png",0)

test = cv2.blur(test,(19,19))
test = test/50

contours, hierarchy = cv2.findContours(test, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(test, contours,-1, (255), 1)

test = cv2.add(test,test1)
cv2.imshow('test', test)
cv2.waitKey()
sturkmen gravatar imagesturkmen ( 2020-08-14 00:58:59 -0600 )edit

Is there any way to fill in the empty space?

tester gravatar imagetester ( 2020-08-14 03:23:28 -0600 )edit

did you mean

cv2.drawContours(test, contours,-1, (255), -1)
sturkmen gravatar imagesturkmen ( 2020-08-14 05:22:33 -0600 )edit

@supra56 please explain reason of your downvote for an accepted answer. @berak and me do not care about carma but i see you frequently downvoted our answers.

sturkmen gravatar imagesturkmen ( 2020-08-31 09:52:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-08-12 22:19:58 -0600

Seen: 1,130 times

Last updated: Aug 13 '20