int CPlateRecognize::plateRecognize(Mat src, std::vector<CPlate> &plateVecOut, int img_index) { std::vector<CPlate> plateVec; int resultPD = plateDetect(src, plateVec, img_index); if (resultPD == 0) { size_t num = plateVec.size(); int index = 0; for (size_t j = 0; j < num; j++) { CPlate item = plateVec.at(j); Mat plateMat = item.getPlateMat(); if (0) { imshow("plate", plateMat); waitKey(0); destroyWindow("plate"); } Color color = item.getPlateColor(); if (color == UNKNOWN) { color = getPlateType(plateMat, true); item.setPlateColor(color); } std::string plateColor = getPlateColor(color); if (0) { std::cout << "plateColor:" << plateColor << std::endl; } std::string plateIdentify = ""; int resultCR = charsRecognise(item, plateIdentify); if (resultCR == 0) { std::string license = plateColor + ":" + plateIdentify; item.setPlateStr(license); plateVecOut.push_back(item); } else { std::string license = plateColor; item.setPlateStr(license); plateVecOut.push_back(item); if (0) { std::cout << "resultCR:" << resultCR << std::endl; } } } if (getResultShow()) { Mat result; src.copyTo(result); for (size_t j = 0; j < num; j++) { CPlate item = plateVec[j]; Mat plateMat = item.getPlateMat(); int height = 36; int width = 136; if (height * index + height < result.rows) { Mat imageRoi = result(Rect(0, 0 + height * index, width, height)); addWeighted(imageRoi, 0, plateMat, 1, 0, imageRoi); } index++; RotatedRect minRect = item.getPlatePos(); Point2f rect_points[4]; minRect.points(rect_points); Scalar lineColor = Scalar(255, 255, 255); if (item.getPlateLocateType() == SOBEL) lineColor = Scalar(255, 0, 0); if (item.getPlateLocateType() == COLOR) lineColor = Scalar(0, 255, 0); if (item.getPlateLocateType() == CMSER) lineColor = Scalar(0, 0, 255); for (int j = 0; j < 4; j++) line(result, rect_points[j], rect_points[(j + 1) % 4], lineColor, 2, 8); } showResult(result); } } return resultPD; }
int CPlateRecognize::plateRecognize(Mat src, std::vector<std::string> &licenseVec) { std::vector<CPlate> plateVec; int resultPD = plateDetect(src, plateVec, 0, kDebug, 0); if (resultPD == 0) { size_t num = plateVec.size(); int index = 0; for (size_t j = 0; j < num; j++) { CPlate item = plateVec[j]; Mat plate = item.getPlateMat(); std::string plateType = getPlateColor(plate); std::string plateIdentify = ""; int resultCR = charsRecognise(plate, plateIdentify); if (resultCR == 0) { std::string license = plateType + ":" + plateIdentify; licenseVec.push_back(license); } } if (getResultShow()) { Mat result; src.copyTo(result); for (size_t j = 0; j < num; j++) { CPlate item = plateVec[j]; Mat plate = item.getPlateMat(); int height = 36; int width = 136; if (height * index + height < result.rows) { Mat imageRoi = result(Rect(0, 0 + height * index, width, height)); addWeighted(imageRoi, 0, plate, 1, 0, imageRoi); } index++; RotatedRect minRect = item.getPlatePos(); Point2f rect_points[4]; minRect.points(rect_points); Scalar lineColor = Scalar(255, 255, 255); if (item.getPlateLocateType() == SOBEL) lineColor = Scalar(255, 0, 0); if (item.getPlateLocateType() == COLOR) lineColor = Scalar(0, 255, 0); if (item.getPlateLocateType() == CMSER) lineColor = Scalar(0, 0, 255); for (int j = 0; j < 4; j++) line(result, rect_points[j], rect_points[(j + 1) % 4], lineColor, 2, 8); } showResult(result); } } return resultPD; }
int CPlateDetect::plateDetect(Mat src, vector<CPlate>& resultVec, bool showDetectArea, int index) { vector<Mat> resultPlates; vector<CPlate> color_Plates; vector<CPlate> sobel_Plates; vector<CPlate> color_result_Plates; vector<CPlate> sobel_result_Plates; vector<CPlate> all_result_Plates; //如果颜色查找找到n个以上(包含n个)的车牌,就不再进行Sobel查找了。 const int color_find_max = m_maxPlates; m_plateLocate->plateColorLocate(src, color_Plates, index); m_plateJudge->plateJudge(color_Plates, color_result_Plates); // for (int i=0;i<color_Plates.size();++i) //{ // color_result_Plates.push_back(color_Plates[i]); //} for (size_t i = 0; i < color_result_Plates.size(); i++) { CPlate plate = color_result_Plates[i]; plate.setPlateLocateType(COLOR); all_result_Plates.push_back(plate); } //颜色和边界闭操作同时采用 { m_plateLocate->plateSobelLocate(src, sobel_Plates, index); m_plateJudge->plateJudge(sobel_Plates, sobel_result_Plates); /*for (int i=0;i<sobel_Plates.size();++i) { sobel_result_Plates.push_back(sobel_Plates[i]); }*/ for (size_t i = 0; i < sobel_result_Plates.size(); i++) { CPlate plate = sobel_result_Plates[i]; if (0) { imshow("plate_mat", plate.getPlateMat()); waitKey(0); destroyWindow("plate_mat"); } plate.bColored = false; plate.setPlateLocateType(SOBEL); all_result_Plates.push_back(plate); } } for (size_t i = 0; i < all_result_Plates.size(); i++) { // 把截取的车牌图像依次放到左上角 CPlate plate = all_result_Plates[i]; resultVec.push_back(plate); } return 0; }
int CPlateDetect::plateDetectDeep(Mat src, vector<Mat>& resultVec, bool showDetectArea, int index) { vector<Mat> resultPlates; vector<CPlate> color_Plates; vector<CPlate> sobel_Plates; vector<CPlate> color_result_Plates; vector<CPlate> sobel_result_Plates; vector<CPlate> all_result_Plates; //如果颜色查找找到n个以上(包含n个)的车牌,就不再进行Sobel查找了。 const int color_find_max = 4; Mat result; src.copyTo(result); m_plateLocate->plateColorLocate(src, color_Plates, index); m_plateJudge->plateJudge(color_Plates, color_result_Plates); for (int i = 0; i< color_result_Plates.size(); i++) { CPlate plate = color_result_Plates[i]; RotatedRect minRect = plate.getPlatePos(); Point2f rect_points[4]; minRect.points(rect_points); for (int j = 0; j < 4; j++) line(result, rect_points[j], rect_points[(j + 1) % 4], Scalar(0, 255, 255), 2, 8); all_result_Plates.push_back(plate); } if (color_result_Plates.size() >= color_find_max) { //如果颜色查找找到n个以上(包含n个)的车牌,就不再进行Sobel查找了。 } else { m_plateLocate->plateSobelLocate(src, sobel_Plates, index); m_plateJudge->plateJudge(sobel_Plates, sobel_result_Plates); for (int i = 0; i< sobel_result_Plates.size(); i++) { CPlate plate = sobel_result_Plates[i]; RotatedRect minRect = plate.getPlatePos(); Point2f rect_points[4]; minRect.points( rect_points ); for( int j = 0; j < 4; j++ ) line(result, rect_points[j], rect_points[(j+1)%4], Scalar(0,0,255), 2, 8 ); all_result_Plates.push_back(plate); } } for (int i = 0; i < all_result_Plates.size(); i++) { // 把截取的车牌图像依次放到左上角 CPlate plate = all_result_Plates[i]; Mat plate_area = plate.getPlateMat(); int height = m_plateLocate->HEIGHT; int width = m_plateLocate->WIDTH; assert(height*i + height < result.rows); Mat imageRoi = result(Rect(0, 0 + height*i, width, height)); addWeighted(imageRoi, 0, plate_area, 1, 0, imageRoi); resultVec.push_back(plate_area); } if (showDetectArea) { namedWindow("EasyPR", CV_WINDOW_AUTOSIZE); showResult(result); destroyWindow("EasyPR"); } if (0) { stringstream ss(stringstream::in | stringstream::out); ss << "image/tmp/" << index << "_" << 9 <<"_result" << ".jpg"; imwrite(ss.str(), result); } return 0; }
// !车牌识别模块 int CPlateRecognize::plateRecognize(Mat src, vector<string>& licenseVec, int index) { // 车牌方块集合 vector<CPlate> plateVec; // 如果设置了Debug模式,就依次显示所有的图片 bool showDetectArea = getPDDebug(); // 进行深度定位,使用颜色信息与二次Sobel int resultPD = plateDetectDeep(src, plateVec, showDetectArea, 0); Mat result; src.copyTo(result); if (resultPD == 0) { int num = plateVec.size(); int index = 0; for (int j = 0; j < num; j++) { CPlate item = plateVec[j]; Mat plate = item.getPlateMat(); //获取车牌颜色 string plateType = getPlateColor(plate); //获取车牌号 string plateIdentify = ""; int resultCR = charsRecognise(plate, plateIdentify); if (resultCR == 0) { string license = plateType + ":" + plateIdentify; licenseVec.push_back(license); /*int height = 36; int width = 136; if(height*index + height < result.rows) { Mat imageRoi = result(Rect(0, 0 + height*index, width, height)); addWeighted(imageRoi, 0, plate, 1, 0, imageRoi); } index++;*/ RotatedRect minRect = item.getPlatePos(); Point2f rect_points[4]; minRect.points( rect_points ); if(item.bColored) { for (int j = 0; j < 4; j++) line(result, rect_points[j], rect_points[(j + 1) % 4], Scalar(255, 255, 0), 2, 8); } else { for( int j = 0; j < 4; j++ ) line(result, rect_points[j], rect_points[(j+1)%4], Scalar(0,0,255), 2, 8 );//sobel定位车牌,红色方框 } } } } if (showDetectArea) showResult(result); return resultPD; }
int test_plate_detect(const char*path) { cout << "test_plate_detect" << endl; char* chCurPath = getcwd(NULL, 0); vector<cv::Mat> matVec; vector<cv::Mat> resultVec; CPlateLocate plate; vector<string> fileVec; cv::Mat src; fileVec = BrowseFilenamesOneLayer(path, "*.jpg"); _chdir(chCurPath); cout << fileVec.size() << endl; double fScale = 1; for (int i = 0; i < fileVec.size(); i++) { string fileName = fileVec[i]; cout << "fileName=" << fileName << endl; cv::Mat src = imread(fileName); int rows = src.rows * fScale; int cols = src.cols * fScale; cv::Mat dst; scaleIntervalSampling(src, dst, fScale, fScale); vector<CPlate> resultVec; CPlateDetect pd; pd.setPDLifemode(true); //pd.setDetectShow(true); pd.setDetectType(PR_DETECT_COLOR|PR_DETECT_CMSER ); int result = pd.plateDetect(dst, resultVec); int pos; string realName; pos = fileName.find_last_of('/'); realName = fileName.substr(pos + 1); if (result == 0) { size_t num = resultVec.size(); for (size_t j = 0; j < num; j++) { CPlate resultMat = resultVec[j]; ostringstream os; char a[10]; itoa(j, a, 10); os << "resources/image/general_test_prLast2/"<<realName<<"_" << a << ".jpg"; cout << "tarName="<<os.str() << endl; imwrite(os.str(), resultMat.getPlateMat()); //imshow("plate_detect", resultMat.getPlateMat()); //waitKey(0); } } else cout << "Detect failed!" << endl; //destroyWindow("plate_detect"); } return 0; }
// !车牌识别模块 int CPlateRecognize::plateRecognize(Mat src, std::vector<std::string> &licenseVec) { // 车牌方块集合 std::vector<CPlate> plateVec; // 进行深度定位,使用颜色信息与二次Sobel int resultPD = plateDetect(src, plateVec, kDebug, 0); if (resultPD == 0) { size_t num = plateVec.size(); int index = 0; //依次识别每个车牌内的符号 for (size_t j = 0; j < num; j++) { CPlate item = plateVec[j]; Mat plate = item.getPlateMat(); //获取车牌颜色 std::string plateType = getPlateColor(plate); //获取车牌号 std::string plateIdentify = ""; int resultCR = charsRecognise(plate, plateIdentify); if (resultCR == 0) { std::string license = plateType + ":" + plateIdentify; licenseVec.push_back(license); } } //完整识别过程到此结束 //如果是Debug模式,则还需要将定位的图片显示在原图左上角 if (getPDDebug()) { Mat result; src.copyTo(result); for (size_t j = 0; j < num; j++) { CPlate item = plateVec[j]; Mat plate = item.getPlateMat(); int height = 36; int width = 136; if (height * index + height < result.rows) { Mat imageRoi = result(Rect(0, 0 + height * index, width, height)); addWeighted(imageRoi, 0, plate, 1, 0, imageRoi); } index++; RotatedRect minRect = item.getPlatePos(); Point2f rect_points[4]; minRect.points(rect_points); Scalar lineColor = Scalar(255, 255, 255); if (item.getPlateLocateType() == SOBEL) lineColor = Scalar(255, 0, 0); if (item.getPlateLocateType() == COLOR) lineColor = Scalar(0, 255, 0); for (int j = 0; j < 4; j++) line(result, rect_points[j], rect_points[(j + 1) % 4], lineColor, 2, 8); } //显示定位框的图片 showResult(result); } } return resultPD; }