【问题标题】:Extract biggest blob with opencv用opencv提取最大的blob
【发布时间】:2018-04-12 20:40:29
【问题描述】:

我有一个二进制图像,将人类描绘为白色斑点,将背景描绘为黑色。我想使用 opencv 从大图像中“裁剪”最大(或 3 个最大)的 blob。

如何解决这个问题?

【问题讨论】:

标签: python c++ opencv blob


【解决方案1】:

我不确定您是否找到了答案,但这里是基于我理解的代码的基本结构是您的要求。您可以根据需要对其进行修改。

import numpy as np
import cv2

# load the image
image = cv2.imread("path_to_your_image.png") # if this not a binary image, you can threshold it
output = image.copy()

im2,contours,hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

if len(contours) != 0:
    # the contours are drawn here
    cv2.drawContours(output, contours, -1, 255, 3)

    #find the biggest area of the contour
    c = max(contours, key = cv2.contourArea)

    x,y,w,h = cv2.boundingRect(c)
    # draw the 'human' contour (in green)
    cv2.rectangle(output,(x,y),(x+w,y+h),(0,255,0),2)

# show the image
cv2.imshow("Result", output)

cv2.waitKey(0)

注意: x、y、x+w 和 y+h 为您提供框的范围,因此您可以通过这些值获得最大斑点的感兴趣区域。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 2017-09-28
    • 1970-01-01
    • 2018-04-13
    • 2015-01-14
    • 2017-04-23
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    相关资源
    最近更新 更多