在开放 cv 中实现 For 循环
Implementing For loop in open cv?
我想在 open cv 中实现类似的循环。由于我是打开cv的新手,我不知道如何进行。任何人都可以给我想法来做到这一点C++
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
这里的图像 1 和图像 2 的大小为 300*300 像素。Y ,DD,image1,image2 al 是垫子图像。
在OpenCV中,图像可以表示为Mat
或IplImage
。您的问题未指定图像的类型。
如果 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 循环中使用这种理解。
正如您已经知道的逻辑一样,我只提到如何从图像中的特定点访问数据。
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()的更多信息。
相关文章:
- 如果没有malloc,链表实现将失败
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- 如何在C++中从两个单独的for循环中添加两个数组
- 如何在c++中实现处理器调度模拟器
- 在Linux for Windows上编译C++代码时出错
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 调用专用模板时出错"no matching function for call to [...]"
- 为什么我的for循环不能正确获取argv
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 实现基于链表的堆栈的基于范围的 for 循环
- 避免在基于反向范围的for循环实现中悬挂参考
- 在C 中实现迭代自相关过程,使用for循环
- 在开放 cv 中实现 For 循环
- 如何创建Google+Plus一键,同时已经使用GooglePlay游戏服务SDK for C++实现了排行榜
- 优化代码/实现"for"循环而不是长"if - else if - else"
- 如何使用CUDA实现2-for粒子交互循环,以及由此产生的复杂性是什么
- C for 循环的实现方式与其他语言不同