(转) 学习OpenCV——车牌检测(定位)

本文介绍了使用OpenCV进行车牌检测的多种方法,包括基于颜色分布、Canny边缘检测和Hough变换的策略。通过实验,探讨了HSV空间、Canny边缘检测与Hough变换在车牌检测中的应用,以及轮廓检测方法。虽然效果各有不同,但这些技术为车牌定位提供了基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:http://blog.youkuaiyun.com/sangni007/article/details/7444470

这两天在做关于车牌识别的实验,用了几种方式:

1.车牌颜色分布(HSV空间,YCrCb空间的没有颜色分布图谱,无法实验);利用HSV的H通道,效果一般,受环境影响大。

  1. #include "highgui.h"  
  2. #include "cv.h"  
  3. #include <stdio.h>     
  4. #include <math.h>    
  5. #include <string>  
  6. #include<iostream>  
  7. using namespace std;  
  8.   
  9. CvPoint Point;  
  10. IplImage* img=0;  
  11. // skin region location using rgb limitation  
  12. void SkinRGB(IplImage* rgb,IplImage* _dst)  
  13. {  
  14.     assert(rgb->nChannels==3&& _dst->nChannels==3);  
  15.   
  16.     static const int R=2;  
  17.     static const int G=1;  
  18.     static const int B=0;  
  19.   
  20.     IplImage* dst=cvCreateImage(cvGetSize(_dst),8,3);  
  21.     cvZero(dst);  
  22.   
  23.     for (int h=0;h<rgb->height;h++) {  
  24.         unsigned char* prgb=(unsigned char*)rgb->imageData+h*rgb->widthStep;  
  25.         unsigned char* pdst=(unsigned char*)dst->imageData+h*dst->widthStep;  
  26.         for (int w=0;w<rgb->width;w++) {  
  27.             if ((prgb[R]>95 && prgb[G]>40 && prgb[B]>20 &&  
  28.                 prgb[R]-prgb[B]>15 && prgb[R]-prgb[G]>15/*&& 
  29.                 !(prgb[R]>170&&prgb[G]>170&&prgb[B]>170)*/)||//uniform illumination   
  30.                 (prgb[R]>200 && prgb[G]>210 && prgb[B]>170 &&  
  31.                 abs(prgb[R]-prgb[B])<=15 && prgb[R]>prgb[B]&& prgb[G]>prgb[B])//lateral illumination  
  32.                 ) {  
  33.                     memcpy(pdst,prgb,3);  
  34.             }             
  35.             prgb+=3;  
  36.             pdst+=3;  
  37.         }  
  38.     }  
  39.     cvCopyImage(dst,_dst);  
  40.     cvReleaseImage(&dst);  
  41. }  
  42. // skin detection in rg space  
  43. void cvSkinRG(IplImage* rgb,IplImage* gray)  
  44. {  
  45.     assert(rgb->nChannels==3&&gray->nChannels==1);  
  46.       
  47.     const int R=2;  
  48.     const int G=1;  
  49.     const int B=0;  
  50.   
  51.     double Aup=-1.8423;  
  52.     double Bup=1.5294;  
  53.     double Cup=0.0422;  
  54.     double Adown=-0.7279;  
  55.     double Bdown=0.6066;  
  56.     double Cdown=0.1766;  
  57.     for (int h=0;h<rgb->height;h++) {  
  58.         unsigned char* pGray=(unsigned char*)gray->imageData+h*gray->widthStep;  
  59.         unsigned char* pRGB=(unsigned char* )rgb->imageData+h*rgb->widthStep;  
  60.         for (int w=0;w<rgb->width;w++)   
  61.         {  
  62.             int s=pRGB[R]+pRGB[G]+pRGB[B];  
  63.             double r=(double)pRGB[R]/s;  
  64.             double g=(double)pRGB[G]/s;  
  65.             double Gup=Aup*r*r+Bup*r+Cup;  
  66.             double Gdown=Adown*r*r+Bdown*r+Cdown;  
  67.             double Wr=(r-0.33)*(r-0.33)+(g-0.33)*(g-0.33);  
  68.             if (g<Gup && g>Gdown && Wr>0.004)  
  69.             {  
  70.                 *pGray=255;  
  71.             }  
  72.             else  
  73.             {   
  74.                 *pGray=0;  
  75.             }  
  76.             pGray++;  
  77.             pRGB+=3;  
  78.         }  
  79.     }  
  80.   
  81. }  
  82. // implementation of otsu algorithm  
  83. // author: onezeros#yahoo.cn  
  84. // reference: Rafael C. Gonzalez. Digital Image Processing Using MATLAB  
  85. void cvThresholdOtsu(IplImage* src, IplImage* dst)  
  86. {  
  87.     int height=src->height;  
  88.     int width=src->width;  
  89.   
  90.     //histogram  
  91.     float histogram[256]={0};  
  92.     for(int i=0;i<height;i++) {  
  93.         unsigned char* p=(unsigned char*)src->imageData+src->widthStep*i;  
  94.         for(int j=0;j<width;j++) {  
  95.             histogram[*p++]++;  
  96.         }  
  97.     }  
  98.     //normalize histogram  
  99.     int size=height*width;  
  100.     for(int i=0;i<256;i++) {  
  101.         histogram[i]=histogram[i]/size;  
  102.     }  
  103.   
  104.     //average pixel value  
  105.     float avgValue=0;  
  106.     for(int i=0;i<256;i++) {  
  107.         avgValue+=i*histogram[i];  
  108.     }  
  109.   
  110.     int threshold;    
  111.     float maxVariance=0;  
  112.     float w=0,u=0;  
  113.     for(int i=0;i<256;i++) {  
  114.         w+=histogram[i];  
  115.         u+=i*histogram[i];  
  116.   
  117.         float t=avgValue*w-u;  
  118.         float variance=t*t/(w*(1-w));  
  119.         if(variance>maxVariance) {  
  120.             maxVariance=variance;  
  121.             threshold=i;  
  122.         }  
  123.     }  
  124.   
  125.     cvThreshold(src,dst,threshold,255,CV_THRESH_BINARY);  
  126. }  
  127.   
  128. void cvSkinOtsu(IplImage* src, IplImage* dst)  
  129. {  
  130.     assert(dst->nChannels==1&& src->nChannels==3);  
  131.   
  132.     IplImage* ycrcb=cvCreateImage(cvGetSize(src),8,3);  
  133.     IplImage* cr=cvCreateImage(cvGetSize(src),8,1);  
  134.     cvCvtColor(src,ycrcb,CV_BGR2YCrCb);  
  135.     cvSplit(ycrcb,0,cr,0,0);  
  136.   
  137.     cvThresholdOtsu(cr,cr);  
  138.     cvCopyImage(cr,dst);  
  139.     cvReleaseImage(&cr);  
  140.     cvReleaseImage(&ycrcb);  
  141. }  
  142.   
  143. void cvSkinYUV(IplImage* src,IplImage* dst)  
  144. {  
  145.     IplImage* ycrcb=cvCreateImage(cvGetSize(src),8,3);  
  146.     //IplImage* cr=cvCreateImage(cvGetSize(src),8,1);  
  147.     //IplImage* cb=cvCreateImage(cvGetSize(src),8,1);  
  148.     cvCvtColor(src,ycrcb,CV_BGR2YCrCb);  
  149.     //cvSplit(ycrcb,0,cr,cb,0);  
  150.   
  151.     static const int Cb=2;  
  152.     static const int Cr=1;  
  153.     static const int Y=0;  
  154.   
  155.     //IplImage* dst=cvCreateImage(cvGetSize(_dst),8,3);  
  156.     cvZero(dst);  
  157.   
  158.     for (int h=0;h<src->height;h++) {  
  159.         unsigned char* pycrcb=(unsigned char*)ycrcb->imageData+h*ycrcb->widthStep;  
  160.         unsigned char* psrc=(unsigned char*)src->imageData+h*src->widthStep;  
  161.         unsigned char* pdst=(unsigned char*)dst->imageData+h*dst->widthStep;  
  162.         for (int w=0;w<src->width;w++) {  
  163.             if ((pycrcb[Cr]<=126||pycrcb[Cr]>=130)&&(pycrcb[Cb]<=126||pycrcb[Cb]>=130))  
  164.             {  
  165.                     memcpy(pdst,psrc,3);  
  166.             }  
  167.             pycrcb+=3;  
  168.             psrc+=3;  
  169.             pdst+=3;  
  170.         }  
  171.     }  
  172.     //cvCopyImage(dst,_dst);  
  173.     //cvReleaseImage(&dst);  
  174. }  
  175.   
  176. void cvSkinHSV(IplImage* src,IplImage* dst)  
  177. {  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值