您的位置:首页 > 运维架构

opencv 车牌定位及分割

2012-08-30 16:31 211 查看
车牌识别大概步骤可分为:车牌定位,字符分割,字符识别三个步骤。

细分点可以有以下几个步骤:

(1)将图片灰度化与二值化

(2)去噪,然后切割成一个一个的字符

(3)提取每一个字符的特征,生成特征矢量或特征矩阵

(4)分类与学习。将特征矢量或特征矩阵与样本库进行比对,挑选出相似的那类样本,将这类样本的值作为输出结果。

下面是车牌识别的第一个步骤,opencv源代码中sample有一个识别矩形的例子,网上资料说改改此代码就可以定位车牌,没有验证过,先贴个代码,权当记录一下,有时间的话再去实践一下。

也可参考以下文章:
http://blog.csdn.net/hbclc/archive/2007/10/14/1824365.aspx
代码如下:

//

// The full "Square Detector" program.

// It loads several images subsequentally and tries to find squares in

// each image

//

#ifdef _CH_

#pragma package <opencv>

#endif

#define CV_NO_BACKWARD_COMPATIBILITY

#include "cv.h"

#include "highgui.h"

#include <stdio.h>

#include <math.h>

#include <string.h>

int thresh = 50;

IplImage* img = 0;

IplImage* img0 = 0;

CvMemStorage* storage = 0;

const char* wndname = "Square Detection Demo";

// helper function:

// finds a cosine of angle between vectors

// from pt0->pt1 and from pt0->pt2

double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 )

{

    double dx1 = pt1->x - pt0->x;

    double dy1 = pt1->y - pt0->y;

    double dx2 = pt2->x - pt0->x;

    double dy2 = pt2->y - pt0->y;

    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);

}

// returns sequence of squares detected on the image.

// the sequence is stored in the specified memory storage

CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )

{

    CvSeq* contours;

    int i, c, l, N = 11;
    CvSize sz = cvSize( img->width & -2, img->height & -2 );    //保证最后一位是偶数,by sing 2010-10-11

    IplImage* timg = cvCloneImage( img ); // make a copy of input image

    IplImage* gray = cvCreateImage( sz, 8, 1 );

    IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 3 );

    IplImage* tgray;

    CvSeq* result;

    double s, t;

    // create empty sequence that will contain points -

    // 4 points per square (the square's vertices)

    CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );

    // select the maximum ROI in the image

    // with the width and height divisible by 2

    cvSetImageROI( timg, cvRect( 0, 0, sz.width, sz.height ));

    // down-scale and upscale the image to filter out the noise

    cvPyrDown( timg, pyr, 7 );

    cvPyrUp( pyr, timg, 7 );

    tgray = cvCreateImage( sz, 8, 1 );

    // find squares in every color plane of the image

    for( c = 0; c < 3; c++ )

    {

        // extract the c-th color plane

        cvSetImageCOI( timg, c+1 );

        cvCopy( timg, tgray, 0 );

        // try several threshold levels

        for( l = 0; l < N; l++ )

        {

            // hack: use Canny instead of zero threshold level.

            // Canny helps to catch squares with gradient shading

            if( l == 0 )

            {

                // apply Canny. Take the upper threshold from slider

                // and set the lower to 0 (which forces edges merging)

                cvCanny( tgray, gray, 0, thresh, 5 );

                // dilate canny output to remove potential

                // holes between edge segments

                cvDilate( gray, gray, 0, 1 );

            }

            else

            {

                // apply threshold if l!=0:

                //     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0

                cvThreshold( tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY );

            }

            // find contours and store them all as a list

            cvFindContours( gray, storage, &contours, sizeof(CvContour),

                CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );

            // test each contour

            while( contours )

            {

                // approximate contour with accuracy proportional

                // to the contour perimeter

                result = cvApproxPoly( contours, sizeof(CvContour), storage,

                    CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 );

                // square contours should have 4 vertices after approximation

                // relatively large area (to filter out noisy contours)

                // and be convex.

                // Note: absolute value of an area is used because

                // area may be positive or negative - in accordance with the

                // contour orientation

                if( result->total == 4 &&

                    cvContourArea(result,CV_WHOLE_SEQ,0) > 1000 &&

                    cvCheckContourConvexity(result) )

                {

                    s = 0;

                    for( i = 0; i < 5; i++ )

                    {

                        // find minimum angle between joint

                        // edges (maximum of cosine)

                        if( i >= 2 )

                        {

                            t = fabs(angle(

                            (CvPoint*)cvGetSeqElem( result, i ),

                            (CvPoint*)cvGetSeqElem( result, i-2 ),

                            (CvPoint*)cvGetSeqElem( result, i-1 )));

                            s = s > t ? s : t;

                        }

                    }

                    // if cosines of all angles are small

                    // (all angles are ~90 degree) then write quandrange

                    // vertices to resultant sequence

                    if( s < 0.3 )

                        for( i = 0; i < 4; i++ )

                            cvSeqPush( squares,

                                (CvPoint*)cvGetSeqElem( result, i ));

                }

                // take the next contour

                contours = contours->h_next;

            }

        }

    }

    // release all the temporary images

    cvReleaseImage( &gray );

    cvReleaseImage( &pyr );

    cvReleaseImage( &tgray );

    cvReleaseImage( &timg );

    return squares;

}

// the function draws all the squares in the image

void drawSquares( IplImage* img, CvSeq* squares )

{

    CvSeqReader reader;

    IplImage* cpy = cvCloneImage( img );

    int i;

    // initialize reader of the sequence

    cvStartReadSeq( squares, &reader, 0 );

    // read 4 sequence elements at a time (all vertices of a square)

    for( i = 0; i < squares->total; i += 4 )

    {

        CvPoint pt[4], *rect = pt;

        int count = 4;

        // read 4 vertices

        CV_READ_SEQ_ELEM( pt[0], reader );

        CV_READ_SEQ_ELEM( pt[1], reader );

        CV_READ_SEQ_ELEM( pt[2], reader );

        CV_READ_SEQ_ELEM( pt[3], reader );

        // draw the square as a closed polyline

        cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 );

    }

    // show the resultant image

    cvShowImage( wndname, cpy );

    cvReleaseImage( &cpy );

}

char* names[] = { "pic1.png", "pic2.png", "pic3.png",

                  "pic4.png", "pic5.png", "pic6.png", 0 };

int main(int argc, char** argv)

{

    int i, c;

    // create memory storage that will contain all the dynamic data

    storage = cvCreateMemStorage(0);

    for( i = 0; names[i] != 0; i++ )

    {

        // load i-th image

        img0 = cvLoadImage( names[i], 1 );

        if( !img0 )

        {

            printf("Couldn't load %s/n", names[i] );

            continue;

        }

        img = cvCloneImage( img0 );

        // create window and a trackbar (slider) with parent "image" and set callback

        // (the slider regulates upper threshold, passed to Canny edge detector)

        cvNamedWindow( wndname, 1 );

        // find and draw the squares

        drawSquares( img, findSquares4( img, storage ) );

        // wait for key.

        // Also the function cvWaitKey takes care of event processing

        c = cvWaitKey(0);

        // release both images

        cvReleaseImage( &img );

        cvReleaseImage( &img0 );

        // clear memory storage - reset free space position

        cvClearMemStorage( storage );

        if( (char)c == 27 )

            break;

    }

    cvDestroyWindow( wndname );

    return 0;

}

这几天研究了一下车牌字符分割的问题,前提是已经进行了车牌定位,角度校正等预处理。

用到的主要知识有:二值化,形态学操作,轮廓查找等。

字符分割网上资料比较少,本人接触opencv一段时间,自己瞎搞了一下,以此抛砖引玉,希望与各位交流一下。

以下为全部源代码:

//==============================================

//write by sing

//2010-10-10

//==============================================

#include "stdafx.h"

//找出含车牌文字的最左端

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;

}

int main(int argc, char* argv[])

{

    IplImage* imgSrc = cvLoadImage("cp.jpg", CV_LOAD_IMAGE_COLOR);

    IplImage* img_gray = cvCreateImage(cvGetSize(imgSrc), IPL_DEPTH_8U, 1);

  

    cvCvtColor(imgSrc, img_gray, CV_BGR2GRAY);

    cvThreshold(img_gray, img_gray, 100, 255, CV_THRESH_BINARY);

    //寻找最小区域,并截取

    CvRect rc = findArea(img_gray);

    cvSetImageROI(img_gray, rc);

    IplImage* img_gray2 = cvCreateImage(cvSize(rc.width, rc.height), IPL_DEPTH_8U, 1);

    cvCopyImage(img_gray, img_gray2);

    cvResetImageROI(img_gray);

    IplImage* imgSrc2 = cvCreateImage(cvSize(rc.width, rc.height), IPL_DEPTH_8U, 3);

    cvSetImageROI(imgSrc, rc);

    cvCopyImage(imgSrc, imgSrc2);

    cvResetImageROI(imgSrc);

    //形态学

    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);

        cvResetImageROI(imgSrc2);

      

        sprintf(szName, "wnd_%d", idx++);

        cvNamedWindow(szName);

        cvShowImage(szName, imgNo);

        cvReleaseImage(&imgNo);

    }

  

    cvNamedWindow("src");

    cvShowImage("src", imgSrc2);

    cvWaitKey(0);

    cvReleaseMemStorage(&storage);

    cvReleaseImage(&imgSrc);

    cvReleaseImage(&imgSrc2);

    cvReleaseImage(&img_gray);

    cvReleaseImage(&img_gray2);

    cvDestroyAllWindows();

    return 0;

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息