Ask Your Question

Revision history [back]

How to use cv::reduce to find max of Row elements?

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";

How to use cv::reduce to find min and max of Row elements?Mat?

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";