学OpenCV
================================================
这里的行列对应y和x。
================================================
1 #include <iostream> 2 3 #include <opencv2/opencv.hpp> 4 #include <opencv2/core/utils/logger.hpp> 5 6 7 void Test1() 8 { 9 float arr[] = { 1,2,3,4, 10 5,6,7,8, 11 9,10,11,12 }; 12 cv::Mat img(3,4,CV_32FC1,arr); 13 14 double min, max; 15 cv::Point posmin, posmax; 16 cv::minMaxLoc(img, &min, &max, &posmin, &posmax); 17 std::cout << "Test1 min:" << min << std::endl; 18 std::cout << "Test1 max:" << max << std::endl; 19 std::cout << "Test1 posmin:" << posmin << " row=" << posmin.y<<",col=" << posmin.x << std::endl; 20 std::cout << "Test1 posmax:" << posmax <<" row=" << posmax.y << ",col=" << posmax.x << std::endl; 21 } 22 23 template <typename T> 24 T* MakeArrInSerial(int datasize) 25 { 26 T* ptr = new T[datasize]; 27 for (int i = 0; i < datasize; i++) 28 { 29 ptr[i] = (T)i; 30 } 31 return ptr; 32 } 33 34 35 void Test2() 36 { 37 float* arr = MakeArrInSerial<float>(36); 38 cv::Mat img(3, 4, CV_32FC3, arr); 39 40 double min, max; 41 cv::Point posmin, posmax; 42 43 cv::Mat imgc1=img.reshape(1); 44 cv::minMaxLoc(imgc1, &min, &max, &posmin, &posmax); 45 std::cout << "Test2 min:" << min << std::endl; 46 std::cout << "Test2 max:" << max << std::endl; 47 std::cout << "Test2 posmin:" << posmin << " row=" << posmin.y << ",col=" << posmin.x << std::endl; 48 std::cout << "Test2 posmax:" << posmax << " row=" << posmax.y << ",col=" << posmax.x << std::endl; 49 } 50 51 int main() 52 { 53 cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR); 54 55 Test1(); 56 Test2(); 57 58 cv::waitKey(0); 59 60 return 0; 61 }
================================================
结果
================================================
这里的reshape函数,虽然有3个重载,但似乎只有1个用起来方便,其他的没成功。
1 #include <iostream> 2 3 #include <opencv2/opencv.hpp> 4 #include <opencv2/core/utils/logger.hpp> 5 6 7 template <typename T> 8 T* MakeSerialArr(int datasize) 9 { 10 T* ptr = new T[datasize]; 11 for (int i = 0; i < datasize; i++) 12 { 13 ptr[i] = (T)i; 14 } 15 return ptr; 16 } 17 18 #define SerialArr_Heap(name,type,size) \ 19 type* name = new type[size];\ 20 for (int i = 0; i < size; i++) \ 21 {\ 22 name[i] = (type)i;\ 23 } 24 25 #define SerialArr_Stack(name,type,size) \ 26 type name[size]={};\ 27 for (int i = 0; i < size; i++) \ 28 {\ 29 name[i] = (type)i;\ 30 } 31 32 33 void Test1() 34 { 35 SerialArr_Stack(arr,int,5*6*3) 36 cv::Mat m1(5, 6, CV_32SC3, arr); 37 cv::Mat m11=m1.reshape(2); 38 cv::Mat m12 = m1.reshape(2, 3); 39 40 //cv::Mat m12 = m1.reshape(2,4); 41 //如果不能整除,那么会报异常。 42 //OpenCV(4.10.0) Error: Bad argument (The total number of matrix elements is not divisible by the new number of rows) 43 // in cv::Mat::reshape, file C:\GHA-OCV-1\_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\core\src\matrix.cpp, 44 // line 1183 45 } 46 47 void Test2() 48 { 49 SerialArr_Stack(arr, int, 5 * 6 * 3); 50 cv::Mat m1(5, 6, CV_32SC3, arr); 51 //cv::Mat m11 = m1.reshape(3,std::vector<int>(6,5)); 52 53 //OpenCV(4.10.0) Error: Sizes of input arguments do not match (Requested and source matrices have different count of elements) 54 // in cv::Mat::reshape, file C:\GHA-OCV-1\_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\core\src\matrix.cpp, 55 // line 1240 56 } 57 58 void Test3() 59 { 60 SerialArr_Stack(arr, int, 5 * 6 * 3); 61 cv::Mat m1(5, 6, CV_32SC3, arr); 62 int newndims[] = { 2,2 }; 63 //cv::Mat m11 = m1.reshape(2, 2, newndims); 64 //cv::Mat m11 = m1.reshape(2,4,newndims); 65 66 //OpenCV(4.10.0) Error: Sizes of input arguments do not match(Requested and source matrices have different count of elements) 67 // in cv::Mat::reshape, file C : \GHA - OCV - 1\_work\ci - gha - workflow\ci - gha - workflow\opencv\modules\core\src\matrix.cpp, 68 // line 1240 69 } 70 71 72 int main() 73 { 74 cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR); 75 76 Test1(); 77 Test2(); 78 Test3(); 79 80 cv::waitKey(0); 81 82 return 0; 83 }