Ask Your Question
0

minimum value per pixel (w.r.t channel) in a 3 channel image

asked 2016-05-30 06:36:23 -0600

hyder gravatar image

Hi everyone,

Is there any function in openCV which can return the minimum/maximum value of a pixel w.r.t channel in a 3 channel image (e.g. a BGR image) like the following available in MATLAB:

temp = min(im, [], 3);

I know that you can use nested loops to access each pixel using Mat::at<type>(i,j) operator and then use min function twice to get the minimum value per pixel like this:

for (int m = 0; m<src.rows; m++)
    {
        for (int n = 0; n<src.cols; n++)
        {
            intensity = src.at<Vec3b>(m, n);
            rgbmin.at<uchar>(m, n) = min(min(intensity.val[0], intensity.val[1]), intensity.val[2]);
        }
    }

but this seems slower and I am looking for a more efficient way to do this.

Thanks

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-06-02 01:10:44 -0600

hyder gravatar image

I found another way of doing this:

Mat temp, minChannel, bgr[3];

split(src, bgr);                           //split src into B,G,R channels
(cv::min)(bgr[0], bgr[1], temp);           //find minimum between B and G channels
(cv::min)(temp, bgr[2], minChannel);       //find minimum between temp and R channels
edit flag offensive delete link more
1

answered 2016-05-30 07:29:07 -0600

LBerger gravatar image

updated 2016-05-31 02:22:26 -0600

You can try like this :

 vector<Mat> x;
 split(src,x)
 vector<double> minChannel;
 vector<double> maxChannel;
 minChannel.resize(x.size());
 maxChannel.resize(x.size());
 for (int i=0;i<x.size();i++)
      minMaxLoc(x[i],&minChannel[i],&maxChannel[i]);

resuts are in vectors minChannels and maxChannels

edit flag offensive delete link more

Comments

Thanks for the reply. Shouldn't it be split(src, x) rather than split(x, src)?

hyder gravatar imagehyder ( 2016-05-31 01:54:02 -0600 )edit

Oops..Yes thanks

LBerger gravatar imageLBerger ( 2016-05-31 02:22:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-05-30 06:36:23 -0600

Seen: 4,372 times

Last updated: Jun 02 '16