2014-02-19 73 views
0

我想要實現在開放cv.This代碼類似環在Matlab。至於做我新開cv.I不知道如何proceed.Can任何人給我的想法做這在C++在open cv中實現For循環?

for m=1:10 
for n=1:20 
    for l=1:Ns 
     for k=1:Ns 
      Y(l,k)=image1(m-Ns+l-1,n-Ns+k-1); 
      DD(l,k)=image2(m-Ns+l-1,n-Ns+k-1);    
     end 
    end 
    e=Y-DD ;  
end 
end 

這裏Image1和image2的尺寸是300 * 300像素。 Y,DD,image1,image2al是mat圖像。

+0

其中編程語言? C++? – Micka

+0

Ya我想在C++中實現我編輯了這個問題:) – kadu

+0

2個外部循環實現了什麼?結果只存儲在m = 20和n = 20中。 – user664303

回答

0

在OpenCV中,圖像可以表示爲MatIplImage。你的問題沒有指定圖像的類型。

如果是IplImage:

IplImage *img; 
unsigned char *image = (unsigned char*)(img->imageData); 
int imageStride = img->widthStep; 
pixData = image[xCount + yCount*imageStride]; 

如果太:

Mat img; 
unsigned char *image = (unsigned char*)(img.data); 
int imageStride = img.step; 
pixData = image[xCount + yCount*imageStride]; 

pixData將包含在(xCount, yCount)數據。你可以在for循環中使用這種理解。

正如你已經知道的邏輯,我只提到如何訪問圖像中特定點的數據。

0

在OpenCV的最有效的方法來訪問像素的for循環:

cv::Mat rgbImage; 
cv::Mat grayImage; 
for (int i = 0; i < rgbImage.rows; ++i) 
    { 
    const uint8_t* rowRgbI = rgbImage.ptr<uint8_t> (i); 
    const uint8_t* rowGrayI = grayImage.ptr<uint8_t> (i); 
    for (int j = 0; j < rgbImage.cols; ++j) 
     { 
     uint8_t redChannel = *rowRgbI++; 
     uint8_t greenChannel = *rowRgbI++; 
     uint8_t blueChannel = *rowRgbI++; 

     uint8_t grayChannel = *rowGrayI++ 
     } 
    } 

根據您圖像是否是一個或多個通道,你可以修改上面的代碼。

如果你想實現窗口滑動,你可以做這樣的事情:

cv::Mat img; 
int windowWidth = 5; 
int windowHeight = 5; 
for (int i = 0; i < img.rows - windowHeight; ++i) 
    { 
    for (int j = 0; j < img.cols - winddowWidth; ++j) 
     { 
     // either this 
     cv::Mat currentWindow = img(cv::Range(j, i), cv::Range(j + windowWidth, i + windowHeight)); 
     // perform some operations on the currentWindow 

     // or do this 
     getRectSubPix(img, cv::Size(windowWidth, windowHeight), cv::Point2f(j, i), currentWindow)); 
     // perform some operations on the currentWindow 
     } 
    } 

你可以閱讀更多關於getRectSubPix()

+0

感謝您的答案。我的意圖是,我需要從每個映像中取出第一個5 * 5塊並執行操作。那接下來的5 * 5像素等等。 – kadu