我正在使用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()
答案 0 :(得分:5)
可能是,你想要的结果就像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):
结果: