在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()。
其中編程語言? C++? – Micka
Ya我想在C++中實現我編輯了這個問題:) – kadu
2個外部循環實現了什麼?結果只存儲在m = 20和n = 20中。 – user664303