0

I created the following function to calculate the point with minimum intensity in an image matrix using opencv. However, I'm always getting the point 0,0 as the point with minimum intensity and the values of intensities are also absurd like -2142484923. Can someone help with this?

img is the input image matrix and minloc returns cvPoint with minimum intensity.

int min_Loc(Mat img, Point minloc)
{

    cvtColor(img, img, CV_RGB2GRAY); 
    int x1;
    int y1;
    int x2;
    int y2;

    x1 = 0;
    x2 = img.rows;
    y1 = 0;
    y2 = img.cols;

    int min = std::numeric_limits< int >::max();
    int currentval;
    for (int j=y1; j<y2; j++)
    {

        for (int i=x1; i<x2; i++){
            currentval = img.at<int>(j,i);
            if(currentval < min){
                min = std::min<int>( currentval, min );
                minloc.x = i;
                minloc.y = j;
            }
        }

    }

    return min;


}
Karthik T
  • 31,456
  • 5
  • 68
  • 87
Abhishek Thakur
  • 16,337
  • 15
  • 66
  • 97

2 Answers2

1

Your function

int min_Loc(Mat img, Point minloc)

does not return minloc. It returns an integer, and takes a Mat and a Point, both by value. If you want to be able to modify the values of minloc and have that persist after you call min_Loc(), you should use pointers or references as parameters like:

int min_loc(Mat * img, Point * minloc)
{
...
minloc->x = i;
minloc->y = j;
...
return min;
}

and the call to the function would be like:

min_loc(&img, &minloc);

this assumes you have somewhere before that call something like:

Mat img = ...;
Point minloc = ...;

more info on that: When to pass by reference and when to pass by pointer in C++?

Community
  • 1
  • 1
nairdaen
  • 1,037
  • 2
  • 11
  • 19
1

Note that OpenCV provides this the ability to find the minimum and maximum values of a (single-channel) Matrix, along with their locations, in the function cv::minMaxLoc

Chris
  • 8,030
  • 4
  • 37
  • 56