Opencv车牌识别之字符提取

来源:互联网 发布:mac os x 升级系统 编辑:程序博客网 时间:2024/05/22 04:27

#ifdef _CH_  
#pragma package <opencv>  
#endif  
 
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
#include <string.h>
#include <opencv/cv.h>
#include <stdio.h>


using namespace cv;  
using namespace std;
 




//找出含车牌文字的最左端
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[])
{
Mat img = imread("7.png .");
    imshow("Lena Original", img);


    IplImage* imgSrc = cvLoadImage("7.png", 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;
}




/*
    int main()
    {
    Mat img = imread("Car2.jpg");
    imshow("Lena Original", img);
    for (int row = 0; row < img.rows; row++)
    {
    for (int col = 0; col < img.cols; col++)
    {

    if(img.at<cv::Vec3b>(row,col) [2] > 128)
{
img.at<cv::Vec3b>(row,col) [0]=255;
img.at<cv::Vec3b>(row,col) [1]=255;
img.at<cv::Vec3b>(row,col) [2]=255;
}
    }
    }
    imshow("Lena Modified", img);
    cvWaitKey();
    return 0;
    }


*/








原创粉丝点击