我试图找到 Mat 的最大值和最小值,但我无法得到正确的答案。我在 MATLAB 中得到了正确的最大值和最小值,即 maxValue==2.2222 和 minValue==0.0810810。但是我得到了其他的结果在 OpenCV 中 maxValue==8.988e+307 和 minValue==0.0232549。在 OpenCV 中,我使用了两种方法,std::max_element() 和 minMaxLoc(),得到了相同的结果。 我的代码(OpenCV):
#include <iostream>
#include <string>
#include <math.h>
#include <cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define PI 3.1415926
using namespace std;
using namespace cv;
int main()
{
Mat img=imread("test.jpg");
if(!img.data)
{
cout<<"Error:Reading image!"<<endl;
}
img.convertTo(img,CV_64FC3);
vector<Mat> rgb;
split( img, rgb);
Mat B=rgb[0];
Mat G=rgb[1];
Mat R=rgb[2];
Mat x,X,y,Y;
divide(R,G,x,1);
divide(B,G,y,1);
log(x,X);
log(y,Y);
Mat projectedPoint;
projectedPoint=X.mul(cos(PI))+Y.mul(sin(PI));
Mat imgGrey;
exp(projectedPoint,imgGrey);
//method 1;
cout<<"max_element=="<<*max_element(imgGrey.begin<double>(),imgGrey.end<double>())<<endl;
//method 2;
double imgGreyMin=0;
double imgGreyMax=0;
minMaxLoc(imgGrey,&imgGreyMin,&imgGreyMax,NULL,NULL);
cout<<"minValue=="<<imgGreyMin<<" maxValue=="<<imgGreyMax<<endl;
waitKey(0);
return 0;
}
MATLAB:
clc;
clear;
format long;
img=imread('test.jpg');
img=im2double(img);
R=img(:,:,1);
G=img(:,:,2);
B=img(:,:,3);
X=log(R./G);
Y=log(B./G);
W=size(X,1);
L=size(X,2);
projectedPoint=ones(W,L);
projectedPoint=X*cosd(180)+Y*sind(180);
imgGrey=exp(projectedPoint);
minValue=min(min(imgGrey));
maxValue=max(max(imgGrey));
最佳答案
我的猜测是问题不在 minMaxLoc 中,即当您的函数达到这一点时,imgGrey 确实具有如此大的值。如果其中一个 G 像素等于零,则可能会发生这种情况(我猜 MATLAB 会以不同方式处理这种情况)。
旁注,我不太明白这一行是干什么用的:
projectedPoint=X.mul(cos(PI))+Y.mul(sin(PI));
cos(PI) 为 -1。 sin(PI) 为 0。你可以这样写:
projectedPoint = -X;
关于c++ - OpenCV 无法获得 Mat 的正确最大值和最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21214936/