int CCharsRecognise::charsRecognise(CPlate& plate, std::string& plateLicense) { std::vector<Mat> matChars; Mat plateMat = plate.getPlateMat(); // 判断车牌颜色以此确认threshold方法 Color color; if (plate.getPlateLocateType() == CMSER) { color = plate.getPlateColor(); } else { int w = plateMat.cols; int h = plateMat.rows; Mat tmpMat = plateMat(Rect_<double>(w * 0.1, h * 0.1, w * 0.8, h * 0.8)); color = getPlateType(tmpMat, true); } int result = m_charsSegment->charsSegment(plateMat, matChars, color); //std::cout << "charsSegment:" << result << std::endl; if (result == 0) { //for (auto block : matChars) { // auto character = CharsIdentify::instance()->identify(block); // plateLicense.append(character.second); //} int num = matChars.size(); for (int j = 0; j < num; j++) { Mat charMat = matChars.at(j); bool isChinses = false; //if (j == 0) // isChinses = true; //auto character = CharsIdentify::instance()->identify(charMat, isChinses); //plateLicense.append(character.second); std::pair<std::string, std::string> character; float maxVal; if (j == 0) { isChinses = true; bool judge = true; character = CharsIdentify::instance()->identifyChinese(charMat, maxVal, judge); plateLicense.append(character.second); } else { isChinses = false; character = CharsIdentify::instance()->identify(charMat, isChinses); plateLicense.append(character.second); } CCharacter charResult; charResult.setCharacterMat(charMat); charResult.setCharacterStr(character.second); plate.addReutCharacter(charResult); } if (plateLicense.size() < 7) { return -1; } } return result; }
// !车牌识别模块 int CPlateRecognize::plateRecognize(Mat src, std::vector<string> &licenseVec) { // 车牌方块集合 vector<CPlate> plateVec; // 进行深度定位,使用颜色信息与二次Sobel int resultPD = plateDetect(src, plateVec, getPDDebug(), 0); 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); } } //完整识别过程到此结束 //如果是Debug模式,则还需要将定位的图片显示在原图左上角 if (getPDDebug() == true) { Mat result; src.copyTo(result); for (int 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; }
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 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; }