首页 > 其他分享 >Mat的最大值、最小值

Mat的最大值、最小值

时间:2024-08-21 11:16:40浏览次数:6  
标签:arr Mat img 最大值 posmax 最小值 posmin cv

学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 }

================================================

结果

 

标签:arr,Mat,img,最大值,posmax,最小值,posmin,cv
From: https://www.cnblogs.com/wlsandwho/p/18371218

相关文章