Opencv车牌号数字识别最后一步!大神帮忙~

Chris唐ml 2013-08-14 10:24:04
上周做车牌号数字识别,网上找了字符分割的代码(可以用,是老代码),自己写了单独数字(0~9)的识别代码(新代码),新老代码可以转换,但是这两段代码不清楚怎么才可以合到一起?(下面是我自己和在一起的代码,编译没问题,算法有问题。)目前的的问题就是车牌号虽然分割了,但是分割的几个字符怎么分别到单个数字识别的那个代码里,一一识别出来,并且最后输出车牌号数字。谢谢各位帮忙啦~~本周五及之前都可以的。。。给点提示或者帮我改下下。下面是代码:
#include <opencv2/opencv.hpp>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <cvaux.h>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
void findX(IplImage* img, int* min, int* max)
{
int found = 0;
CvScalar maxVal = cvRealScalar(img->width * 255);
CvScalar val = cvRealScalar(0);
CvMat data;
int minCount = img->width * 255 / 5;
int count = 0;

for (int i = 0; i < img->width; i++) {
cvGetCol(img, &data, i);
val = cvSum(&data);
if (val.val[0] < maxVal.val[0]) {
count = val.val[0];
if (count > minCount && count < img->width * 255) {
*max = i;
if (found == 0) {
*min = i;
found = 1;
}
}
}
}

}

//找出含车牌文字的最上端,排除两颗螺丝的位置
void findY(IplImage* img, int* min, int* max)
{
int found = 0;
CvScalar maxVal = cvRealScalar(img->height * 255);
CvScalar val = cvRealScalar(0);
CvMat data;
int minCount = img->width * 255 / 5;
int count = 0;

for (int i = 0; i < img->height; i++) {
cvGetRow(img, &data, i);
val = cvSum(&data);
if (val.val[0] < maxVal.val[0]) {
count = val.val[0];
if (count > minCount && count < img->height * 255) {
*max = i;
if (found == 0) {
*min = i;
found = 1;
}
}
}
}
}

//车牌字符的最小区域
CvRect findArea(IplImage* img)
{
int minX, maxX;
int minY, maxY;

findX(img, &minX, &maxX);
findY(img, &minY, &maxY);

CvRect rc = cvRect(minX, minY, maxX - minX, maxY - minY);

return rc;
}

Mat img_divide(Mat src)
{
Mat dst_gray,dst_gra,dst_gr;
int i=5;//矩阵大小

Mat dst( cvRound (src.rows/1), cvRound(src.cols/1), CV_8UC1 );
cvtColor( src, dst_gray, CV_BGR2GRAY );
resize( dst_gray, dst, dst.size(), 0, 0, INTER_LINEAR );
medianBlur (dst , dst_gra, i );
threshold( dst_gra, dst_gr, 0, 255,CV_THRESH_BINARY|CV_THRESH_OTSU );

//寻找最小区域,并截取
IplImage* pI = &dst_gr.operator IplImage();
CvRect rc = findArea(pI);
cvSetImageROI(pI, rc);
IplImage* img_gray2 = cvCreateImage(cvSize(rc.width, rc.height), IPL_DEPTH_8U, 1);
cvCopyImage(pI, img_gray2);
cvResetImageROI(pI);

IplImage* imgSrc2 = cvCreateImage(cvSize(rc.width, rc.height), IPL_DEPTH_8U, 3);
IplImage* P_src = &src.operator IplImage();
cvSetImageROI(P_src, rc);
cvCopyImage(P_src, imgSrc2);
cvResetImageROI(P_src);

//形态学
cvMorphologyEx(img_gray2, img_gray2, NULL, NULL, CV_MOP_CLOSE);

CvSeq* contours = NULL;
CvMemStorage* storage = cvCreateMemStorage(0);
int count = cvFindContours(img_gray2, storage, &contours,
sizeof(CvContour), CV_RETR_EXTERNAL);

int idx = 0;
char szName[56] = {0};

for (CvSeq* c = contours; c != NULL; c = c->h_next)
{

//cvDrawContours(imgSrc2, c, CV_RGB(255, 0, 0), CV_RGB(255, 255, 0), 100);
CvRect rc = cvBoundingRect(c);
cvDrawRect(imgSrc2, cvPoint(rc.x, rc.y), cvPoint(rc.x + rc.width, rc.y + rc.height), CV_RGB(255, 0, 0));

if (rc.width < imgSrc2->width / 10 && rc.height < imgSrc2->height / 5) { continue; }
}

IplImage* imgNo = cvCreateImage(cvSize(rc.width, rc.height), IPL_DEPTH_8U, 3);
cvSetImageROI(imgSrc2, rc);
cvCopyImage(imgSrc2, imgNo);
cvShowImage("src", imgNo);
Mat pic(imgNo);

cvWaitKey(0);
cvDestroyAllWindows();

return pic;
}

int img_gramme(Mat img,int* result)
{
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
float b[3],s[3],num[3];
int i;

/*int result[5];CV_THRESH_TOZERO|*/
CvPoint rect_pts[4], *pt = rect_pts;
Mat src_image;

threshold( img, src_image, 0, 255,CV_THRESH_BINARY );
img=src_image.clone();
findContours( src_image, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); //Find contours

vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );

for (i=0; i<contours.size(); i++)
{
s[i]=contourArea(contours[i]);

approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) );

result[0]=boundRect[i].x;
result[1]=boundRect[i].y;
result[2]=boundRect[i].width;
result[3]=boundRect[i].height;

s[i]=s[i]/(result[2]*result[3]);
b[i]=(float)result[2]/result[3];

boundRect[i].height=boundRect[i].height/2;
IplImage* pI = &img.operator IplImage();
cvSetImageROI(pI, boundRect[i]);
Mat img_half(pI);
num[i]=countNonZero(img_half);
num[i]=num[i]/(result[2]*result[3]);

cout<<"-面积:"<<s[i]<<" ";
cout<<"-长宽比:"<<b[i]<<" ";
cout<<"-长:"<<result[2]<<" ";
cout<<"-宽:"<<result[3]<<" ";
cout<<"半边面积"<<num[i]<<endl;
}
if(contours.size()==3) return 8;
else if(contours.size()==2)
{
if(b[1]<0.44&&b[0]>0.48&&b[0]<0.49) return 0;
else if(b[1]>0.49&&b[1]<0.6&&b[0]>0.49&&b[0]<0.508) return 4;
else if(b[1]>0.7&&b[1]<0.8&&b[0]>0.47&&b[0]<=0.48) return 9;
else if(b[1]>0.83&&b[1]<=1&&b[0]>0.508&&b[0]<0.6&&num[0]>0.14&&num[0]<0.19) return 6;
}
else if(contours.size()==1)
{
if(num[0]>0.15&&num[0]<0.2) return 2;
else if(num[0]>0.27&&num[0]<0.3) return 5;
else if(num[0]>0.48&&num[0]<0.6) return 1;
else if(num[0]>0.21&&num[0]<26)
if(s[0]>0.26&&s[0]<0.3) return 7;
else if(s[0]>0.35&&s[0]<0.45) return 3;

}
else return 11;
}

int main()
{
Mat src, dst_gr, src_image;
int result[5],data;
string pstrImageName;
string cmd;
string pstrWindowsDstTitle = "缩放图";

cout<<"What do you want to test ?"<<endl;
while(true)
{
waitKey(10);
cin>>pstrImageName;
src = imread( pstrImageName);

dst_gr=img_divide(src);
data=img_gramme(dst_gr,result);
cout<<"the num is : "<<data<<" !"<<endl;

imshow(pstrWindowsDstTitle,dst_gr);
}
waitKey(0);
return 0;
}
...全文
3228 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
huyazhou123 2016-04-07
  • 打赏
  • 举报
回复
车牌识别做出来了吗?
筱晓晓燕梓 2014-05-17
  • 打赏
  • 举报
回复
楼主你好,请问你的问题解决了吗?我最近也在做这个,遇到类似问题,求助楼主!

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧