【问题标题】:Trying to Plot OpenCV's MSER regions using matplotlib尝试使用 matplotlib 绘制 OpenCV 的 MSER 区域
【发布时间】:2016-03-27 16:12:47
【问题描述】:

我正在使用 OpenCV 的 MSER 特征检测器来查找文本区域。使用以下 Python 代码,我可以检测文本(和一些非文本)并围绕每个字母绘制多边形曲线。现在,我需要使用 matplotlib 使用不同的颜色绘制这些文本(更具体地说是每个字母表)。不同的颜色在这里很重要。我是 matplotlib 的新手,我不知道如何实现它。我寻求你的指导。我不需要完整的解决方案,但一些提示会有所帮助。

import numpy as np
import cv2
import matplotlib.pyplot as plt #plt.plot(x,y) plt.show()

img = cv2.imread('TestText.png')
mser = cv2.MSER_create()

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()

regions = mser.detectRegions(gray, None)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(vis, hulls, 1, (0, 255, 0)) 
# cv2.putText(vis, str('change'), (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0))
# cv2.fillPoly(vis, hulls, (0, 255, 0))


# cv2.imwrite("test.png", vis)    
cv2.imshow('img', vis)
cv2.waitKey(0)
cv2.destroyAllWindows()

【问题讨论】:

  • drawContoursCV_FILLED 和一些随机颜色一起使用,就像使用findContours 返回的轮廓一样

标签: python opencv matplotlib


【解决方案1】:

可能是,您想要的结果就像 Matlab 一样。你应该做更多的步骤来获得结果。找到坐标,用随机颜色修改数值。

这是我用于 OpenCV 3.3 的 Python 3 代码。

#!/usr/bin/python3
# 2017.10.05 10:52:58 CST
# 2017.10.05 13:27:18 CST
"""
Text detection with MSER, and fill with random colors for each detection.
"""

import numpy as np
import cv2

## Read image and change the color space
imgname = "handicapSign.jpg"
img = cv2.imread(imgname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

## Get mser, and set parameters
mser = cv2.MSER_create()
mser.setMinArea(100)
mser.setMaxArea(800)

## Do mser detection, get the coodinates and bboxes
coordinates, bboxes = mser.detectRegions(gray)

## Filter the coordinates
vis = img.copy()
coords = []
for coord in coordinates:
    bbox = cv2.boundingRect(coord)
    x,y,w,h = bbox
    if w< 10 or h < 10 or w/h > 5 or h/w > 5:
        continue
    coords.append(coord)

## colors 
colors = [[43, 43, 200], [43, 75, 200], [43, 106, 200], [43, 137, 200], [43, 169, 200], [43, 200, 195], [43, 200, 163], [43, 200, 132], [43, 200, 101], [43, 200, 69], [54, 200, 43], [85, 200, 43], [116, 200, 43], [148, 200, 43], [179, 200, 43], [200, 184, 43], [200, 153, 43], [200, 122, 43], [200, 90, 43], [200, 59, 43], [200, 43, 64], [200, 43, 95], [200, 43, 127], [200, 43, 158], [200, 43, 190], [174, 43, 200], [142, 43, 200], [111, 43, 200], [80, 43, 200], [43, 43, 200]]

## Fill with random colors
np.random.seed(0)
canvas1 = img.copy()
canvas2 = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
canvas3 = np.zeros_like(img)

for cnt in coords:
    xx = cnt[:,0]
    yy = cnt[:,1]
    color = colors[np.random.choice(len(colors))]
    canvas1[yy, xx] = color
    canvas2[yy, xx] = color
    canvas3[yy, xx] = color

## Save 
cv2.imwrite("result1.png", canvas1)
cv2.imwrite("result2.png", canvas2)
cv2.imwrite("result3.png", canvas3)

原图(handicapSign.jpg):

结果:

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2017-02-25
    • 2011-09-03
    • 1970-01-01
    • 2018-06-17
    • 2018-01-05
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    相关资源
    最近更新 更多