<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了OpenCV3實現車牌識別的具體程式碼,供大家參考,具體內容如下
車牌識別(基於OpenCV3.4.7+VS2017)
視訊識別
藍色車牌識別
視覺入坑的第一個Demo(註釋很詳細),因為本人之前拖延,一直沒能寫詳細實現部落格,先將程式碼貼出來供大家交流,個人認為精華部分在字元切割(直接用指標遍歷畫素加限制條件切割),車牌模板已上傳,整個工程也已上傳,後續完善各環節實現步驟詳解。
標頭檔案:Global.h
#ifdef GLOBAL extern int flag_1; extern bool flag; extern bool specialFlag; extern int captureRead extern string carPlate; extern char test[10]; extern struct stu1 { char number; Mat image; double matchDegree; }; extern struct stu { Mat image; double matchDegree; }; #endif
唯一的.cpp檔案:PlateIdentify.cpp(說實話,這Demo挺 “C” 的)
#include <opencv2/opencv.hpp> #include<opencv2/imgproc/imgproc.hpp> #include<opencv2/highgui/highgui.hpp> #include"Global.h" #include <windows.h> #include <string> using namespace std; using namespace cv; void fillHole(const Mat srcBw, Mat &dstBw); //填補演演算法 Mat cutOne(Mat cutImage); //邊框切割演演算法 void CharCut(Mat srcImage); //單個字元切割演演算法 Mat Location(Mat srcImage); //影象識別演演算法 void SingleCharCut(Mat doubleImage, int k1, int k2); void ShowChar(); void MatchProvince(); void MatchNumber(); void readProvince(); void readNumber(); void VideoShow(Mat videoImage); void GetStringSize(HDC hDC, const char* str, int* w, int* h); void putTextZH(Mat &dst, const char* str, Point org, Scalar color, int fontSize, const char* fn, bool italic, bool underline); int flag_1; //判斷是否傾斜,需不需要二次定位車牌 bool flag; //判斷提取是否成功 bool specialFlag = false; //針對巢狀車牌 int captureRead = 0; int videoFlag = 0; string carPlateProvince = " "; string carPlate = " "; char test[10]; vector<Mat> singleChar; //字元圖片容器 int main(int argc, char *argv[]) { //計時開始 double time0 = static_cast<double>(getTickCount()); //視訊操作 VideoCapture capture("1.mp4"); Mat srcImage; Mat theFirst; int singleCharLength; //讀取字元圖片 readProvince(); readNumber(); while (1) { capture >> srcImage; try { if (!srcImage.data) { printf("視訊識別結束 n"); return 0; } if (srcImage.rows >= srcImage.cols) { resize(srcImage, srcImage, Size(640, 640 * srcImage.rows / srcImage.cols)); } else { resize(srcImage, srcImage, Size(400 * srcImage.cols / srcImage.rows, 400)); } //車牌定位 theFirst = Location(srcImage); if (flag) { if (flag_1 == 1) //旋轉後要再次定位去上下雜邊 { theFirst = Location(theFirst); flag_1 = 0; } } if (flag) { //車牌切割(切割上下邊,去除干擾) theFirst = cutOne(theFirst); //單個字元切割 CharCut(theFirst); singleCharLength = singleChar.size(); printf("採取字元輪廓數 %dn", singleCharLength); ShowChar(); if (singleCharLength >= 7) { MatchProvince(); MatchNumber(); } singleChar.clear(); } } catch (Exception e) { cout << "Standard ecxeption : " << e.what() << " n" << endl; } if (waitKey(30) >= 0) break; //延時30ms } //imwrite("match\xxxxxx.bmp", singleChar[2]); time0 = ((double)getTickCount() - time0) / getTickFrequency(); cout << "執行時間" << time0 << "秒" << endl; waitKey(0); } void fillHole(const Mat srcBw, Mat &dstBw) { Size imageSize = srcBw.size(); Mat Temp = Mat::zeros(imageSize.height + 2, imageSize.width + 2, srcBw.type());//延展影象 srcBw.copyTo(Temp(Range(1, imageSize.height + 1), Range(1, imageSize.width + 1))); cv::floodFill(Temp, Point(0, 0), Scalar(255)); Mat cutImg;//裁剪延展的影象 Temp(Range(1, imageSize.height + 1), Range(1, imageSize.width + 1)).copyTo(cutImg); dstBw = srcBw | (~cutImg); } Mat Location(Mat srcImage) { //判斷變數重賦值 flag = false; //用於旋轉車牌 int imageWidth, imageHeight; //輸入影象的長和寬 imageWidth = srcImage.rows; //獲取圖片的寬 imageHeight = srcImage.cols; //獲取影象的長 //!!!!!!!!!!!!!!!!!!! Mat blueROI = srcImage.clone(); cvtColor(blueROI, blueROI, CV_BGR2HSV); //namedWindow("hsv圖"); //imshow("hsv圖", blueROI); //中值濾波操作 medianBlur(blueROI, blueROI, 3); //namedWindow("medianBlur圖"); //imshow("medianBlur圖", blueROI); //將藍色區域二值化 inRange(blueROI, Scalar(100, 130, 50), Scalar(124, 255, 255), blueROI); //namedWindow("blue圖"); //imshow("blue圖", blueROI); Mat element1 = getStructuringElement(MORPH_RECT, Size(2, 2)); //size()對速度有影響 morphologyEx(blueROI, blueROI, MORPH_OPEN, element1); //namedWindow("0次K運算後影象"); //imshow("0次K運算後影象", blueROI); Mat element0 = getStructuringElement(MORPH_ELLIPSE, Size(10, 10)); //size()對速度有影響 morphologyEx(blueROI, blueROI, MORPH_CLOSE, element0); //namedWindow("0次閉運算後影象"); //imshow("0次閉運算後影象", blueROI); vector<vector<Point>> contours; findContours(blueROI, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); int cnt = contours.size(); cout << "number of contours " << cnt << endl; //列印輪廓個數 if (cnt == 0) { if (!flag) //在視訊中顯示 { cout << "圖中無車牌 " << endl; //namedWindow("提取車牌結果圖"); //imshow("提取車牌結果圖", srcImage); //顯示最終結果圖 VideoShow(srcImage); return srcImage; } } double area; double longside, temp, shortside, long2short; float angle = 0; Rect rect; RotatedRect box; //可旋轉的矩形盒子 Point2f vertex[4]; //四個頂點 Mat image = srcImage.clone(); //為後來顯示做準備 Mat rgbCutImg; //車牌裁剪圖 //box.points(vertex); //獲取矩形四個頂點座標 //length=arcLength(contour[i]); //獲取輪廓周長 //area=contourArea(contour[i]); //獲取輪廓面積 //angle=box.angle; //得到車牌傾斜角度 for (int i = 0; i < cnt; i++) { area = contourArea(contours[i]); //獲取輪廓面積 if (area > 600 && area < 15000) //矩形區域面積大小判斷 { rect = boundingRect(contours[i]); //計算矩形邊界 box = minAreaRect(contours[i]); //獲取輪廓的矩形 box.points(vertex); //獲取矩形四個頂點座標 angle = box.angle; //得到車牌傾斜角度 longside = sqrt(pow(vertex[1].x - vertex[0].x, 2) + pow(vertex[1].y - vertex[0].y, 2)); shortside = sqrt(pow(vertex[2].x - vertex[1].x, 2) + pow(vertex[2].y - vertex[1].y, 2)); if (shortside > longside) //短軸大於長軸,交換資料 { temp = longside; longside = shortside; shortside = temp; cout << "交換" << endl; } else angle += 90; long2short = longside / shortside; if (long2short > 1.5 && long2short < 4.5) { flag = true; for (int i = 0; i < 4; ++i) //劃線框出車牌區域 line(image, vertex[i], vertex[((i + 1) % 4) ? (i + 1) : 0], Scalar(0, 255, 0), 1, CV_AA); if (!flag_1) //在視訊中顯示 { printf("提取成功n"); /*namedWindow("提取車牌結果圖"); imshow("提取車牌結果圖", image); */ //顯示最終結果圖 VideoShow(image); } rgbCutImg = srcImage(rect); //namedWindow("車牌圖"); //imshow("車牌圖", rgbCutImg);//裁剪出車牌 break; //退出迴圈,以免容器中變數變換 } } } cout << "傾斜角度:" << angle << endl; if (flag && fabs(angle) > 0.8) //車牌過偏,轉一下 偏移角度小時可不呼叫,後續找到合適範圍再改進 { flag_1 = 1; Mat RotractImg(imageWidth, imageHeight, CV_8UC1, Scalar(0, 0, 0)); //傾斜矯正圖片 Point2f center = box.center; //獲取車牌中心座標 Mat M2 = getRotationMatrix2D(center, angle, 1); //計算旋轉加縮放的變換矩陣 warpAffine(srcImage, RotractImg, M2, srcImage.size(), 1, 0, Scalar(0)); //進行傾斜矯正 //namedWindow("傾斜矯正後圖片",0); //imshow("傾斜矯正後圖片", RotractImg); rgbCutImg = RotractImg(rect); //擷取車牌彩色照片 //namedWindow("矯正後車牌照"); //imshow("矯正後車牌照", rgbCutImg); /*cout << "矩形中心:" << box.center.x << "," << box.center.y << endl;*/ return rgbCutImg; } if (flag == false) { printf("提取失敗n"); //後期加邊緣檢測法識別 if (!flag_1) //在視訊中顯示 { /*namedWindow("提取車牌結果圖"); imshow("提取車牌結果圖", image); */ //顯示最終結果圖 VideoShow(image); } } return rgbCutImg; } Mat cutOne(Mat cutImage) { //列印車牌長寬 try { /*cout << " cutImage.rows : " << cutImage.rows << endl; cout << " cutImage.cols : " << cutImage.cols << endl;*/ if(cutImage.rows >= cutImage.cols) resize(cutImage, cutImage, Size(320, 320 * cutImage.rows / cutImage.cols)); } catch (Exception e) { resize(cutImage, cutImage, Size(320, 100)); } /*namedWindow("Resize車牌圖"); imshow("Resize車牌圖", cutImage);*/ int height = cutImage.rows; cout << "tHeight:" << height << "tWidth:" << 320 << endl; if (height < 86) { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!處理新型巢狀車牌!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! printf("巢狀車牌n"); specialFlag = true; } Mat whiteROI = cutImage.clone(); if (specialFlag) { cvtColor(whiteROI, whiteROI, CV_BGR2HSV); //將白色區域二值化 //inRange(whiteROI, Scalar(0, 0, 0), Scalar(130, 50, 245), whiteROI); //增大 S 即飽和度可以使hsv白色檢測範圍更大 inRange(whiteROI, Scalar(0, 0, 0), Scalar(180, 100, 245), whiteROI); //namedWindow("specialFlagwhiteROI圖"); //imshow("specialFlagwhiteROI圖", whiteROI); } else { GaussianBlur(whiteROI, whiteROI, Size(3, 3), 0, 0); /*namedWindow("GaussianBlur車牌圖"); imshow("GaussianBlur車牌圖", whiteROI);*/ cvtColor(whiteROI, whiteROI, CV_BGR2HSV); //medianBlur(whiteROI, whiteROI, 3); //namedWindow("Src_medianBlur圖"); //imshow("Src_medianBlur圖", whiteROI); //將白色區域二值化 //inRange(whiteROI, Scalar(0, 0, 10), Scalar(180, 30, 255), whiteROI); //增大 S 即飽和度可以使hsv白色檢測範圍更大 inRange(whiteROI, Scalar(0, 0, 10), Scalar(180, 120, 255), whiteROI); //namedWindow("whiteROI圖"); //imshow("whiteROI圖", whiteROI); } /* Mat element0 = getStructuringElement(MORPH_ELLIPSE, Size(4, 4)); //size()對速度有影響 morphologyEx(whiteROI, whiteROI, MORPH_OPEN, element0); namedWindow("OPEN圖"); imshow("OPEN圖", whiteROI); */ Mat dstImage = cutImage.clone(); vector<vector<Point>> contours; findContours(whiteROI, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); drawContours(dstImage, contours, -1, Scalar(0, 0, 255), 1); //namedWindow("疑似字元輪廓識別圖"); //imshow("疑似字元輪廓識別圖", dstImage); inRange(dstImage, Scalar(0, 0, 255), Scalar(0, 0, 255), dstImage); //namedWindow("字元大輪廓圖"); //imshow("字元大輪廓圖", dstImage); /*fillHole(dstImage, dstImage); namedWindow("填補輪廓後圖"); imshow("填補輪廓後圖", dstImage);*/ int row1 = 2; int row2 = dstImage.rows; int rowMax = dstImage.rows - 1; //開區間,防止越界 int colMax = dstImage.cols - 1; //開區間,防止越界 int addFirst = 10; int addFirst0 = 0; int addFirst1 = 0; int addFirst2 = 0; //測中間畫素 //dstImage.at<uchar>(rowMax-1, colMax-1); //cout << "Width:" << j << endl; int addFirstTemp = addFirst; //第一次用時已經改變數值,容易忽略!!!!! uchar* data; //裁剪上下邊。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 //上邊 for (int i = 2; i < rowMax / 3; i++, addFirst1 = 0) // 6 剛剛好 { data = dstImage.ptr<uchar>(i); for (int j = 2; j < colMax; j++) { if (data[j] == 255) { addFirst1++; } } if (addFirst1 < addFirst) //篩選最小值所在行 { row1 = i; addFirst = addFirst1 + 3; //cout << "行頭" << row1 << endl; //flag_x = 1; } } //下邊 for (int i = rowMax - 2; i > rowMax - rowMax / 4; i--, addFirst2 = 0) // 6 剛剛好 { data = dstImage.ptr<uchar>(i); for (int j = 2; j < colMax; j++) { if (data[j] == 255) { addFirst2++; } } if (addFirst2 < addFirstTemp) //篩選最小值所在行 { row2 = i; addFirstTemp = addFirst2 + 3; //cout << "行底" << row2 << endl; //flag_y = 1; } } int orow; orow = row2 - row1; Mat w_image; Mat rgb_w_image; w_image = dstImage(Rect(0, row1, colMax, orow)); rgb_w_image = cutImage(Rect(0, row1, colMax, orow)); //namedWindow("裁剪上下圖"); //imshow("裁剪上下圖", w_image); int rowMax_ALT = w_image.rows - 1; //開區間,防止越界(注意,裁剪完上下後要重新寫行和寬,因為行和寬已經改變) int colMax_ALT = w_image.cols - 1; //開區間,防止越界(注意,裁剪完上下後要重新寫行和寬,因為行和寬已經改變) int col_1 = 2; int col_2 = w_image.cols; int add = 2; int add1 = 0; int add2 = 0; int addTemp = add; //第一次用時已經改變數值,容易忽略!!!!! //裁剪左右邊。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 //左邊 //for (int i = 0; i < colMax_ALT / 18; i++, add1 = 0) // 剛剛好 //{ // for (int j = 2; j < rowMax_ALT; j++) // { // data = dstImage.ptr<uchar>(j); // if (data[i] == 255) // { // add1++; // } // } // if (add1 < add) //篩選最小值所在列 // { // col_1 = i; // add = add1 + 1; // } //} //右邊 if (specialFlag) { for (int i = colMax_ALT; i > colMax_ALT - colMax_ALT / 18; i--, add2 = 0) // 剛剛好 { for (int j = 2; j < rowMax_ALT; j++) { data = dstImage.ptr<uchar>(j); if (data[i] == 255) { add2++; } } if (add2 < addTemp) //篩選最小值所在列 { col_2 = i; addTemp = add2 + 1; //cout << "行底" << row2 << endl; } } } int o_col; o_col = col_2 - col_1; Mat H_image; H_image = w_image(Rect(col_1, 0, o_col, rowMax_ALT)); rgb_w_image = rgb_w_image(Rect(col_1, 0, o_col, rowMax_ALT)); //namedWindow("再裁剪左右圖"); //imshow("再裁剪左右圖", H_image); //namedWindow("裁剪後彩圖"); //imshow("裁剪後彩圖", rgb_w_image); return rgb_w_image; } void CharCut(Mat srcImage) { resize(srcImage, srcImage, Size(320, 320 * srcImage.rows / srcImage.cols)); //namedWindow("Resize車牌圖"); //imshow("Resize車牌圖", srcImage); GaussianBlur(srcImage, srcImage, Size(3, 3), 0, 0); /*namedWindow("GaussianBlur車牌圖"); imshow("GaussianBlur車牌圖", srcImage); */ medianBlur(srcImage, srcImage, 3); //namedWindow("Src_medianBlur圖"); //imshow("Src_medianBlur圖", srcImage); cvtColor(srcImage, srcImage, CV_BGR2HSV); //將白色區域二值化 Mat doubleImage; //inRange(srcImage, Scalar(0, 0, 10), Scalar(180, 75, 255), doubleImage); //增大 S 即飽和度可以使hsv白色檢測範圍更大 inRange(srcImage, Scalar(0, 0, 0), Scalar(180, 125, 245), doubleImage); namedWindow("doubleImage圖"); imshow("doubleImage圖", doubleImage); int colTemp = 0; int rowMax = doubleImage.rows; int colMax = doubleImage.cols; int addFirst = 0; int add = 0; int k1 = 0; int k2; int kTemp = 0; int times = 0; int oneCutEnd = 0; float t = 1.0; uchar* data; cout << "Test: " << specialFlag << endl; //針對巢狀車牌處理 if (specialFlag) { for (int i = 2; i < colMax; i++, addFirst = 0, add = 0) { for (int j = rowMax / 10.8; j < rowMax - rowMax / (10.8*t); j++) { data = doubleImage.ptr<uchar>(j); if (data[i - 1] == 255) { addFirst++; //統計前一列 } } for (int j = rowMax / 10.8; j < rowMax - rowMax / (10.8*t); j++) { data = doubleImage.ptr<uchar>(j); if (data[i] == 255) { add++; //統計後一列 } } //省份字元分開切割 if (!times) { if (!oneCutEnd && (!addFirst && add)) k1 = i - 1; if (addFirst && !add) { k2 = i; oneCutEnd = 1; if (k2 - k1 > colMax / 11.25) { times = 1; if (k2 - k1 < colMax / 5.625) SingleCharCut(doubleImage, k1, k2); else i = 2; } } } //切割其他字元 else { if (!addFirst && add) k1 = i - 1; if (addFirst && !add) { k2 = i; if (k2 - k1 > colMax / 32) { if (k2 - k1 < colMax / 5.625) SingleCharCut(doubleImage, k1, k2); else //針對巢狀車牌下部連線過靠上 { i = k1; t -= 0.1; } } else { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!處理中間分割點與‘ 1 '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! for (int a = k1; a <= k2; a++) { data = doubleImage.ptr<uchar>(rowMax / 5); if (data[a] == 255) kTemp++; } if (kTemp > 0) SingleCharCut(doubleImage, k1, k2); kTemp = 0; } } } } k2 = colMax; if (k2 - k1 > colMax / 32) SingleCharCut(doubleImage, k1, k2); specialFlag = false; } else { for (int i = 2; i < colMax; i++, addFirst = 0, add = 0) { for (int j = rowMax / 12.8; j < rowMax - rowMax / 12.8; j++) { data = doubleImage.ptr<uchar>(j); if (data[i - 1] == 255) { addFirst++; } } for (int j = rowMax / 12.8; j < rowMax - rowMax / 12.8; j++) { data = doubleImage.ptr<uchar>(j); if (data[i] == 255) { add++; } } if (!times) { if (!oneCutEnd && (!addFirst && add)) k1 = i - 1; if (addFirst && !add) { k2 = i; oneCutEnd = 1; if (k2 - k1 > colMax / 11.25) { times = 1; if (k2 - k1 < colMax / 5.625) SingleCharCut(doubleImage, k1, k2); else i = 2; } } } else { if (!addFirst && add) k1 = i - 1; if (addFirst && !add) { k2 = i; if (k2 - k1 > colMax / 32) SingleCharCut(doubleImage, k1, k2); else { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!處理中間分割點與‘ 1 '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! for (int a = k1; a <= k2; a++) { data = doubleImage.ptr<uchar>(rowMax / 5); if (data[a] == 255) kTemp++; } if (kTemp > 0) SingleCharCut(doubleImage, k1, k2); kTemp = 0; } } } } } } void SingleCharCut(Mat doubleImage, int k1, int k2) { //printf("k1 = %d ,k2 = %dn", k1, k2); int rowMax = doubleImage.rows; Mat image; image = doubleImage(Rect(k1, 0, k2 - k1, rowMax)); int row1 = 0; int row2 = image.rows; rowMax = image.rows - 1; //開區間,防止越界 int colMax = image.cols; //開區間,防止越界 int addFirst = 2; int addFirst1 = 0; int addFirst2 = 0; uchar* data; //測中間畫素 //image.at<uchar>(rowMax-1, colMax-1); //cout << "Width:" << j << endl; int addFirstTemp = addFirst; //第一次用時已經改變數值,容易忽略!!!!! //裁剪上下邊。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 //上邊 for (int i = 0; i < rowMax / 4; i++, addFirst1 = 0) // 6 剛剛好 { data = image.ptr<uchar>(i); for (int j = 0; j < colMax; j++) { if (data[j] == 255) { addFirst1++; } } if (addFirst1 < addFirst) //篩選最小值所在行 { row1 = i; addFirst = addFirst1 + 1; } } //下邊 for (int i = rowMax; i > rowMax - rowMax / 4; i--, addFirst2 = 0) // 6 剛剛好 { data = image.ptr<uchar>(i); for (int j = 2; j < colMax; j++) { if (data[j] == 255) { addFirst2++; } } if (addFirst2 < addFirstTemp) //篩選最小值所在行 { row2 = i; addFirstTemp = addFirst2 + 1; } } int orow; orow = row2 - row1; Mat w_image; w_image = image(Rect(0, row1, colMax, orow)); singleChar.push_back(w_image); } void ShowChar() { int length = singleChar.size(); for (int i = 0; i < length; i++) { resize(singleChar[i], singleChar[i], Size(20, 40)); //字元影象歸一化 //namedWindow(to_string(i) + "圖"); //imshow(to_string(i) + "圖", singleChar[i]); } } //讀取省份模板 struct stu { Mat image; double matchDegree; }; struct stu first[35]; void readProvince() { int i = 0; //讀取字元 { first[i].image = imread("match\zw1.bmp", 0); i++; first[i].image = imread("match\zw2.bmp", 0); i++; first[i].image = imread("match\zw3.bmp", 0); i++; first[i].image = imread("match\zw4.bmp", 0); i++; first[i].image = imread("match\zw5.bmp", 0); i++; first[i].image = imread("match\zw6.bmp", 0); i++; first[i].image = imread("match\zw7.bmp", 0); i++; first[i].image = imread("match\zw8.bmp", 0); i++; first[i].image = imread("match\zw9.bmp", 0); i++; first[i].image = imread("match\zw10.bmp", 0); i++; first[i].image = imread("match\zw11.bmp", 0); i++; first[i].image = imread("match\zw12.bmp", 0); i++; first[i].image = imread("match\zw13.bmp", 0); i++; first[i].image = imread("match\zw14.bmp", 0); i++; first[i].image = imread("match\zw15.bmp", 0); i++; first[i].image = imread("match\zw16.bmp", 0); i++; first[i].image = imread("match\zw17.bmp", 0); i++; first[i].image = imread("match\zw18.bmp", 0); i++; first[i].image = imread("match\zw19.bmp", 0); i++; first[i].image = imread("match\zw20.bmp", 0); i++; first[i].image = imread("match\zw21.bmp", 0); i++; first[i].image = imread("match\zw22.bmp", 0); i++; first[i].image = imread("match\zw23.bmp", 0); i++; first[i].image = imread("match\zw24.bmp", 0); i++; first[i].image = imread("match\zw25.bmp", 0); i++; first[i].image = imread("match\zw26.bmp", 0); i++; first[i].image = imread("match\zw27.bmp", 0); i++; first[i].image = imread("match\zw28.bmp", 0); i++; first[i].image = imread("match\zw29.bmp", 0); i++; first[i].image = imread("match\zw30.bmp", 0); i++; first[i].image = imread("match\zw31.bmp", 0); i++; first[i].image = imread("match\zw32.bmp", 0); i++; first[i].image = imread("match\zw33.bmp", 0); i++; first[i].image = imread("match\zw34.bmp", 0); i++; first[i].image = imread("match\zw35.bmp", 0); } } //識別省份字元 void MatchProvince() { int rowMax = 40; int colMax = 20; int add = 0; int addTemp = 0; Mat absCutImage; double temp; int index = 0; uchar* data; for (int i = 0; i < rowMax; i++) { data = singleChar[0].ptr<uchar>(i); for (int j = 0; j < colMax; j++) { if (data[j] == 255) { add++; } } } int firstLength = end(first) - begin(first); //printf("陣列長度1 %dn",firstLength); for (int x = 0; x < firstLength; x++, addTemp = 0) { absCutImage = abs(first[x].image - singleChar[0]); for (int i = 0; i < rowMax; i++) { data = absCutImage.ptr<uchar>(i); for (int j = 0; j < colMax; j++) { if (data[j] == 255) { addTemp++; } } } temp = 1.0 - 1.0*addTemp / add; if (temp <= 1) first[x].matchDegree = temp; else first[x].matchDegree = 0; if (x > 0 && first[x].matchDegree > first[index].matchDegree) index = x; } /*absCutImage = abs(singleChar[0] - first[index].image); namedWindow("省份圖片相減後圖" + to_string(0)); imshow("省份圖片相減後圖" + to_string(0), absCutImage);*/ printf("省份字元最大匹配度: %lfn", first[index].matchDegree); switch (index) { case 0: printf("藏"); carPlateProvince += "藏"; break; case 1: printf("川"); carPlateProvince += "川"; break; case 2: printf("鄂"); carPlateProvince += "鄂"; break; case 3: printf("甘"); carPlateProvince += "甘"; break; case 4: printf("贛"); carPlateProvince += "贛"; break; case 5: printf("貴"); carPlateProvince += "貴"; break; case 6: printf("桂"); carPlateProvince += "桂"; break; case 7: printf("黑"); carPlateProvince += "黑"; break; case 8: printf("瀘"); carPlateProvince += "瀘"; break; case 9: printf("吉"); carPlateProvince += "吉"; break; case 10: printf("翼"); carPlateProvince += "翼"; break; case 11: printf("津"); carPlateProvince += "津"; break; case 12: printf("晉"); carPlateProvince += "晉"; break; case 13: printf("京"); carPlateProvince += "京"; break; case 14: printf("遼"); carPlateProvince += "遼"; break; case 15: printf("魯"); carPlateProvince += "魯"; break; case 16: printf("蒙"); carPlateProvince += "蒙"; break; case 17: printf("閩"); carPlateProvince += "閩"; break; case 18: printf("寧"); carPlateProvince += "寧"; break; case 19: printf("青"); carPlateProvince += "青"; break; case 20: printf("瓊"); carPlateProvince += "瓊"; break; case 21: printf("陝"); carPlateProvince += "陝"; break; case 22: printf("蘇"); carPlateProvince += "蘇"; break; case 23: printf("皖"); carPlateProvince += "皖"; break; case 24: printf("湘"); carPlateProvince += "湘"; break; case 25: printf("新"); carPlateProvince += "新"; break; case 26: printf("渝"); carPlateProvince += "渝"; break; case 27: printf("豫"); carPlateProvince += "豫"; break; case 28: printf("粵"); carPlateProvince += "粵"; break; case 29: printf("雲"); carPlateProvince += "雲"; break; case 30: printf("浙"); carPlateProvince += "浙"; break; case 31: printf("湘"); carPlateProvince += "湘"; break; case 32: printf("湘"); carPlateProvince += "湘"; break; case 33: printf("魯"); carPlateProvince += "魯"; break; case 34: printf("粵"); carPlateProvince += "粵"; break; } printf("n"); } //讀取字母和數位模板 struct stu1 { char number; Mat image; double matchDegree; }; struct stu1 second[49]; void readNumber() { for (int i = 0; i < 10; i++) { second[i].image = imread("match\" + to_string(i) + ".bmp", 0); second[i].number = 48 + i; } //讀取字元 { int i = 10; second[i].image = imread("match\6a.bmp", 0); second[i].number = '6'; i++; second[i].image = imread("match\3a.bmp", 0); second[i].number = '3'; i++; second[i].image = imread("match\P1.bmp", 0); second[i].number = 'P'; i++; second[i].image = imread("match\8b.bmp", 0); second[i].number = '8'; i++; second[i].image = imread("match\K1.bmp", 0); second[i].number = 'K'; i++; second[i].image = imread("match\9a.bmp", 0); second[i].number = '9'; i++; second[i].image = imread("match\B2.bmp", 0); second[i].number = 'B'; i++; second[i].image = imread("match\G1.bmp", 0); second[i].number = 'G'; i++; second[i].image = imread("match\T1.bmp", 0); second[i].number = 'T'; i++; second[i].image = imread("match\B1.bmp", 0); second[i].number = 'B'; i++; second[i].image = imread("match\8a.bmp", 0); second[i].number = '8'; i++; second[i].image = imread("match\1a.bmp", 0); second[i].number = '1'; i++; second[i].image = imread("match\7a.bmp", 0); second[i].number = '7'; i++; second[i].image = imread("match\D1.bmp", 0); second[i].number = 'D'; i++; second[i].image = imread("match\0a.bmp", 0); second[i].number = '0'; i++; second[i].image = imread("match\A.bmp", 0); second[i].number = 'A'; i++; second[i].image = imread("match\B.bmp", 0); second[i].number = 'B'; i++; second[i].image = imread("match\C.bmp", 0); second[i].number = 'C'; i++; second[i].image = imread("match\D.bmp", 0); second[i].number = 'D'; i++; second[i].image = imread("match\E.bmp", 0); second[i].number = 'E'; i++; second[i].image = imread("match\F.bmp", 0); second[i].number = 'F'; i++; second[i].image = imread("match\G.bmp", 0); second[i].number = 'G'; i++; second[i].image = imread("match\H.bmp", 0); second[i].number = 'H'; i++; second[i].image = imread("match\J.bmp", 0); second[i].number = 'J'; i++; second[i].image = imread("match\K.bmp", 0); second[i].number = 'K'; i++; second[i].image = imread("match\L.bmp", 0); second[i].number = 'L'; i++; second[i].image = imread("match\M.bmp", 0); second[i].number = 'M'; i++; second[i].image = imread("match\N.bmp", 0); second[i].number = 'N'; i++; second[i].image = imread("match\P.bmp", 0); second[i].number = 'P'; i++; second[i].image = imread("match\Q.bmp", 0); second[i].number = 'Q'; i++; second[i].image = imread("match\R.bmp", 0); second[i].number = 'R'; i++; second[i].image = imread("match\S.bmp", 0); second[i].number = 'S'; i++; second[i].image = imread("match\T.bmp", 0); second[i].number = 'T'; i++; second[i].image = imread("match\U.bmp", 0); second[i].number = 'U'; i++; second[i].image = imread("match\V.bmp", 0); second[i].number = 'V'; i++; second[i].image = imread("match\W.bmp", 0); second[i].number = 'W'; i++; second[i].image = imread("match\X.bmp", 0); second[i].number = 'X'; i++; second[i].image = imread("match\Y.bmp", 0); second[i].number = 'Y'; i++; second[i].image = imread("match\Z.bmp", 0); second[i].number = 'Z'; } } //識別其他字元 void MatchNumber() { int rowMax = 40; int colMax = 20; int add = 0; int addTemp = 0; Mat absCutImage; double temp; int index = 0; int length = singleChar.size(); int secondLength = end(second) - begin(second); //printf("陣列長度2 %d n", secondLength); uchar* data; int q = 0; for (int y = 1; y < length; y++, add = 0, index = 0) { if (y > 6) //防止多讀 break; //統計要讀取字元的白色畫素總值 for (int i = 0; i < rowMax; i++) { data = singleChar[y].ptr<uchar>(i); for (int j = 0; j < colMax; j++) { if (data[j] == 255) { add++; } } } //逐個字元識別 for (int x = 0; x < secondLength; x++, addTemp = 0) { absCutImage = abs(singleChar[y] - second[x].image); //統計相減之後的影象白色畫素總值 for (int i = 0; i < rowMax; i++) { data = absCutImage.ptr<uchar>(i); for (int j = 0; j < colMax; j++) { if (data[j] == 255) { addTemp++; } } } temp = 1.0 - 1.0*addTemp / add; if (temp <= 1 && temp > 0) second[x].matchDegree = temp; else second[x].matchDegree = 0; //獲取最大匹配度對應索引index if (x > 0 && second[x].matchDegree > second[index].matchDegree) index = x; } absCutImage = abs(singleChar[y] - second[index].image); /* namedWindow("圖片相減後圖"+to_string(y)); imshow("圖片相減後圖" + to_string(y), absCutImage);*/ printf("最大匹配度: %lfn", second[index].matchDegree); printf("對應字元: %cn", second[index].number); test[q] = second[index].number; //printf("ntest11111 %cn", test[q]); q++; } test[q] = ' '; //printf("ntest22222 %cn", test[q-1]); //cout<< "xxxxxxxxxxxxxx"<<carPlate<<endl; } void VideoShow(Mat videoImage) { /*if(videoFlag==0)*/ //carPlate = "京A J9846"; carPlate += test; carPlateProvince += carPlate; /*carPlate.copy(test,strlen(test));*/ cout << carPlateProvince << endl; cout << carPlateProvince.length() << endl; if(carPlateProvince.length()<10) putTextZH(videoImage, "Not Plate!", Point(490, 20), Scalar(0, 0, 255), 30, "Arial", false, false); else putTextZH(videoImage, carPlateProvince.c_str(), Point(490, 20), Scalar(0, 0, 255), 30, "Arial", false, false); /*else if(videoFlag==1)*/ namedWindow("提取車牌結果圖"); imshow("提取車牌結果圖", videoImage); carPlateProvince = " "; carPlate = " "; } void GetStringSize(HDC hDC, const char* str, int* w, int* h) { SIZE size; GetTextExtentPoint32A(hDC, str, strlen(str), &size); if (w != 0) *w = size.cx; if (h != 0) *h = size.cy; } void putTextZH(Mat &dst, const char* str, Point org, Scalar color, int fontSize, const char* fn, bool italic, bool underline) { CV_Assert(dst.data != 0 && (dst.channels() == 1 || dst.channels() == 3)); int x, y, r, b; if (org.x > dst.cols || org.y > dst.rows) return; x = org.x < 0 ? -org.x : 0; y = org.y < 0 ? -org.y : 0; LOGFONTA lf; lf.lfHeight = -fontSize; lf.lfWidth = 0; lf.lfEscapement = 0; lf.lfOrientation = 0; lf.lfWeight = 5; lf.lfItalic = italic; //斜體 lf.lfUnderline = underline; //下劃線 lf.lfStrikeOut = 0; lf.lfCharSet = DEFAULT_CHARSET; lf.lfOutPrecision = 0; lf.lfClipPrecision = 0; lf.lfQuality = PROOF_QUALITY; lf.lfPitchAndFamily = 0; strcpy_s(lf.lfFaceName, fn); HFONT hf = CreateFontIndirectA(&lf); HDC hDC = CreateCompatibleDC(0); HFONT hOldFont = (HFONT)SelectObject(hDC, hf); int strBaseW = 0, strBaseH = 0; int singleRow = 0; char buf[1 << 12]; strcpy_s(buf, str); char *bufT[1 << 12]; // 這個用於分隔字串後剩餘的字元,可能會超出。 //處理多行 { int nnh = 0; int cw, ch; const char* ln = strtok_s(buf, "n", bufT); while (ln != 0) { GetStringSize(hDC, ln, &cw, &ch); strBaseW = max(strBaseW, cw); strBaseH = max(strBaseH, ch); ln = strtok_s(0, "n", bufT); nnh++; } singleRow = strBaseH; strBaseH *= nnh; } if (org.x + strBaseW < 0 || org.y + strBaseH < 0) { SelectObject(hDC, hOldFont); DeleteObject(hf); DeleteObject(hDC); return; } r = org.x + strBaseW > dst.cols ? dst.cols - org.x - 1 : strBaseW - 1; b = org.y + strBaseH > dst.rows ? dst.rows - org.y - 1 : strBaseH - 1; org.x = org.x < 0 ? 0 : org.x; org.y = org.y < 0 ? 0 : org.y; BITMAPINFO bmp = { 0 }; BITMAPINFOHEADER& bih = bmp.bmiHeader; int strDrawLineStep = strBaseW * 3 % 4 == 0 ? strBaseW * 3 : (strBaseW * 3 + 4 - ((strBaseW * 3) % 4)); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = strBaseW; bih.biHeight = strBaseH; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = BI_RGB; bih.biSizeImage = strBaseH * strDrawLineStep; bih.biClrUsed = 0; bih.biClrImportant = 0; void* pDibData = 0; HBITMAP hBmp = CreateDIBSection(hDC, &bmp, DIB_RGB_COLORS, &pDibData, 0, 0); CV_Assert(pDibData != 0); HBITMAP hOldBmp = (HBITMAP)SelectObject(hDC, hBmp); //color.val[2], color.val[1], color.val[0] SetTextColor(hDC, RGB(255, 255, 255)); SetBkColor(hDC, 0); //SetStretchBltMode(hDC, COLORONCOLOR); strcpy_s(buf, str); const char* ln = strtok_s(buf, "n", bufT); int outTextY = 0; while (ln != 0) { TextOutA(hDC, 0, outTextY, ln, strlen(ln)); outTextY += singleRow; ln = strtok_s(0, "n", bufT); } uchar* dstData = (uchar*)dst.data; int dstStep = dst.step / sizeof(dstData[0]); unsigned char* pImg = (unsigned char*)dst.data + org.x * dst.channels() + org.y * dstStep; unsigned char* pStr = (unsigned char*)pDibData + x * 3; for (int tty = y; tty <= b; ++tty) { unsigned char* subImg = pImg + (tty - y) * dstStep; unsigned char* subStr = pStr + (strBaseH - tty - 1) * strDrawLineStep; for (int ttx = x; ttx <= r; ++ttx) { for (int n = 0; n < dst.channels(); ++n) { double vtxt = subStr[n] / 255.0; int cvv = vtxt * color.val[n] + (1 - vtxt) * subImg[n]; subImg[n] = cvv > 255 ? 255 : (cvv < 0 ? 0 : cvv); } subStr += 3; subImg += dst.channels(); } } SelectObject(hDC, hOldBmp); SelectObject(hDC, hOldFont); DeleteObject(hf); DeleteObject(hBmp); DeleteDC(hDC); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45