1

我想从 python3.5 中的图像中提取 MSER 特征,但我找不到任何解决方案。我正在尝试以下代码:

import cv2
import sys

mser = cv2.MSER_create()
img = cv2.imread('signboard.jpg')
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))

cv2.imshow('img', vis)

if cv2.waitKey(0) == 9:
    cv2.destroyAllWindows()

但在 mser.detectRegions 中出现错误。任何人都可以在 python3.5 中分享 MSER 的工作代码吗?

4

1 回答 1

1

你可以试试这个:

import cv2
import numpy as np

img = cv2.imread('adhr/adhr/front6.jpg')
mser = cv2.MSER_create()

#Resize the image so that MSER can work better
img = cv2.resize(img, (img.shape[1]*2, img.shape[0]*2))

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[0]]
cv2.polylines(vis, hulls, 1, (0,255,0))

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

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


cv2.namedWindow('img', 0)
cv2.imshow('img', vis)
while(cv2.waitKey()!=ord('q')):
    continue
cv2.destroyAllWindows()
于 2017-11-10T08:46:40.483 回答