Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Finding defects in the contours of a masked derived from a complex medical image and correcting them

I have several thousand images of the lungs, taken from a CT scanner. An image looks like this.

C:\fakepath\IM-0020-0085.jpg

I am attempting to extract the "lungs" section from the image by creating a mask. For example:

C:\fakepath\mask.jpg

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.

Finding defects in the contours of a masked derived from a complex medical image and correcting them

I have several thousand images of the lungs, taken from a CT scanner. An image looks like this.

C:\fakepath\IM-0020-0085.jpg

I am attempting to extract the "lungs" section from the image by creating a mask. For example:

C:\fakepath\mask.jpg

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.

Finding defects in the contours of a masked mask, derived from a complex medical image and correcting them

I have several thousand images of the lungs, taken from a CT scanner. An image looks like this.

C:\fakepath\IM-0020-0085.jpgC:\fakepath\IM-0020-0085.jpg

I am attempting to extract the "lungs" section from the image by creating a mask. For example:

C:\fakepath\mask.jpgC:\fakepath\mask.jpg

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. appreciated.

Finding defects in the contours of a mask, derived from a complex medical image and correcting them

I have several thousand images of the lungs, taken from a CT scanner. An image looks like this.

C:\fakepath\IM-0020-0085.jpg

I am attempting to extract the "lungs" section from the image by creating a mask. For example:

C:\fakepath\mask.jpg

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.

Finding defects in the contours of a mask, derived from a complex medical image and correcting them

I have several thousand images of the lungs, taken from a CT scanner. An image looks like this.

C:\fakepath\IM-0020-0085.jpg

I am attempting to extract the "lungs" section from the image by creating a mask. For example:

C:\fakepath\mask.jpg

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.