无法裁剪视网膜图像

时间:2019-10-04 15:53:58

标签: python opencv

我正在尝试使用canny检测并通过opencv查找countours来裁剪视网膜图像。我正在使用以下代码,但没有得到裁剪的图像,而是得到类似的图像

retinal_image_cropped.jpg

以下代码有什么问题。

原始图片是这样的

original_retinal_image.jpg

import cv2
# Load image, convert to grayscale, and find edges
image = cv2.imread('IDRiD_001.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray, 120, 255, 1)

# Find contour and sort by contour area
cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea)

# Find bounding box and extract ROI
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]
    break

cv2.imwrite('retinal_image.jpg',ROI)

1 个答案:

答案 0 :(得分:1)

那是因为在cnts列表中为ROI分配了最后一个轮廓的边界框。可以在for循环外定义ROI并使用并运算符|进行ROI的并集,或者以某种方式确保只有一个轮廓。

top = []
left = []
bot = []
right = []
for c in cnts:
    x, y, w, h = cv2.boundingRect(c)
    top.append(y)
    bot.append(y+h)
    left.append(x)
    right.append(x+w)

t, l, b, r = (min(top), min(left), max(bot), max(right))
ROI = image[t:b, l:r]
相关问题