opencv學習筆記——minMaxIdx函數的含義及用法


opencv中有時需要對Mat數據需要對其中的數據求取最大值和最小值。opencv提供了直接的函數

CV_EXPORTS_W void minMaxLoc(InputArray src, CV_OUT double* minVal,  
                           CV_OUT double* maxVal=0, CV_OUT Point* minLoc=0,  
                           CV_OUT Point* maxLoc=0, InputArray mask=noArray());  
CV_EXPORTS void minMaxIdx(InputArray src, double* minVal, double* maxVal,  
                          int* minIdx=0, int* maxIdx=0, InputArray mask=noArray());  

實現代碼如下所示:

    #include <iostream>  
    #include <cstdlib>  
    #include <cmath>  
    #include <iomanip>  
    #include <algorithm>  
      
    #include "opencv2/core/core.hpp"  
    #include "opencv2/highgui/highgui.hpp"  
    #include "opencv2/imgproc/imgproc.hpp"  
    #include "opencv2/contrib/contrib.hpp"  
      
    using namespace std;  
    using namespace cv;  
      
    int main(int argc, char* argv[])  
    {  
    #pragma region min_max  
      
        float Tval = 0.0;  
        float RawData[2][3] = {{4.0,1.0,3.0},{8.0,7.0,9.0}};  
        Mat RawDataMat(2,3,CV_32FC1,RawData);  
      
        for (int j = 0; j < 2; j++)  
        {  
            for (int i = 0; i < 3; i++)  
            {     
                //Tval = RawData[j][i];             //No problem !!!  
                Tval = RawDataMat.at<float>(j,i);  
                cout << "(j,i) = "<<j<<","<<i<<"\t"<<Tval<<endl;  
            }  
        }  
      
        double minv = 0.0, maxv = 0.0;  
        double* minp = &minv;  
        double* maxp = &maxv;  
      
        minMaxIdx(RawDataMat,minp,maxp);  
      
        cout << "Mat minv = " << minv << endl;  
        cout << "Mat maxv = " << maxv << endl;  
      
    #pragma endregion  
      
        return 0;  
    }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM