关于汽车颜色识别
#1,首先說說我自己做的一個小程序
有參考CSDN中的一篇博客,具體文章鏈接找不到了。
主要思想:將RGB顏色空間中的圖像轉換到HSV空間,根據常見九種顏色的H、S、V范圍來判斷給定圖像中的汽車最大概率屬于哪種色系的車。
常見顏色的HSV范圍:(也是參考的網上的為主)
程序接口:【輸入】Mat類型,汽車圖片的patch;【輸出】string類型,上面九種顏色中的一種;
算法流程圖:
?
#2,存在的問題
可能原因:我個人覺得,HSV空間中的值與實際肉眼中的顏色并不是線性的映射關系,所以單純用一個線性范圍來給出一中顏色的HSV值是有問題的,實驗結果也驗證了我這個猜想。很多車子在進去檢測時都會有誤,如下給出幾種:
盡管代碼有問題,但還是在此貼出,以備之后改進:
/*********************************** -----------------------------------Name: sophia Date: 20161206 Email: hxinwen1218@sina.com----------------------------------- Function: Reco Identification the color of the cars.************************************/#include<iostream> #include<vector> #include<string> #include<conio.h> #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc.hpp>using namespace std; using namespace cv;string ImgClass(Mat image);/* number color 0 blue; 1 orange; 2 yellow; 3 green; 4 violet 5 red; 6 white; 7 grey; 8 black; 9 red; */int main(){while (1){ string img_name;cout << "Please enter the name of the image: " << endl;cin >> img_name;string imgN = img_name + ".jpg";Mat img = imread(imgN);string className;className=ImgClass(img);cout << "The class of the car is: " << className << endl;char ch;ch = _getch();if (ch == 27)break;elsecontinue;}system("Pause");return 0; }string ImgClass(Mat image){int classNum = 10;int lowH, highH, lowS, highS, lowV, highV;vector<double> ptRate(classNum, 0.0);string outClass;for (int ic = 0; ic < classNum; ic++){switch (ic){case 0://bluelowH = 100;highH = 124;lowS = 63;highS = 255;lowV = 76;highV = 220;break;case 1://orangelowH = 11;highH = 25;lowS = 43;highS = 255;lowV = 46;highV = 255;break;case 2://yellowlowH = 22;highH = 37;lowS = 43;highS = 255;lowV = 46;highV = 255;break;case 3:lowH = 35;highH = 77;lowS = 43;highS = 255;lowV = 46;highV = 255;break;case 4:lowH = 125;highH = 155;lowS = 43;highS = 255;lowV = 46;highV = 255;break;case 5:lowH = 0;highH = 10;lowS = 43;highS = 255;lowV = 46;highV = 255;break;case 6://whitelowH = 0;highH = 180;lowS = 0;highS = 25;lowV = 225;highV = 255;break;case 7://greylowH = 0;highH = 180;lowS = 28;highS = 40;lowV = 30;highV = 221;break;case 8://blacklowH = 0;highH = 180;lowS = 0;highS = 255;lowV = 0;highV = 30;break;case 9://redlowH = 156;highH = 180;lowS = 43;highS = 255;lowV = 46;highV = 255;break;}Mat imgHSV;vector<Mat> hsvSplit;cvtColor(image, imgHSV, COLOR_BGR2HSV);split(imgHSV, hsvSplit);equalizeHist(hsvSplit[2], hsvSplit[2]);merge(hsvSplit, imgHSV);Mat imgThresholded;inRange(imgHSV, Scalar(lowH, lowS, lowV), Scalar(highH, highS, highV), imgThresholded);int nonZeroNum = 0;vector<Mat> channelsImg;split(imgThresholded, channelsImg);Mat_<uchar> imgResult = channelsImg[0];for (int ia = 0; ia < imgResult.rows; ia++)for (int ib = 0; ib < imgResult.cols; ib++)if (imgResult(ia, ib) != 0)nonZeroNum++;double rateCac = (double)nonZeroNum / (double)(imgResult.rows*imgResult.cols);ptRate[ic] = rateCac;}double curRate = 0.0;int classN;for (int id = 0; id < ptRate.size(); id++){if (ptRate[id] > curRate){curRate = ptRate[id];classN = id;}}switch (classN){case 0:outClass = "blue";break;case 1:outClass = "orange";break;case 2:outClass = "yellow";break;case 3:outClass = "green";break;case 4:outClass = "violet";break;case 5:outClass = "red";break;case 6:outClass = "white";break;case 7:outClass = "grey";break;case 8:outClass = "black";break;case 9:outClass = "red";}return outClass; }?
如果您有更好的想法,請多多指教!
?
總結
- 上一篇: 十万小时理论
- 下一篇: 四年级下册英语计算机房和教师办公室的图片