Reputation: 61
I'm using a special case of filling for copped blobs that can not filled by hole filling, the algorithm works as following:
Example is shown the attached image
What is the name of this algorithm so I can find a reference for it?
Upvotes: 2
Views: 202
Reputation: 20658
I finally figured out how to do this.
import cv2
img = cv2.imread('hull.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
contours,hierarchy = cv2.findContours(gray, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours detected:", len(contours))
cnt = contours[1]
hull = cv2.convexHull(cnt)
#img = cv2.drawContours(img,[cnt],0,(255,0,0),2)
thickness = -1 #negative thickness fills the contour insides
img = cv2.drawContours(img,[hull],0,(255,255,255),-1) #the colours are (B, G, R)
cv2.imshow("Convex Hull", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Upvotes: 0
Reputation: 21
What you are after is an algorithm that leverages the 'convex hull' of a region. The final image above is not convex, but you can generate it by iteratively intersecting the dilation of the region with the convex hull and adding the result to the original image. This adds pixels layer-by-layer to just the concave regions of the original image. This alone will not fill up the central void before the exterior becomes identical to the convex hull, however, and so the trick is to perform hole-filling at every iteration. After 4-5 iterations the 'cave mouth' closes up and the hole filling will take care of the remaining void.
Upvotes: 2
Reputation:
This is similar to a succession of two morphological closing operations, respectively with a W x 1 and a 1 x H linear structuring element. A closing is a dilation followed by an erosion with the same structuring element.
Upvotes: 0