2014-01-19 125 views
1

我试图找到最大值和最小值的Mat,但我无法得到正确答案。我得到了正确的最大值和最小值,即maxValue == 2.2222和minValue == 0.0810810在MATLAB中。但我得到了其他结果,maxValue == 8.988e + 307和minValue == 0.0232549在OpenCV.In OpenCV中,我使用了两个方法,std :: max_element()和minMaxLoc(),并得到相同的结果。 我的代码(OpenCV的):OpenCV无法获得正确的最大值和最小值Mat

#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)); 
+0

在MATLAB中,数据类型是双倍的。 –

+0

1e307看起来像NaN/Inf,可能来自绿色图像通道中的零。 matlab中的'isnan(imgGrey)'和'isinf(imgGrey)'给出了什么? – mars

+0

你是对的,imgGrey的一些元素是NaN,但是当我在opencv中检查这些元素时,结果显示它们不是NaN。@ mars –

回答

1

我的猜测是,这个问题是不是在minMaxLoc,即当您的功能达到这一点确实imgGrey有如此大的值。如果其中一个G像素等于零(我想MATLAB会以不同的方式处理这种情况)。

在侧面说明,我不是很懂这行是:

projectedPoint=X.mul(cos(PI))+Y.mul(sin(PI)); 

COS(PI)是-1。 sin(PI)为0.你可以这样写:

projectedPoint = -X; 
+0

感谢你的答复。我在MATLAB中获得了max的位置,并且我使用该位置在opencv中获得了正确的值,所以我认为ingGrey没有这么大的值.PS:这段代码是我的程序的一部分,原型是:'projectionPoint = X.mul(cos(theta * PI/180))+ Y.mul(sin(theta * PI/180));'@Michael Burdinov –

+0

你可以检查所有的值这个特定像素中的矩阵(R,G,B,x,X,y,Y,投影点和imgGrey)? –

+0

opencv中的ProjectedPoint和imgGrey都和matlab中的一样。@ Michael Burdinov –

相关问题