From 4ad8ff3ffd3a2c778a8e4513e4ac4985377e328c Mon Sep 17 00:00:00 2001 From: zhuyiming Date: Tue, 4 Jun 2024 21:06:08 +0800 Subject: [PATCH] support for opencv 4.5.0 --- CMakeLists.txt | 2 +- include/easypr/config.h | 10 +++++----- src/core/core_func.cpp | 1 + src/core/feature.cpp | 18 +++++++++--------- src/core/plate_locate.cpp | 15 ++++++++------- src/train/annCh_train.cpp | 2 +- src/train/ann_train.cpp | 2 +- src/train/svm_train.cpp | 2 +- thirdparty/CMakeLists.txt | 2 +- thirdparty/LBP/helper.cpp | 18 +++++++++--------- 10 files changed, 37 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 43df99d0..10baa166 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ if (CMAKE_SYSTEM_NAME MATCHES "Darwin") endif () # OpenVC3 required -find_package(OpenCV 3.2.0 REQUIRED) +find_package(OpenCV 4.5.0 REQUIRED) # where to find header files include_directories(.) diff --git a/include/easypr/config.h b/include/easypr/config.h index 9634a305..ee1b9ea2 100644 --- a/include/easypr/config.h +++ b/include/easypr/config.h @@ -1,7 +1,7 @@ #ifndef EASYPR_CONFIG_H_ #define EASYPR_CONFIG_H_ -#define CV_VERSION_THREE_ZERO +#define CV_VERSION_TWO namespace easypr { @@ -133,14 +133,14 @@ private:\ // Load model. compatitable withe 3.0, 3.1 and 3.2 #ifdef CV_VERSION_THREE_TWO #define LOAD_SVM_MODEL(model, path) \ - model = ml::SVM::load(path); + model = Algorithm::load(path); #define LOAD_ANN_MODEL(model, path) \ - model = ml::ANN_MLP::load(path); + model = Algorithm::load(path); #else #define LOAD_SVM_MODEL(model, path) \ - model = ml::SVM::load(path); + model = Algorithm::load(path); #define LOAD_ANN_MODEL(model, path) \ - model = ml::ANN_MLP::load(path); + model = Algorithm::load(path); #endif } diff --git a/src/core/core_func.cpp b/src/core/core_func.cpp index a4e419e1..24376368 100644 --- a/src/core/core_func.cpp +++ b/src/core/core_func.cpp @@ -4,6 +4,7 @@ #include "easypr/config.h" #include "easypr/core/params.h" #include "thirdparty/mser/mser2.hpp" +#include #include namespace easypr { diff --git a/src/core/feature.cpp b/src/core/feature.cpp index ddced9ab..7b551fd1 100644 --- a/src/core/feature.cpp +++ b/src/core/feature.cpp @@ -33,12 +33,12 @@ Mat getHistogram(Mat in) { void getHistogramFeatures(const Mat& image, Mat& features) { Mat grayImage; - cvtColor(image, grayImage, CV_RGB2GRAY); + cvtColor(image, grayImage, COLOR_RGB2GRAY); //grayImage = histeq(grayImage); Mat img_threshold; - threshold(grayImage, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); + threshold(grayImage, img_threshold, 0, 255, cv::THRESH_OTSU + cv::THRESH_BINARY); //Mat img_threshold = grayImage.clone(); //spatial_ostu(img_threshold, 8, 2, getPlateType(image, false)); @@ -50,7 +50,7 @@ void getColorFeatures(const Mat& src, Mat& features) { Mat src_hsv; //grayImage = histeq(grayImage); - cvtColor(src, src_hsv, CV_BGR2HSV); + cvtColor(src, src_hsv, COLOR_BGR2HSV); int channels = src_hsv.channels(); int nRows = src_hsv.rows; @@ -108,7 +108,7 @@ void getSIFTFeatures(const Mat& image, Mat& features) { //HOG Features void getHOGFeatures(const Mat& image, Mat& features) { //HOG descripter - HOGDescriptor hog(cvSize(128, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 3); //these parameters work well + HOGDescriptor hog(cv::Size(128, 64), cv::Size(16, 16), cv::Size(8, 8), cv::Size(8, 8), 3); //these parameters work well std::vector descriptor; // resize input image to (128,64) for compute @@ -133,7 +133,7 @@ void getHSVHistFeatures(const Mat& image, Mat& features) { void getLBPFeatures(const Mat& image, Mat& features) { Mat grayImage; - cvtColor(image, grayImage, CV_RGB2GRAY); + cvtColor(image, grayImage, COLOR_RGB2GRAY); Mat lbpimage; lbpimage = libfacerec::olbp(grayImage); @@ -377,7 +377,7 @@ void getGrayPlusProject(const Mat& grayChar, Mat& features) } SHOW_IMAGE(grayChar, 0); Mat binaryChar; - threshold(grayChar, binaryChar, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); + threshold(grayChar, binaryChar, 0, 255, cv::THRESH_OTSU + cv::THRESH_BINARY); SHOW_IMAGE(binaryChar, 0); Mat projectFeature = charProjectFeatures(binaryChar, 32); @@ -442,7 +442,7 @@ void getGrayPlusLBP(const Mat& grayChar, Mat& features) void getLBPplusHistFeatures(const Mat& image, Mat& features) { Mat grayImage; - cvtColor(image, grayImage, CV_RGB2GRAY); + cvtColor(image, grayImage, COLOR_RGB2GRAY); Mat lbpimage; lbpimage = libfacerec::olbp(grayImage); @@ -450,12 +450,12 @@ void getLBPplusHistFeatures(const Mat& image, Mat& features) { //features = lbp_hist.reshape(1, 1); Mat greyImage; - cvtColor(image, greyImage, CV_RGB2GRAY); + cvtColor(image, greyImage, COLOR_RGB2GRAY); //grayImage = histeq(grayImage); Mat img_threshold; threshold(greyImage, img_threshold, 0, 255, - CV_THRESH_OTSU + CV_THRESH_BINARY); + cv::THRESH_OTSU + cv::THRESH_BINARY); Mat histomFeatures = getHistogram(img_threshold); hconcat(lbp_hist.reshape(1, 1), histomFeatures.reshape(1, 1), features); diff --git a/src/core/plate_locate.cpp b/src/core/plate_locate.cpp index 90b87191..d4dd2d65 100644 --- a/src/core/plate_locate.cpp +++ b/src/core/plate_locate.cpp @@ -2,6 +2,7 @@ #include "easypr/core/core_func.h" #include "easypr/util/util.h" #include "easypr/core/params.h" +#include using namespace std; @@ -120,7 +121,7 @@ int CPlateLocate::colorSearch(const Mat &src, const Color r, Mat &out, Mat src_threshold; threshold(match_grey, src_threshold, 0, 255, - CV_THRESH_OTSU + CV_THRESH_BINARY); + cv::THRESH_OTSU + cv::THRESH_BINARY); Mat element = getStructuringElement( MORPH_RECT, Size(color_morph_width, color_morph_height)); @@ -313,7 +314,7 @@ int CPlateLocate::sobelOper(const Mat &in, Mat &out, int blurSize, int morphW, Mat mat_gray; if (mat_blur.channels() == 3) - cvtColor(mat_blur, mat_gray, CV_RGB2GRAY); + cvtColor(mat_blur, mat_gray, COLOR_RGB2GRAY); else mat_gray = mat_blur; @@ -333,7 +334,7 @@ int CPlateLocate::sobelOper(const Mat &in, Mat &out, int blurSize, int morphW, Mat mat_threshold; double otsu_thresh_val = - threshold(grad, mat_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); + threshold(grad, mat_threshold, 0, 255, cv::THRESH_OTSU + cv::THRESH_BINARY); Mat element = getStructuringElement(MORPH_RECT, Size(morphW, morphH)); @@ -346,7 +347,7 @@ int CPlateLocate::sobelOper(const Mat &in, Mat &out, int blurSize, int morphW, void deleteNotArea(Mat &inmat, Color color = UNKNOWN) { Mat input_grey; - cvtColor(inmat, input_grey, CV_BGR2GRAY); + cvtColor(inmat, input_grey, COLOR_BGR2GRAY); int w = inmat.cols; int h = inmat.rows; @@ -368,7 +369,7 @@ void deleteNotArea(Mat &inmat, Color color = UNKNOWN) { Mat tmp = input_grey(Rect_(w * 0.15, h * 0.15, w * 0.7, h * 0.7)); int threadHoldV = ThresholdOtsu(tmp); - threshold(input_grey, img_threshold, threadHoldV, 255, CV_THRESH_BINARY); + threshold(input_grey, img_threshold, threadHoldV, 255, cv::THRESH_BINARY); // threshold(input_grey, img_threshold, 5, 255, CV_THRESH_OTSU + // CV_THRESH_BINARY); @@ -380,7 +381,7 @@ void deleteNotArea(Mat &inmat, Color color = UNKNOWN) { int threadHoldV = ThresholdOtsu(tmp); threshold(input_grey, img_threshold, threadHoldV, 255, - CV_THRESH_BINARY_INV); + cv::THRESH_BINARY_INV); utils::imwrite("resources/image/tmp/inputgray2.jpg", img_threshold); @@ -388,7 +389,7 @@ void deleteNotArea(Mat &inmat, Color color = UNKNOWN) { // CV_THRESH_BINARY_INV); } else threshold(input_grey, img_threshold, 10, 255, - CV_THRESH_OTSU + CV_THRESH_BINARY); + cv::THRESH_OTSU + cv::THRESH_BINARY); //img_threshold = input_grey.clone(); //spatial_ostu(img_threshold, 8, 2, plateType); diff --git a/src/train/annCh_train.cpp b/src/train/annCh_train.cpp index ec98607c..f451b8be 100644 --- a/src/train/annCh_train.cpp +++ b/src/train/annCh_train.cpp @@ -71,7 +71,7 @@ namespace easypr { ann_->setLayerSizes(layers); ann_->setActivationFunction(cv::ml::ANN_MLP::SIGMOID_SYM, 1, 1); ann_->setTrainMethod(cv::ml::ANN_MLP::TrainingMethods::BACKPROP); - ann_->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER, 30000, 0.0001)); + ann_->setTermCriteria(TermCriteria(cv::TermCriteria::MAX_ITER, 30000, 0.0001)); ann_->setBackpropWeightScale(0.1); ann_->setBackpropMomentumScale(0.1); diff --git a/src/train/ann_train.cpp b/src/train/ann_train.cpp index 51aec1ff..5d9a871d 100644 --- a/src/train/ann_train.cpp +++ b/src/train/ann_train.cpp @@ -74,7 +74,7 @@ void AnnTrain::train() { ann_->setLayerSizes(layers); ann_->setActivationFunction(cv::ml::ANN_MLP::SIGMOID_SYM, 1, 1); ann_->setTrainMethod(cv::ml::ANN_MLP::TrainingMethods::BACKPROP); - ann_->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER, 30000, 0.0001)); + ann_->setTermCriteria(TermCriteria(cv::TermCriteria::MAX_ITER, 30000, 0.0001)); ann_->setBackpropWeightScale(0.1); ann_->setBackpropMomentumScale(0.1); diff --git a/src/train/svm_train.cpp b/src/train/svm_train.cpp index edd711eb..832155d7 100644 --- a/src/train/svm_train.cpp +++ b/src/train/svm_train.cpp @@ -30,7 +30,7 @@ void SvmTrain::train() { svm_->setC(1); svm_->setNu(0.1); svm_->setP(0.1); - svm_->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER, 20000, 0.0001)); + svm_->setTermCriteria(TermCriteria(cv::TermCriteria::MAX_ITER, 20000, 0.0001)); this->prepare(); diff --git a/thirdparty/CMakeLists.txt b/thirdparty/CMakeLists.txt index 9d329ffe..8367b3fd 100644 --- a/thirdparty/CMakeLists.txt +++ b/thirdparty/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.1.0) project(thirdparty) # OpenVC3 required -find_package(OpenCV 3.1.0 REQUIRED) +find_package(OpenCV 4.5.0 REQUIRED) # where to find header files include_directories(${OpenCV_INCLUDE_DIRS}) diff --git a/thirdparty/LBP/helper.cpp b/thirdparty/LBP/helper.cpp index 8026c54a..985af605 100644 --- a/thirdparty/LBP/helper.cpp +++ b/thirdparty/LBP/helper.cpp @@ -89,9 +89,9 @@ bool libfacerec::isSymmetric(InputArray src, double eps) { Mat libfacerec::argsort(InputArray _src, bool ascending) { Mat src = _src.getMat(); if (src.rows != 1 && src.cols != 1) { - CV_Error(CV_StsBadArg, "cv::argsort only sorts 1D matrices."); + CV_Error(Error::StsBadArg, "cv::argsort only sorts 1D matrices."); } - int flags = CV_SORT_EVERY_ROW+(ascending ? CV_SORT_ASCENDING : CV_SORT_DESCENDING); + int flags = cv::SORT_EVERY_ROW+(ascending ? cv::SORT_ASCENDING : cv::SORT_DESCENDING); Mat sorted_indices; cv::sortIdx(src.reshape(1,1),sorted_indices,flags); return sorted_indices; @@ -142,7 +142,7 @@ Mat libfacerec::histc(InputArray _src, int minVal, int maxVal, bool normed) { return histc_(src, minVal, maxVal, normed); break; default: - CV_Error(CV_StsUnmatchedFormats, "This type is not implemented yet."); break; + CV_Error(cv::Error::StsUnmatchedFormats, "This type is not implemented yet."); break; } return Mat(); } @@ -154,7 +154,7 @@ Mat libfacerec::histc(InputArray _src, int minVal, int maxVal, bool normed) { void libfacerec::sortMatrixColumnsByIndices(InputArray _src, InputArray _indices, OutputArray _dst) { if(_indices.getMat().type() != CV_32SC1) { string error_message = format("cv::sortRowsByIndices only works on integer indices! Expected: %d. Given: %d.", CV_32SC1, _indices.getMat().type()); - CV_Error(CV_StsBadArg, error_message); + CV_Error(Error::StsBadArg , error_message); } Mat src = _src.getMat(); vector indices = _indices.getMat(); @@ -179,7 +179,7 @@ Mat libfacerec::sortMatrixColumnsByIndices(InputArray src, InputArray indices) { void libfacerec::sortMatrixRowsByIndices(InputArray _src, InputArray _indices, OutputArray _dst) { if(_indices.getMat().type() != CV_32SC1) { string error_message = format("cv::sortRowsByIndices only works on integer indices! Expected: %d. Given: %d.", CV_32SC1, _indices.getMat().type()); - CV_Error(CV_StsBadArg, error_message); + CV_Error(Error::StsBadArg, error_message); } Mat src = _src.getMat(); vector indices = _indices.getMat(); @@ -204,7 +204,7 @@ Mat libfacerec::sortMatrixRowsByIndices(InputArray src, InputArray indices) { Mat libfacerec::asRowMatrix(InputArrayOfArrays src, int rtype, double alpha, double beta) { // make sure the input data is a vector of matrices or vector of vector if(src.kind() != _InputArray::STD_VECTOR_MAT && src.kind() != _InputArray::STD_VECTOR_VECTOR) { - CV_Error(CV_StsBadArg, "The data is expected as InputArray::STD_VECTOR_MAT (a std::vector) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >)."); + CV_Error(Error::StsBadArg , "The data is expected as InputArray::STD_VECTOR_MAT (a std::vector) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >)."); } // number of samples size_t n = src.total(); @@ -220,7 +220,7 @@ Mat libfacerec::asRowMatrix(InputArrayOfArrays src, int rtype, double alpha, dou // make sure data can be reshaped, throw exception if not! if(src.getMat(i).total() != d) { string error_message = format("Wrong number of elements in matrix #%d! Expected %d was %d.", i, d, src.getMat(i).total()); - CV_Error(CV_StsBadArg, error_message); + CV_Error(Error::StsBadArg , error_message); } // get a hold of the current row Mat xi = data.row(i); @@ -240,7 +240,7 @@ Mat libfacerec::asRowMatrix(InputArrayOfArrays src, int rtype, double alpha, dou Mat libfacerec::asColumnMatrix(InputArrayOfArrays src, int rtype, double alpha, double beta) { // make sure the input data is a vector of matrices or vector of vector if(src.kind() != _InputArray::STD_VECTOR_MAT && src.kind() != _InputArray::STD_VECTOR_VECTOR) { - CV_Error(CV_StsBadArg, "The data is expected as InputArray::STD_VECTOR_MAT (a std::vector) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >)."); + CV_Error(Error::StsBadArg , "The data is expected as InputArray::STD_VECTOR_MAT (a std::vector) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >)."); } int n = (int) src.total(); // return empty matrix if no data given @@ -255,7 +255,7 @@ Mat libfacerec::asColumnMatrix(InputArrayOfArrays src, int rtype, double alpha, // make sure data can be reshaped, throw exception if not! if(src.getMat(i).total() != d) { string error_message = format("Wrong number of elements in matrix #%d! Expected %d was %d.", i, d, src.getMat(i).total()); - CV_Error(CV_StsBadArg, error_message); + CV_Error(Error::StsBadArg , error_message); } // get a hold of the current row Mat yi = data.col(i);