提取MSER检测到的区域(Python,OpenCV) [英] Extract MSER detected areas (Python, OpenCV)

查看:509
本文介绍了提取MSER检测到的区域(Python,OpenCV)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在这张图片中通过MSER提取检测到的区域:

I can't extract the detected regions by MSER in this image:

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

What I want to do is to save the green bounded areas. My actual code is this:

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.

Expected result should be a ROI like image.

源图像:

推荐答案

仅获取每个轮廓的边界框,将其用作ROI即可提取区域并将其保存:

Just get the bounding box for each contour, use that as a ROI to extract the area and save it out:

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])

这篇关于提取MSER检测到的区域(Python,OpenCV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆