OpenCV 利用hsv颜色空间的车牌底色检测
一个针对车牌颜色分类的小测试,根据hsv颜色空间,遍历像素点后计算在每个颜色下的像素个数,区分蓝,黄,黑,白车牌底色。
#include<windows.h>
#include<ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
char adr[128]={0};
int main()
{
Mat hsv;
Mat imgH ;
Mat imgS ;
Mat imgV ;
for (int i=57;i<=114;i++)
{
int bluenum=0;
int yellownum = 0;
int whitenum = 0;
int blacknum = 0;
int othernum = 0;
sprintf_s(adr, "C:\\Users\\Administrator\\Desktop\\save\\1 (%d).jpg",i);
printf("-------------------------------------------------------\n");
printf("当前图片为%d张\n",i);
Mat carplate= imread(adr);
cvtColor(carplate,hsv,CV_BGR2HSV);
vector<Mat> channels;
split(hsv, channels);
imgH = channels.at(0);
imgS = channels.at(1);
imgV = channels.at(2);
for (int i=0;i<hsv.rows;i++)
{
for(int j=0;j<hsv.cols;j++)
{
//h通道/-/
uchar hvalue=imgH.at<uchar>(i,j); //
//s通道/-/
uchar svalue=imgS.at<uchar>(i,j); //
//v通道/-/
uchar vvalue=imgV.at<uchar>(i,j); //
// printf("x=%d,y=%d,HSV: H=%d,S=%d,V=%d\n",i,j,hvalue,svalue,vvalue);
if ((hvalue>90&&hvalue<120)&&(svalue>80&&svalue<220)&&(vvalue>80&&vvalue<255))//hsv图像在蓝色区域里
{
//蓝色+1
bluenum++;
}
else if ((hvalue>11&&hvalue<34)&&(svalue>43&&svalue<255)&&(vvalue>46&&vvalue<255))//hsv在黄色区域里
{
//黄色+1
yellownum++;
}
else if ((hvalue>0&&hvalue<180)&&(svalue>0&&svalue<255)&&(vvalue>0&&vvalue<46))//hsv在黑色区域里
{
blacknum++;
//黑色+1
}
else if ((hvalue>0&&hvalue<180)&&(svalue>0&&svalue<30)&&(vvalue>46&&vvalue<220))//hsv在白色区域里
{
//白色+1
whitenum++;
}
else
{
//其他颜色
othernum++;
}
}
}
四个颜色值找最大值//
if ((bluenum>yellownum)&(bluenum>blacknum)&(bluenum>whitenum)&(bluenum>othernum))
printf("图%d底色为蓝色\n",i);
if ((yellownum>whitenum)&(yellownum>blacknum)&(yellownum>bluenum)&(yellownum>othernum))
printf("图%d底色为黄色\n",i);
if ((whitenum>yellownum)&(whitenum>blacknum)&(whitenum>bluenum)&(whitenum>othernum))
printf("图%d底色为白色\n",i);
if ((blacknum>yellownum)&(blacknum>whitenum)&(blacknum>bluenum)&(blacknum>othernum))
printf("图%d底色为黑色\n",i);
if ((othernum>yellownum)&(othernum>whitenum)&(othernum>bluenum)&(othernum>blacknum))
printf("图%d不是车牌\n",i);
}
getchar();
waitKey(0);
return 0;
}
测试结果如下:
测试素材:
还没有评论,来说两句吧...