mser处理文本区域

时间:2021-04-18 22:19:44

 

Maximal stable extremal regions 在opencv中有原型。可以用来检测文本区域。本文对比率他和SWT的结果

效果依情况而定。

int main( int argc, char** argv )
{

	char* filename = argc >= 2 ? argv[1] : (char*)"../mingpian.jpg";
	Mat img0 = imread(filename, 1);
	Mat rgb_test = imread(filename, 1);
	vector<Mat>sbgr(rgb_test.channels());
	split(rgb_test,sbgr);
	imshow("blue",sbgr[2]);
	Mat gray_img, mask_img;
	vector<vector<Point>> contours;
	cvtColor(img0,gray_img,COLOR_BGR2GRAY);
	MSER ms(5, 20,14400,
		0.25,.2,
		200, 1.01,
		0.003,5 );
	ms(sbgr[2],contours,mask_img);
	Mat wshed(gray_img.size(), CV_8UC3);
	wshed = Scalar::all(255);
	for ( int i = 0; i < contours.size(); i++ )
	{
		const Point* p = &contours[i][0];
		int n = (int)contours[i].size();
		if (contourArea(Mat(contours[i]))<((gray_img.cols*gray_img.rows)/100)){	
			Rect a = boundingRect(contours[i]);
			if(a.width/a.height<5&&a.height/a.width<5){
				for (int j = 0; j<n; j++)
				{

					Point sd = contours[i][j];
					wshed.at<Vec3b>(sd.y,sd.x) = Vec3b(0,0,0);
				}
				rectangle(img0,Point(a.x,a.y),Point(a.x+a.width,a.y+a.height),Scalar(0,255,0),-1,8,0);
			}
		}
	}

	Mat gray_wshed,m_gaussianImage ;
	cvtColor(wshed,gray_wshed,COLOR_BGR2GRAY);
	gray_wshed.convertTo(m_gaussianImage,CV_32FC3);
	m_gaussianImage/=255;
	IplImage* textimage = cvCreateImage(cvSize(wshed.cols,wshed.rows),IPL_DEPTH_8U,1);
	imshow("sdf",sbgr[1]);

	waitKey(0);
}


选择红色通道的原因是给名片在红色下对比度高,而且去处了红色背景

mser处理文本区域

原图


mser处理文本区域

mser处理之后mser处理文本区域处SWT处理