尝试使用matplotlib绘制OpenCV的MSER区域 [英] Trying to Plot OpenCV's MSER regions using matplotlib

查看:108
本文介绍了尝试使用matplotlib绘制OpenCV的MSER区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using OpenCV's MSER feature detector to find text regions. With the following Python code, I can detect texts (and some non-texts) and draw polygonal curves around each alphabet. Now, I need to plot these texts (more specifically each alphabet) using matplotlib using different colors. Different colors are important here. I am new to matplotlib and I cannot figure out how to implement that. I seek your guidance. I do not need a full solution, but some hints will be helpful.

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

推荐答案

也许是,您想要的结果就像Matlab一样.您应该执行更多步骤来获得结果.找到坐标,用随机颜色修改值.

May be, you want the result just like Matlab. You should do more steps to do to get the result. Find the coordinates, modify the values with random color.

这是我的OpenCV 3.3的Python 3代码.

Here is my Python 3 code for OpenCV 3.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):


The original image (handicapSign.jpg):

结果:

这篇关于尝试使用matplotlib绘制OpenCV的MSER区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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