gpt4 book ai didi

OpenCV:矩阵迭代

转载 作者:太空宇宙 更新时间:2023-11-03 20:42:34 25 4
gpt4 key购买 nike

我是 OpenCV 的新手。我正在尝试使用迭代器而不是“for”循环,这对我来说太慢了。我尝试了一些这样的代码:

MatIterator_<uchar> it, end;
for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
{
//some codes here
}

我的问题是:如何将 for 循环转换为:

for ( int i = 0; i < 500; i ++ )
{
exampleMat.at<int>(i) = srcMat>.at<int>( i +2, i + 3 )
}

进入迭代器模式?也就是说,我怎样才能以迭代器形式执行“i +2,i + 3”?我想只能通过“*it”得到相应的值,但我无法得到它的计数。提前谢谢了。

最佳答案

慢的不是 for 循环而是 exampleMat.at<int>(i)正在进行范围检查。

为了有效地遍历所有像素,您可以使用 .ptr() 在每行的开头获取指向数据的指针

for(int row = 0; row < img.rows; ++row) {
uchar* p = img.ptr(row);
for(int col = 0; col < img.cols; ++col) {
*p++ //points to each pixel value in turn assuming a CV_8UC1 greyscale image
}

or
for(int col = 0; col < img.cols*3; ++col) {
*p++ //points to each pixel B,G,R value in turn assuming a CV_8UC3 color image
}

}

关于OpenCV:矩阵迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11977954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com