When I applied OpenCV findContours() function on this image, I found that it created two separate contours. I am trying to merge these two contours to form a single contour. My final goal is as follows:
Please ignore the dots. I have created it with annotation tools. The final contour might be slightly different from the given image but somewhat similar to this image. The most important part is to get a continuous shape of the contour.
I am summarizing my work below with some code and images.
Step one (Finding contours):
Using the findContours() function I got the contours as shown below:
contour = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, [contour], -1, (0,0,255), 2)
Step two (merge contours):
Then I tried to merge the two contours using np.vstack() function and draw it on the image. Basically, I have stacked the two contour coordinates into one.
contours_combined = np.vstack(contour)
cv2.drawContours(img, [contours_combined], -1, (0,0,255), 2)
Using this code I got the contour as shown in the image:
Step three (Us of OpenCV convexHull):
Then I created convex hull using the stacked contours.
hull = cv2.convexHull(contours_combined)
cv2.polylines(img, [hull], True, (0,0,255), 2)
I got the image below:
I have used cv2.morphologyEx() but unfortunately, that also did not help me to achieve my goal (check the image below).
How do I get a single contour shown in the first image?





