2011-10-22 39 views
2

這是cv::Mat訪問所有像素的正確方法:訪問所有像素CV ::墊

for(row = 0; row < mat.rows; ++row) 
    { 
      for (col = 0; col < mat.cols; ++col) 
      { 



      } 
    } 

還是有類似於此公式公式法爲IplImage *

temp_ptr = &((uchar*)(img->imageData + (img->widthStep*pt.x)))[pt.y*3]; 
+1

你知道'Mat'有一個公共領域稱爲'型data''uchar'? – Nawaz

+0

@Nawaz - 是的,先生,我知道這一點,請幫助我,我在這裏掙扎了一下,我知道這是指向數據,但我如何循環像素,我可以得到與rgb值工作與數據但不循環。 –

回答

1

在最好的情況下,所有的像素連續存儲,你應該能夠做到:

uchar* pixel = mat.data; 
for(int i = 0; i < mat.rows * mat.cols; ++i) 
{ 
    // access pixel[0],pixel[1],pixel[2] here 
    pixel += 3; // move to next pixel 
} 

爲了更通用一些,但仍然很快,請查看Mat::isContinuous()提到的sample code。計算元素地址的一般公式可見here(轉載於下方)。

Address calculation