I have several thousand images of the lungs, taken from a CT scanner. An image looks like this.
I am attempting to extract the "lungs" section from the image by creating a mask. For example:
The problem is the edges of the lungs in the mask. Ideally, I could perform contour approximation to approximate the boundaries of the lungs in the mask and smooth them out so that bits weren't chopped out.
To begin with, I have tried the following code - the idea being to find the contours and then the "defects". However in this code example, defects returns None. I am new to OpenCV:
mask = cv2.imread("mask.jpg", 0)
_, contours, hierarchy = cv2.findContours(mask, 1, 2)
cnt = contours[0]
hull = cv2.convexHull(cnt, returnPoints = False)
defects = cv2.convexityDefects(cnt, hull)
for i in range(defects.shape[0]):
s,e,f,d = defects[i,0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
cv2.line(img,start,end,[0,255,0],2)
cv2.circle(img,far,5,[0,0,255],-1)
This work is a follow on from this question on stackoverflow
https://stackoverflow.com/questions/47483411/python-and-opencv-how-do-i-convert-the-all-of-the-background-of-this-image-to-o/47483538?noredirect=1#comment81923318_47483538
Where it was suggested I look into contour approximation. I am wondering based on what I have researched so far, if it is contour hull I am really looking for. Any suggestions would be greatly appreciated.