C++: void minMaxLoc(InputArray src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray() )
C++: void minMaxLoc(const SparseMat& a, double* minVal, double* maxVal, int* minIdx=0, int*maxIdx=0 )
Opencv中的minMaxLoc()函数可以在一幅图像上查找最大最小值及其位置坐标。
里面参数很多,比如最大值maxVlaue、最小值minValue、最大值位置maxLoc、最小值位置minLoc等。
看起来很繁琐但是用起来却很方便,一些不需要的参数,在调用时直接设置为0即可。
函数中,最后一个参数是mask,默认为noArray()。这是一个很有用的参数。
用法也很简单,先预设一个矩阵mask,其大小、数据类型与src一致,每个像素值为0。如果像搜索src某个局部区域的最大最小值,
只需要把mask对应区域设置为大于0的数值即可。下面我们看一个示例:
int main() { Mat img=imread("D:/Qt/MyImage/building.jpg",0); Mat mask(img.rows,img.cols,img.type(),Scalar::all(0)); mask(Rect(150,50,100,100))=255 ;//设定搜索区域 imshow("mask",mask); imshow("img",img); double maxv; minMaxLoc(img,0,&maxv,0,0,mask); cout<<"maxvalue="<<maxv<<endl; waitKey(); return 0; }