2015-04-01 58 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數組。沒有辦法可以正確地從這個數組中獲取數據。

相關問題