2015-04-01 57 views
0
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
更新: 我已经改变了我的阵列为双*还是一样的结果; 我认为这与步骤有关吗?
我的图像输出 enter image description hereOpenCV的CV ::垫不返回相同的结果

SRC图像输出 enter image description here

+1

也许是因为channel_gr是一个int数组,而不是一个float数组。 – isarandi 2015-04-01 09:12:04

+0

使用CV_64F怎么样?或使用int数组的问题可能是? – ha9u63ar 2015-04-01 10:15:35

+0

@ ha9u63ar异常被抛出 – Gilad 2015-04-01 10:17:48

回答

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

谢谢,你认为问题只是我的channel_gr?哪些需要浮动而不是双倍? – Gilad 2015-04-01 12:13:44

+0

可能。但是如果src不是连续的,你将不得不考虑'.step'值来访问正确的像素位置,如果你使用一维索引! – Micka 2015-04-01 12:15:13

+0

已标记为每个地区已更改什么样的事情。最重要的部分将是float-instead-of-double数组。 row-vs-col可能或可能不重要。如果矩阵不是连续的,1D-vs-2D索引应该只是一个问题,但可能应该关心稳定的代码。例如。如果你使用IPP支持,我猜矩阵不会一直持续。 – Micka 2015-04-01 13:47:03

0

看来,您正在使用的构造版本

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

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

+0

是的,这是我知道的一个秘密损失。并没有问题 – Gilad 2015-04-01 10:28:48

1

'my'是浮点数组,但是您将它指向double数组。没有办法可以正确地从这个数组中获取数据。

相关问题