1

在此图像中,我无法通过 MSER 提取检测到的区域:

图像

我想要做的是保存绿色边界区域。我的实际代码是这样的:

import cv2
import numpy as np

mser = cv2.MSER_create()
img = cv2.imread('C:\\Users\\Link\\img.tif')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
regions, _ = mser.detectRegions(gray)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(vis, hulls, 1, (0, 255, 0))

mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)
mask = cv2.dilate(mask, np.ones((150, 150), np.uint8))
for contour in hulls:
    cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1)

    text_only = cv2.bitwise_and(img, img, mask=mask)


cv2.imshow('img', vis)
cv2.waitKey(0)
cv2.imshow('mask', mask)
cv2.waitKey(0)
cv2.imshow('text', text_only)
cv2.waitKey(0)

预期结果应该是像 ROI 一样的图像。

出去

源图像:

源代码

4

3 回答 3

4

detectRegions 还返回边界框:

regions, boundingBoxes = mser.detectRegions(gray)

for box in boundingBoxes:
        x, y, w, h = box;
        cv2.rectangle(vis, (x, y), (x+w, y+h), (0, 255, 0), 1)

这会绘制绿色矩形,或者按照 GPhilo 的回答中所述保存它们。

于 2019-08-23T09:48:58.623 回答
2

只需获取每个轮廓的边界框,将其用作 ROI 以提取区域并将其保存:

for i, contour in enumerate(hulls):
    x,y,w,h = cv2.boundingRect(contour)
    cv2.imwrite('{}.png'.format(i), img[y:y+h,x:x+w])
于 2017-12-01T14:55:02.110 回答
1

嘿找到了一种更简洁的方法来获取边界框

regions, _ = mser.detectRegions(roi_gray)

bounding_boxes = [cv2.boundingRect(p.reshape(-1, 1, 2)) for p in regions]
于 2020-05-17T13:26:06.790 回答