opencv cv :: mat没有返回相同的结果

时间:2015-04-01 08:57:01

标签: opencv

    int sizeOfChannel = (_width / 2) * (_height / 2);
    double* channel_gr = new double[sizeOfChannel];

  // filling the data into channel_gr....

    cv::Mat my( _width/2, _height/2, CV_32F,channel_gr);        
    cv::Mat src(_width/2, _height/2, CV_32F);
    for (int i = 0; i < (_width/2) * (_height/2); ++i)
    {
        src.at<float>(i) = channel_gr[i];       
    }
    cv::imshow("src",src);
    cv::imshow("my",my);
    cv::waitKey(0);

我想知道为什么我在我和src imshow中没有获得相同的图像
更新: 我已将我的数组改为double *仍然是相同的结果; 我认为这与步骤有关?
我的图像输出 enter image description here

src图像输出 enter image description here

3 个答案:

答案 0 :(得分:2)

这个对我有用:

int halfWidth = _width/2;
int halfHeight = _height/2;
int sizeOfChannel = halfHeight*halfWidth;

// ******************************* //
// you use CV_321FC1 later so it is single precision float
float* channel_gr = new float[sizeOfChannel];

// filling the data into channel_gr....
for(int i=0; i<sizeOfChannel; ++i) channel_gr[i] = i/(float)sizeOfChannel;



// ******************************* //
// changed row/col ordering, but this shouldnt be important
cv::Mat my( halfHeight , halfWidth , CV_32FC1,channel_gr);        
cv::Mat src(halfHeight , halfWidth, CV_32FC1);


// ******************************* //
// changed from 1D indexing to 2D indexing
for(int y=0; y<src.rows; ++y)
for(int x=0; x<src.cols; ++x)
{
    int arrayPos = y*halfWidth + x;
    // you have a 2D mat so access it in 2D
    src.at<float>(y,x) = channel_gr[arrayPos ];       
}


cv::imshow("src",src);
cv::imshow("my",my);

// check for differences
cv::imshow("diff1 > 0",src-my > 0);
cv::imshow("diff2 > 0",my-src > 0);
cv::waitKey(0);

答案 1 :(得分:1)

'my'是浮点数组,但你给它指向double数组。它无法正确地从这个数组中获取数据。

答案 2 :(得分:0)

您使用的构造函数版本似乎是

 Mat::Mat(int rows, int cols, int type, const Scalar& s)

这是来自OpenCV文档。您似乎正在使用float src并从channel_gr分配(声明为double)。是不是某种形式的精确损失?

相关问题