java opencv 提取车牌_opencv 学习之 车牌提取

本文介绍了使用Java OpenCV进行车牌提取的步骤,包括图像载入、均值滤波、边缘检测、二值化、形态学变换和轮廓检测。通过这些操作,有效地识别并分离出车牌区域,为后续的字符识别做准备。
摘要由CSDN通过智能技术生成

车牌识别分两步,一是车牌提取,而是字符识别。

下面是车牌提取。

VS2010。

OpenCV249。

//载入图像

char * path = "d:\\picture\\06.jpg";

IplImage * frame = cvLoadImage(path);

if(!frame) return 0;

cvNamedWindow("frame", 1);

cvShowImage("frame", frame);

6cfcde03632003dd21aee3fd069e5a39.png

//均值滤波

cvSmooth(frame, frame, CV_MEDIAN);

//cvSmooth(frame, frame, CV_GAUSSIAN, 3, 3);

//灰度图

IplImage * gray = cvCreateImage(cvGetSize(frame), frame->depth, 1);

cvCvtColor(frame, gray, CV_BGR2GRAY);

cvNamedWindow("gray", 1);

cvShowImage("gray", gray);

131e9b409b5e63dc475bb122f40bc551.png//边缘检测

IplImage * temp = cvCreateImage(cvGetSize(gray), IPL_DEPTH_16S,1);

//x方向梯度,垂直边缘

cvSobel(gray, temp, 2, 0, 3);

IplImage * sobel = cvCreateImage(cvGetSize(temp), IPL_DEPTH_8U,1);

cvConvertScale(temp, sobel, 1, 0);

cvNamedWindow("sobel", 1);

cvShowImage("sobel", sobel);

e507ce357f86894c10a4f1da421b695c.png

//二值化

IplImage * threshold = cvCreateImage(cvGetSize(sobel), gray->depth, 1);

cvThreshold(sobel, threshold, 0, 255, CV_THRESH_BINARY|CV_THRESH_OTSU);

cvNamedWindow("threshold", 1);

cvShowImage("threshold", threshold);

9f3cee9f3fc2484c09595b51865e8082.png

//形态学变化

IplConvKernel * kernal;

IplImage * morph = cvCreateImage(cvGetSize(threshold), threshold->depth, 1);

//自定义 1x3 的核进行 x 方向的膨胀腐蚀

kernal = cvCreateStructuringElementEx(3, 1, 1, 0, CV_SHAPE_RECT);

cvDilate(threshold, morph, kernal, 2); //x 膨胀联通数字

cvErode(morph, morph, kernal, 4); //x 腐蚀去除碎片

cvDilate(morph, morph, kernal, 4); //x 膨胀回复形态

//自定义 3x1 的核进行 y 方向的膨胀腐蚀

kernal = cvCreateStructuringElementEx(1, 3, 0, 1, CV_SHAPE_RECT);

cvErode(morph, morph, kernal, 1); //y 腐蚀去除碎片

cvDilate(morph, morph, kernal, 3); //y 膨胀回复形态

cvNamedWindow("erode", 1);

cvShowImage("erode", morph);

0503de8bbf135273d16bc3f6d666d453.png

//轮廓检测

IplImage * frame_draw = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);

cvCopy(frame, frame_draw);

CvMemStorage * storage = cvCreateMemStorage(0);

CvSeq * contour = 0;

int count = cvFindContours(morph, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

CvSeq * _contour = contour;

for( ; contour != 0; contour = contour->h_next )

{

double tmparea = fabs(cvContourArea(contour));

CvRect aRect = cvBoundingRect( contour, 0 );

if(tmparea > ((frame->height*frame->width)/10))

{

cvSeqRemove(contour,0); //删除面积小于设定值的轮廓,1/10

continue;

}

if (aRect.width < (aRect.height*2))

{

cvSeqRemove(contour,0); //删除宽高比例小于设定值的轮廓

continue;

}

if ((aRect.width/aRect.height) > 4 )

{

cvSeqRemove(contour,0); //删除宽高比例小于设定值的轮廓

continue;

}

if((aRect.height * aRect.width) < ((frame->height * frame->width)/100))

{

cvSeqRemove(contour,0); //删除宽高比例小于设定值的轮廓

continue;

}

CvScalar color = CV_RGB( 255, 0, 0);

cvDrawContours(frame_draw, contour, color, color, 0, 1, 8 );//绘制外部和内部的轮廓

}

cvNamedWindow("轮廓", 1);

cvShowImage("轮廓", frame_draw);

165dc509453efc7a3be00747e7024abf.png

下面就是要字符分割与识别了吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值