【发布时间】:2019-07-31 19:30:38
【问题描述】:
我正在尝试分割足球场。我找到了代表场地面积的最大轮廓。我需要使用该区域生成二进制图像。
我正在关注一篇研究论文并遵循了所有步骤,包括
- 转换为 HSV
- 捕获 H 通道
- 生成直方图
- 一些处理(未提及与问题无关)
- 在二值图像中查找最大的斑点
我已经使用 Contours 完成了它,并且我拥有代表字段区域的最大轮廓。
我需要使用这个特定的轮廓来生成一个新的二进制图像,它只包含这个轮廓的区域。
# Find Largest Blob
# mask is the processed binary image
# using that mask I find the contours and draw them on original
#image
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
largest_blob = max(contours, key=cv2.contourArea)
cv2.drawContours(image, largest_blob, -1, (0, 0, 255), 2)
【问题讨论】:
-
使用
cv2.BoundingRect()在图像中查找ROI并使用cv2.imwrite()保存ROI怎么样? -
@xanjay 我对存储 ROI 不感兴趣。我只想生成一个包含
largest_blob轮廓区域的二进制掩码 -
在我看来你已经这样做了。只需在
image上将轮廓绘制为二进制,即cv2.drawContours(image, largest_blob, -1, 255, 2),其中image的类型为uint8 -
我必须同意@gilad。你想要什么
drawContours没有给你?