2014-07-01 94 views
1

INT陣列我分裂我的IMG到3個獨立的墊這樣的:保存墊入的OpenCV

std::vector<Mat> planes(3); 
cv::split(img, planes); 
cv::Mat R = planes[2]; 
cv::Mat G = planes[1]; 
cv::Mat B = planes[0]; 

現在我想存儲這些R,在三個不同的陣列G和燒烤值。財產以後這樣的: 例如用於R.

std::vector<Mat> planes(3); 
cv::split(img, planes); 
cv::Mat R = planes[2]; 
int r[20]; 

for (i=0 ; i<20 ; i++) 

{ 

r[i]= R[i]; 

} 

我知道這會給錯誤。那麼我如何正確實現這個功能呢?

回答

1

這裏是你如何可以對R做到這一點(有明顯擴展到B & G)

std::vector<Mat> planes(3); 
cv::split(img, planes); 
cv::Mat R; 

// change the type from uchar to int 
planes[2].convertTo(R, CV_32SC1); 

// get a pointer to the first row 
int* r = R.ptr<int>(0); 

// iterate of all data (R has to be continuous 
// with no row padding to do it like this) 
for (i = 0 ; i < R.rows * R.cols; ++i) 
{ // you have to write the following :-) 
    your_code(r[i]); 
} 
+0

這會給我第一行的像素值(紅色),對吧? – user2799508

+0

是的,但據我可以從cv :: split()內部看到,第二行將從沒有填充的第一個圖像繼續。你可以使用'R.isContinuous()'來檢查這是否爲真。因此'r'不僅僅是一個指向第一行的指針,它是一個指向整個圖像數據的指針。一般來說,我不會像這樣公開圖像的結構,沒有很好的理由。 – Bull

2

你幾乎有:

std::vector<Mat> planes(3); 
cv::split(img, planes); 
cv::Mat R = planes[2]; 
int r[20]; 
unsigned char *Rbuff = R.data; 
for (i=0 ; i<20 ; i++) 

{ 

r[i]= (int)Rbuff[i]; 

} 
+0

使用'的memcpy()',而不是deferencing每個像素更安全,因爲它可以避免某些體系結構中的「錯誤對齊」錯誤。 –

+1

@KeillRandor,對於這個答案,像素數據是uchars,他們怎麼會錯位?對於我的答案,像素數據是ints,OpenCV在16個字節邊界上分配緩衝區,所以它們也不會錯位。使用'memcpy()'進行類型轉換也有點困難。在哪個架構上這不起作用? – Bull