我试图在OpenCV 3.0.0 beta中使用MSER算法从图像中提取文本区域.最后,我需要一个带有检测到的MSER区域的二进制掩码,但该算法仅提供轮廓.我试图绘制这些轮廓但我没有得到预期的结果.
这是我使用的代码:
void mserExtractor (const Mat& image, Mat& mserOutMask){
Ptr<MSER> mserExtractor = MSER::create();
vector<vector<cv::Point>> mserContours;
vector<cv::Rect> mserBbox;
mserExtractor->detectRegions(image, mserContours, mserBbox);
for( int i = 0; i<mserContours.size(); i++ )
{
drawContours(mserOutMask, mserContours, i, Scalar(255, 255, 255), 4);
}
}
Run Code Online (Sandbox Code Playgroud)
这是结果:

问题是非凸区域由穿过实际MSER区域的线填充.我想像我从MATLAB获得的区域像素列表detectMSERFeatures:

任何想法如何从轮廓中获取填充区域(或以其他方式获取MSER掩码)?
正如@Silencer所建议的,我使用他在这里发布的代码来绘制图像中数字的轮廓.在某些时候,处理数字,就像0,6,8,9我看到他们的内部轮廓(圆圈)也被填充.我怎么能阻止这个?是否有为cv2.drawContours()设置的最小/最大动作区域,所以我可以排除内部区域?
我试图传递cv2.RETR_EXTERNAL但是使用此参数只考虑整个外部区域.
代码就是这样(再次感谢Silencer.几个月来一直在寻找这个......):
import numpy as np
import cv2
im = cv2.imread('imgs\\2.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#contours.sort(key=lambda x: int(x.split('.')[0]))
for i, cnts in enumerate(contours):
## this contour is a 3D numpy array
cnt = contours[i]
res = cv2.drawContours(im, [cnt], 0, (255, 0, 0), 1)
cv2.imwrite("contours.png", res)
'''
## Method 1: crop the region
x,y,w,h = cv2.boundingRect(cnt)
croped = res[y:y+h, x:x+w] …Run Code Online (Sandbox Code Playgroud) 我正在尝试确定动脉的宽度,并制作了一个非常粗略的程序来验证 drawContours 是否合适。我的问题是轮廓仅打印黑色或白色线条,而不是我尝试使用的彩色线条。我不确定我正在绘制的img是否仅限于 bw 或者我的轮廓设置错误。第二张图像是附加到原始图像的轮廓。

import cv2
import numpy
from FileSupport import *
# Declared Variables ###########################
# file vars
image_file_name = '14.05.25 hrs __[0011697].avi' # this will be given through some file selection of the user
temp_image_file_path = ReturnTempImagePath() # gives file path for .avi files to be stored
# image vars
sample_start_row = 144 # measurements are based off the 640x480 sample image
sample_end_row = 408
sample_start_col = 159
sample_end_col = 518
# colors
RED = …Run Code Online (Sandbox Code Playgroud)