【问题标题】:Unable to crop the retinal image无法裁剪视网膜图像
【发布时间】:2020-02-02 23:43:10
【问题描述】:

我正在尝试使用 canny 检测和使用 opencv 查找计数来裁剪视网膜图像。我正在使用下面的代码,但我没有得到裁剪的图像,而是得到像

.

以下代码有什么问题。

原图是这样的

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)

【问题讨论】:

标签: python opencv


【解决方案1】:

那是因为 ROI 被分配了 cnts 列表中最后一个轮廓的边界框。要么在 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]

【讨论】:

  • 能否提供代码。因为我是新开简历。
  • 类似的东西
  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
相关资源
最近更新 更多