简单的集装箱号码识别


2017-5-31:花了一个礼拜的时间,通过网上各种找资料,自己独自完成了一个简单的集装箱号码识别的小程序;

网上大多资料都是车牌号码识别,集装箱号码可能所处的环境因素会复杂一点,比如号码横竖问题、拍摄角度不同、号码字体不同...但总体框架还是和车牌识别一个道理的;能力有限很多因素都没考虑进来,暑假有机会的话希望能进一步研究一下,这先做个小小总结以防时间久了想复习一下都不知道从哪抓起;

1 获取号码区域

集装箱号码类似与文字区域的检测,先使用 morphologyEx函数膨胀,再用 cvFindContours 函数提取轮廓;

集装箱号码的排列一般分横型和竖型,所以膨胀处理也对应两种

// 寻找所有矩形轮廓
// 参数:     
// 1. pBinary: 输入二值图像,单通道,位深IPL_DEPTH_8U。    
// 2. Size s: 横竖形态选择参数
vector FindRect(IplImage *src_img, Size s)
{
	vector Rectbuf;

	IplImage *gray_img = cvCreateImage(cvGetSize(src_img), 8, 1);

	cvSmooth(src_img, src_img, CV_GAUSSIAN);	//IplI *arr转mat:mat(arr)
	cvCvtColor(src_img, gray_img, CV_BGR2GRAY);

	double th_maxArea = (gray_img->width*gray_img->height)*0.5;
	double th_minArea = (gray_img->width*gray_img->height)*0.01;
	double th_maxRatio = 0.3;	//height/width比
	double th_minRatio = 0.01;

	Mat element1 = getStructuringElement(MORPH_RECT, s);

	//morphologyEx(cvarrToMat(gray_img), cvarrToMat(gray_img), MORPH_CLOSE, element1);
	morphologyEx(cvarrToMat(gray_img), cvarrToMat(gray_img), MORPH_DILATE, element1);


	IplImage *pBinary = cvCreateImage(cvGetSize(gray_img), 8, 1);
	int thresh = Otsu(gray_img);
	cvThreshold(gray_img, pBinary, thresh, 255, CV_THRESH_BINARY);
	//cvAdaptiveThreshold(gray_img, pBinary, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 5, 5);


	CvSeq *pContour = NULL;
	CvMemStorage *pStorage = NULL;


	//IplImage *dst = cvCreateImage(cvGetSize(gray_img), 8, 3);
	// 执行条件     
	if (pBinary)
	{
		// 查找所有轮廓     
		pStorage = cvCreateMemStorage(0);
		cvFindContours(pBinary, pStorage, &pContour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
 
		int cnt = 0;
		CvContourScanner scanner = cvStartFindContours(pBinary, pStorage);
		while (pContour = cvFindNextContour(scanner))
		{
			//double tmparea = fabs(cvContourArea(pContour));
			CvRect rect = cvBoundingRect(pContour, 0);
			double tmparea = rect.height*rect.width;	//面积筛选
			if (tmparea < th_minArea || tmparea > th_maxArea)
			{
				cvSubstituteContour(scanner, NULL);//删除当前的轮廓  
				continue;
			}
			double tmpRatio = (rect.height th_maxRatio)//长宽比筛选
			{
				cvSubstituteContour(scanner, NULL);//删除当前的轮廓  
				continue;
			}

			cnt++;
			Rectbuf.push_back(rect);
			//cvDrawContours(src_img, pContour, CV_RGB(0, 0, 255), CV_RGB(0, 0, 255), 0, 2, CV_FILLED, cvPoint(0, 0));
			cvRectangle(src_img, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height), CV_RGB(255, 0, 0), 1, 8, 0);
		}
		pContour = cvEndFindContours(&scanner);

		cout << Rectbuf.size() << endl;

		cvReleaseMemStorage(&pStorage);
		pStorage = NULL;
	}
	cvNamedWindow("result");
	cvShowImage("result", src_img); waitKey();
	destroyWindow("result");

	cvReleaseImage(&pBinary);
	cvReleaseImage(&gray_img);

	return Rectbuf;
}

 2 确定单个字符 
  

3 识别字符


你可能感兴趣的:(简单的集装箱号码识别)