问题描述
我是OpenCV的新手。我试图使用迭代器而不是for循环,这对我的情况来说太慢了。我试过这样的代码:
MatIterator_< uchar>它,结束;
for(it = I.begin< uchar>(),end = I.end< uchar>(); it!= end; ++ it)
{
// some codes这里
}
我的问题是:如何转换for循环,如: {
exampleMat。 at< int>(i)= srcMat> .at< int>(i + 2,i + 3)
}
进入迭代器模式?也就是说,如何以迭代器形式执行i + 2,i + 3?我只能通过* it得到相应的值,但我无法得到它的计数数字。
非常感谢提前。
这不是for循环,它是 exampleMat.at< int>(i )
正在进行范围检查。
要有效地遍历所有像素,您可以获得指向每行开头的数据的指针with .ptr()
for(int row = 0; row< img.rows; ++ row){
uchar * p = img.ptr(row);
for(int col = 0; col< img.cols; ++ col){
* p ++ //指向每个像素值,假设CV_8UC1灰度图像
}
或
for(int col = 0; col< img.cols * 3; ++ col){
* p ++ //指向每个像素B,G,R值假设CV_8UC3彩色图像
}
}
I am new to OpenCV. I am trying to use iterator instead of "for" loop, which is too slow for my case. I tried some codes like this:
MatIterator_<uchar> it, end;
for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
{
//some codes here
}
My question here is: how can I convert a for loop like:
for ( int i = 0; i < 500; i ++ )
{
exampleMat.at<int>(i) = srcMat>.at<int>( i +2, i + 3 )
}
into iterator mode? That is, how can I do the "i +2, i + 3" in iterator form? I only can get the corresponding value by " *it " I think, but I couldn't get its counting number.Many thanks in advance.
It's not the for loop which is slow it is the exampleMat.at<int>(i)
which is doing range checking.
To efficently loop over all the pixels you can get a pointer to the data at the start of each row with .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:矩阵迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!