Ask Your Question
0

How to find min and max of Mat?

asked 2016-07-19 09:02:44 -0600

updated 2016-07-20 00:38:23 -0600

Greetings everyone! I want to find Max and Min of RGB Values of a Pixel of a Input Mat and I think opencv has a built-in function(cv::reduce) to do that! Could you please tell me how to use that?

Mat mSrc_Bgr(5,5,CV_8UC3);

//Fill Random Numbers
randn(mSrc_Bgr,Scalar::all(125),Scalar::all(100));

Mat mMin_Bgr(mSrc_Bgr.size(),CV_8UC1),mMax_Bgr(mSrc_Bgr.size(),CV_8UC1);

cout<<format("[ B,  G,  R ] -> [    Min ,   Max ]\n");
for( int Row = 0; Row < mSrc_Bgr.rows; Row++ )
{ 
    for( int Col = 0; Col < mSrc_Bgr.cols; Col++ )
    {
        Vec3b Pix_BGR = mSrc_Bgr.at<Vec3b>(Row,Col);
        uchar Min= MIN(Pix_BGR.val[0], MIN(Pix_BGR.val[1], Pix_BGR.val[2])); 
        uchar Max= MAX(Pix_BGR.val[0], MAX(Pix_BGR.val[1], Pix_BGR.val[2])); 
        mMin_Bgr.at<uchar>(Row,Col)=Min;
        mMax_Bgr.at<uchar>(Row,Col)=Max;
        cout<<format("[ %d, %d, %d ]    -> [    %d ,    %d ]\n",Pix_BGR.val[0],Pix_BGR.val[1],Pix_BGR.val[2],Min,Max);
    }
}

//My Expected output
cout<<format("Src Mat Size:[Rows= %d, Cols= %d] \n",mSrc_Bgr.rows,mSrc_Bgr.cols)<<mSrc_Bgr<<"\n";
cout<<" Min: \n"<<mMin_Bgr<<"\n";
cout<<" Max: \n"<<mMax_Bgr<<"\n\n\n";

//What i have tried?
Mat mReshaped= mSrc_Bgr.clone();
mReshaped=mReshaped.reshape(0,mSrc_Bgr.rows*mSrc_Bgr.cols);

reduce(mReshaped,mMin_Bgr,1,CV_REDUCE_MIN);//->?What is wrong here?
reduce(mReshaped,mMax_Bgr,1,CV_REDUCE_MAX);//->?What is wrong here?

// What i get!
cout<< format("Reshaped Mat Size:[Rows= %d, Cols= %d] \n",mReshaped.rows,mReshaped.cols)<<mReshaped<<"\n\n\n";
cout<<format("Min Mat Size:[Rows= %d, Cols= %d] \n",mMin_Bgr.rows,mMin_Bgr.cols)<<mMin_Bgr<<"\n\n\n";
cout<<format("Max Mat Size:[Rows= %d, Cols= %d] \n",mMax_Bgr.rows,mMax_Bgr.cols)<<mMax_Bgr<<"\n\n\n";
edit retag flag offensive close merge delete

Comments

@berak Did you mistakenly deleted the answer?

Balaji R gravatar imageBalaji R ( 2016-07-19 23:08:15 -0600 )edit

yes, sorry, since i misunderstood your question.

let me ask again: you want the maximum of all rows (a single col vector) and also of all channels ?

so in the end , it should be a [5x1] Mat ?

berak gravatar imageberak ( 2016-07-20 00:13:34 -0600 )edit

No no! You are correct only! I just want the Min and max of individual pixels RGB Values only! I reshaped it because reduce works only on Vertical or Horizontal values.

Balaji R gravatar imageBalaji R ( 2016-07-20 00:32:49 -0600 )edit

I was not aware of cv::min & cv::max! Thanks for your input

Balaji R gravatar imageBalaji R ( 2016-07-20 00:33:26 -0600 )edit
1

Please add your answer & i'll mark it as answer

Balaji R gravatar imageBalaji R ( 2016-07-20 00:34:00 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-07-20 00:51:30 -0600

berak gravatar image

we'll do this in 2 steps, the 1st will find the maximum of the channels, the 2nd will reduce over rows to a single column:

Mat bgr(5,5,CV_8UC3);
//Fill Random Numbers
randn(bgr,Scalar::all(125),Scalar::all(100));
cout << "bgr\n" << bgr<< endl;


// no need to iterate, since there's a cv::max() overload for whole Mat's:    
Mat chn[3];
split(bgr, chn);

Mat oneChan = max(chn[0], max(chn[1], chn[2]));

cout << "oneChan\n" << oneChan << endl;

// reduce over rows:
Mat maxR;
reduce(oneChan, maxR, 0, REDUCE_MAX);

cout << "maxR\n" << maxR << endl;

 bgr
[125, 141,  55,  82, 249,  97,  60, 156, 177,  10, 202, 120, 255,   0,   0;
  71,  50, 255, 152,   0, 255,  46,   0,   0,   0, 119, 255, 205,  99,  73;
 255, 162, 254,  61,  39, 232, 130,  97, 110,  88, 230, 172, 114, 228,  50;
  64, 104, 241,   0, 195, 103, 135, 151,  41, 190, 108, 255,  96,  45, 196;
 184, 194,  76,  67, 137,  29,  29, 242, 190, 162,   8, 255, 253, 255,  75]
oneChan
[141, 249, 177, 202, 255;
 255, 255,  46, 255, 205;
 255, 232, 130, 230, 228;
 241, 195, 151, 255, 196;
 194, 137, 242, 255, 255]
maxR
[255, 255, 242, 255, 255]
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-07-19 09:02:44 -0600

Seen: 9,027 times

Last updated: Jul 20 '16