车牌识别代码

   |    2016年5月17日  |   经验积累  |    评论已关闭  |    217

  1. #include <cv.h>
  2. #include <cxcore.h>
  3. #include <highgui.h>
  4. #include <iostream>
  5. using namespace std;
  6. #pragma comment(lib,”cv.lib”)
  7. #pragma comment(lib,”cxcore.lib”)
  8. #pragma comment(lib,”highgui.lib”)
  9. #define T 27
  10. #define T1 2
  11. #define S(image,x,y) ((uchar*)(image->imageData + image->widthStep*(y)))[(x)]     //S
  12. void main()
  13. {
  14.     IplImage *src;       /* 定义IplImage指针变量src*/
  15.     IplImage *pImg8u=NULL;             //灰度图
  16.     IplImage *pImg8uSmooth=NULL;       //高斯滤波后的图
  17.     IplImage *pImgCanny=NULL;          //二值化的图
  18.     IplImage *pImgHist=NULL;           //直方图
  19.     int hist_size=155;
  20.     float range_0[]={0,256};
  21.     float *ranges[]={range_0};
  22.     int i,j,bin_w;
  23.     float max_value,min_value;
  24.     int min_dx,max_dx;
  25.     int row_start,row_end;//用来记录车牌开始,结束行
  26.     int col_start,col_end;//用来记录车牌开始,结束列
  27.     int count=0;//用来记录行或列的白点个数
  28.     src=cvLoadImage(“F:\\毕业设计\\车牌识别\\车牌识别\\12.bmp”,-1);
  29.     pImg8uSmooth=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
  30.     pImg8u=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
  31.     pImgCanny=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
  32.     cvCvtColor(src,pImg8u,CV_RGB2GRAY);           //灰度化
  33.     cvSmooth(pImg8u,pImg8uSmooth,CV_GAUSSIAN,3,0,0);//高斯滤波
  34.     cvCanny(pImg8uSmooth,pImgCanny,100,200,3);    //二值化
  35.     /*cvDilate(pImgCanny,pImgCanny,0,1);
  36.     cvErode(pImgCanny,pImgCanny,0,1);*/
  37.     row_start=0;
  38.     row_end=0;
  39.     col_start=0;
  40.     col_end=0;
  41.     int row[120];
  42.     int col[340];
  43.     int k;
  44.     k=0;
  45.     bool flag=false;
  46.     for(j=0;j<pImgCanny->height;j++)           //找到上行开始
  47.     {
  48.         count=0;
  49.         for(i=0;i<pImgCanny->width-1;i++)
  50.         {
  51.             if(S(pImgCanny,i,j)!=S(pImgCanny,i+1,j))  //统计行跳数
  52.                 count++;
  53.             if(count>T)
  54.             {
  55.                 row[k]=j;
  56.                 k++;
  57.                 break;
  58.             }
  59.         }
  60.     }
  61.     for(i=0;i<k;i++)          //从上边开始,3行连续时认为是起始行
  62.     {
  63.         if((row[i]==row[i+1]-1)&&(row[i+1]==row[i+2]-1)){
  64.             row_start=row[i];
  65.             break;
  66.         }
  67.     }
  68.     cout<<“the start row:”<<row_start<<endl;
  69.     cvLine(pImg8u,cvPoint(0,row_start),cvPoint(src->width,row_start),cvScalar(255,0,0),1,8,0);
  70.     for(i=k-1;i>row_start;i–)     //从下边开始,3行连续时认为是起始行
  71.     {
  72.         if((row[i]==row[i-1]+1)&&(row[i-1]==row[i-2]+1)){
  73.             row_end=row[i];
  74.             break;
  75.         }
  76.     }
  77.     cout<<“the end row:”<<row_end<<endl;
  78.     cvLine(pImg8u,cvPoint(0,row_end),cvPoint(src->width,row_end),cvScalar(255,0,0),1,8,0);
  79.     flag=false;
  80.     for(i=10;i<pImgCanny->width;i++)           //找到左列开始
  81.     {
  82.         count=0;
  83.         for(j=row_start;j<row_end;j++)
  84.         {
  85.             if(S(pImgCanny,i,j)==255)
  86.                 count++;
  87.             if(count>T1)
  88.             {
  89.                 col_start=i;
  90.                 flag=true;
  91.                 break;
  92.             }
  93.         }
  94.         if(flag) break;
  95.     }
  96.     cout<<“the start col:”<<col_start<<endl;
  97.     cvLine(pImg8u,cvPoint(col_start,row_start),cvPoint(col_start,row_end),cvScalar(255,0,0),1,8,0);
  98.     flag=false;
  99.     for(i=pImgCanny->width-10;i>col_start;i–)           //找到右列开始
  100.     {
  101.         count=0;
  102.         for(j=row_start;j<row_end;j++)
  103.         {
  104.             if(S(pImgCanny,i,j)==255)
  105.                 count++;
  106.             if(count>T1)
  107.             {
  108.                 col_end=i;
  109.                 flag=true;
  110.                 break;
  111.             }
  112.         }
  113.         if(flag) break;
  114.     }
  115.     cout<<“the end col:”<<col_end<<endl;
  116.     cvLine(pImg8u,cvPoint(col_end,row_start),cvPoint(col_end,row_end),cvScalar(255,0,0),1,8,0);
  117.     CvRect ROI_rect;                 //获得图片感兴趣区域
  118.     ROI_rect.x=col_start;
  119.     ROI_rect.y=row_start;
  120.     ROI_rect.width=col_end-col_start;
  121.     ROI_rect.height=row_end-row_start;
  122.     IplImage *pImg8uROI=NULL;         //感兴趣的图片
  123.     cvSetImageROI(pImg8u,ROI_rect);
  124.     pImg8uROI=cvCreateImage(cvSize(ROI_rect.width,ROI_rect.height),IPL_DEPTH_8U,1);
  125.     cvCopy(pImg8u,pImg8uROI);
  126.     cvResetImageROI(pImg8u);
  127.     int nWidth=409;//(409,90)分别为感兴趣图像的宽度与高度
  128.     int nHeight=90;
  129.     IplImage *pImgResize=NULL;        //归一化的灰度图
  130.     pImgResize=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);
  131.     cvResize(pImg8uROI,pImgResize,CV_INTER_LINEAR); //线性插值
  132.     int nCharWidth=45;
  133.     int nSpace=12;
  134.     for(i=0;i<7;i++)           //得到每个字符的双边界
  135.     {
  136.         switch(i){
  137.             case 0:
  138.             case 1:
  139.                 col[i*2]=i*nCharWidth+i*nSpace;
  140.                 col[i*2+1]=(i+1)*nCharWidth+i*nSpace;
  141.                 break;
  142.             case 2:
  143.             case 3:
  144.             case 4:
  145.             case 5:
  146.             case 6:
  147.                 col[i*2]=i*nCharWidth+i*nSpace+22;
  148.                 col[i*2+1]=(i+1)*nCharWidth+i*nSpace+22;
  149.                 break;
  150.         }
  151.     }
  152.     for(i=0;i<14;i++)        //画出每个字符的区域
  153.     {
  154.         cvLine(pImgResize,cvPoint(col[i],0),cvPoint(col[i],nHeight),cvScalar(255,0,0),1,8,0);
  155.         //cout<<col[i*2]<<” “<<col[2*i+1]<<” “;
  156.     }
  157.     IplImage *pImgCharOne=NULL;
  158.     IplImage *pImgCharTwo=NULL;
  159.     IplImage *pImgCharThree=NULL;
  160.     IplImage *pImgCharFour=NULL;
  161.     IplImage *pImgCharFive=NULL;
  162.     IplImage *pImgCharSix=NULL;
  163.     IplImage *pImgCharSeven=NULL;
  164.     pImgCharOne=cvCreateImage(cvSize(nCharWidth,nHeight),IPL_DEPTH_8U,1);
  165.     pImgCharTwo=cvCreateImage(cvSize(nCharWidth,nHeight),IPL_DEPTH_8U,1);
  166.     pImgCharThree=cvCreateImage(cvSize(nCharWidth,nHeight),IPL_DEPTH_8U,1);
  167.     pImgCharFour=cvCreateImage(cvSize(nCharWidth,nHeight),IPL_DEPTH_8U,1);
  168.     pImgCharFive=cvCreateImage(cvSize(nCharWidth,nHeight),IPL_DEPTH_8U,1);
  169.     pImgCharSix=cvCreateImage(cvSize(nCharWidth,nHeight),IPL_DEPTH_8U,1);
  170.     pImgCharSeven=cvCreateImage(cvSize(nCharWidth,nHeight),IPL_DEPTH_8U,1);
  171.     CvRect ROI_rect1;
  172.     ROI_rect1.x=col[0];
  173.     ROI_rect1.y=0;
  174.     ROI_rect1.width=nCharWidth;
  175.     ROI_rect1.height=nHeight;
  176.     cvSetImageROI(pImgResize,ROI_rect1);
  177.     cvCopy(pImgResize,pImgCharOne,NULL); //获取第1个字符
  178.     cvResetImageROI(pImgResize);
  179.     ROI_rect1.x=col[2];
  180.     ROI_rect1.y=0;
  181.     ROI_rect1.width=nCharWidth;
  182.     ROI_rect1.height=nHeight;
  183.     cvSetImageROI(pImgResize,ROI_rect1);
  184.     cvCopy(pImgResize,pImgCharTwo,NULL); //获取第2个字符
  185.     cvResetImageROI(pImgResize);
  186.     ROI_rect1.x=col[4];
  187.     ROI_rect1.y=0;
  188.     ROI_rect1.width=nCharWidth;
  189.     ROI_rect1.height=nHeight;
  190.     cvSetImageROI(pImgResize,ROI_rect1);
  191.     cvCopy(pImgResize,pImgCharThree,NULL); //获取第3个字符
  192.     cvResetImageROI(pImgResize);
  193.     ROI_rect1.x=col[6];
  194.     ROI_rect1.y=0;
  195.     ROI_rect1.width=nCharWidth;
  196.     ROI_rect1.height=nHeight;
  197.     cvSetImageROI(pImgResize,ROI_rect1);
  198.     cvCopy(pImgResize,pImgCharFour,NULL); //获取第4个字符
  199.     cvResetImageROI(pImgResize);
  200.     ROI_rect1.x=col[8];
  201.     ROI_rect1.y=0;
  202.     ROI_rect1.width=nCharWidth;
  203.     ROI_rect1.height=nHeight;
  204.     cvSetImageROI(pImgResize,ROI_rect1);
  205.     cvCopy(pImgResize,pImgCharFive,NULL); //获取第5个字符
  206.     cvResetImageROI(pImgResize);
  207.     ROI_rect1.x=col[10];
  208.     ROI_rect1.y=0;
  209.     ROI_rect1.width=nCharWidth;
  210.     ROI_rect1.height=nHeight;
  211.     cvSetImageROI(pImgResize,ROI_rect1);
  212.     cvCopy(pImgResize,pImgCharSix,NULL); //获取第6个字符
  213.     cvResetImageROI(pImgResize);
  214.     ROI_rect1.x=col[12];
  215.     ROI_rect1.y=0;
  216.     ROI_rect1.width=nCharWidth;
  217.     ROI_rect1.height=nHeight;
  218.     cvSetImageROI(pImgResize,ROI_rect1);
  219.     cvCopy(pImgResize,pImgCharSeven,NULL); //获取第7个字符
  220.     cvResetImageROI(pImgResize);
  221.     /*cvLine(src,cvPoint(col_start,row_start),cvPoint(col_end,row_start),cvScalar(255,0,0),3,8,0); //在原图中得到车牌区域
  222.     cvLine(src,cvPoint(col_start,row_end),cvPoint(col_end,row_end),cvScalar(255,0,0),3,8,0);
  223.     cvLine(src,cvPoint(col_start,row_start),cvPoint(col_start,row_end),cvScalar(255,0,0),3,8,0);
  224.     cvLine(src,cvPoint(col_end,row_start),cvPoint(col_end,row_end),cvScalar(255,0,0),3,8,0);*/
  225.     pImgHist=cvCreateImage(cvSize(300,200),8,1);   //画直方图
  226.     CvHistogram *hist=cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
  227.     cvCalcHist(&pImg8uSmooth,hist,0,0);
  228.     cvGetMinMaxHistValue(hist,&min_value,&max_value,&min_dx,&max_dx);
  229.     cout<<min_value<<”  “<<max_value<<”  “<<min_dx<<”  “<<max_dx<<endl;
  230.     cvScale(hist->bins,hist->bins,((double)pImgHist->height)/max_value,0);
  231.     cvSet(pImgHist,cvScalarAll(255),0);
  232.     bin_w=cvRound((pImgHist->width)/hist_size);
  233.     for(i=0;i<hist_size;i++)
  234.     {
  235.         cvRectangle(pImgHist,cvPoint(i*bin_w,pImgHist->height),
  236.             cvPoint((i+1)*bin_w,pImgHist->height-cvRound(cvGetReal1D(hist->bins,i))),
  237.             cvScalarAll(0),-1,8,0);
  238.     }
  239.     CvMemStorage *storage=NULL;
  240.     CvSeq *lines=NULL;
  241.     storage=cvCreateMemStorage(0);
  242.     lines=cvHoughLines2(pImgCanny,storage,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,80,30,20);
  243.     /*for(i=0;i<lines->total;i++)
  244.     {
  245.         CvPoint *line=(CvPoint*)cvGetSeqElem(lines,i);
  246.         cvLine(src,line[0],line[1],CV_RGB(255,0,0),3,8,0);
  247.     }*/
  248.     /*cvNamedWindow(“src”,1);
  249.     cvShowImage(“src”,src);
  250.     cvNamedWindow(“pimg8u”,1);
  251.     cvShowImage(“pimg8u”,pImg8u);
  252.     cvNamedWindow(“canny”,1);
  253.     cvShowImage(“canny”,pImgCanny);*/
  254.     cvNamedWindow(“resize”,1);
  255.     cvShowImage(“resize”,pImgResize);
  256.     cvNamedWindow(“roi”,1);
  257.     cvShowImage(“roi”,pImg8uROI);
  258.     /*cvNamedWindow(“hist”,1);
  259.     cvShowImage(“hist”,pImgHist); */
  260.     cvNamedWindow(“one”,CV_WINDOW_AUTOSIZE);
  261.     cvShowImage(“one”,pImgCharOne);
  262.     cvNamedWindow(“two”,1);
  263.     cvShowImage(“two”,pImgCharTwo);
  264.     cvNamedWindow(“three”,1);
  265.     cvShowImage(“three”,pImgCharThree);
  266.     cvNamedWindow(“four”,1);
  267.     cvShowImage(“four”,pImgCharFour);
  268.     cvNamedWindow(“five”,1);
  269.     cvShowImage(“five”,pImgCharFive);
  270.     cvNamedWindow(“six”,1);
  271.     cvShowImage(“six”,pImgCharSix);
  272.     cvNamedWindow(“seven”,1);
  273.     cvShowImage(“seven”,pImgCharSeven);
  274.     cvWaitKey(0);
  275.     /*cvReleaseImage(&src);
  276.     cvReleaseImage(&pImg8u);
  277.     cvReleaseImage(&pImg8uSmooth);
  278.     cvReleaseImage(&pImgCanny);
  279.     cvReleaseImage(&pImgHist);*/
  280.     cvReleaseImage(&pImgResize);
  281.     cvReleaseImage(&pImg8uROI);
  282.     cvReleaseImage(&pImgCharOne);
  283.     cvReleaseImage(&pImgCharTwo);
  284.     cvReleaseImage(&pImgCharThree);
  285.     cvReleaseImage(&pImgCharFour);
  286.     cvReleaseImage(&pImgCharFive);
  287.     cvReleaseImage(&pImgCharSix);
  288.     cvReleaseImage(&pImgCharSeven);
  289.     cvDestroyWindow(“roi”);
  290.     /*cvDestroyWindow(“src”);
  291.     cvDestroyWindow(“pimg8u”);
  292.     cvDestroyWindow(“canny”);
  293.     cvDestroyWindow(“hist”);*/
  294.     cvDestroyWindow(“resize”);
  295.     cvDestroyWindow(“one”);
  296.     cvDestroyWindow(“two”);
  297.     cvDestroyWindow(“three”);
  298.     cvDestroyWindow(“four”);
  299.     cvDestroyWindow(“five”);
  300.     cvDestroyWindow(“six”);
  301.     cvDestroyWindow(“seven”);
  302. }

噢!评论已关闭。