简体   繁体   English

在opencv(c ++)中的所有像素上运行方法的最快方法是什么

[英]what is the fastest way to run a method on all pixels in opencv (c++)

I have several tasks to do on each pixel in opencv. 我在opencv中的每个像素上有几个任务要做。 I am using a construct like this: 我正在使用这样的构造:

for(int row = 0; row < inputImage.rows; ++row)
    {
        uchar* p = inputImage.ptr(row);
        for(int col = 0; col < inputImage.cols*3; col+=3)
        {
            int blue=*(p+col);  //points to each pixel B,G,R value in turn assuming a CV_8UC3 colour image
            int green=*(p+col+1);
            int red=*(p+col+2);
            // process pixel            }

    }

This is working, but I am wondering if there is any faster way to do this? 这是可行的,但是我想知道是否有更快的方法可以做到这一点? This solution doesn't use any SIMD or any paralle processing of OpenCV. 此解决方案不使用任何SIMD或OpenCV的任何并行处理。

What is the best way to run a method over all pixels of an image in opencv? 在opencv中对图像的所有像素运行方法的最佳方法是什么?

If the Mat is continuous, ie the matrix elements are stored continuously without gaps at the end of each row, which can be referred using Mat::isContinuous() , you can treat them as a long row. 如果Mat是连续的,即矩阵元素在每行的末尾连续存储而没有间隙,可以使用Mat::isContinuous()进行引用,则可以将它们视为长行。 Thus you can do something like this: 因此,您可以执行以下操作:

const uchar *ptr = inputImage.ptr<uchar>(0);

for (size_t i=0; i<inputImage.rows*inputImage.cols; ++i){
    int blue  = ptr[3*i];
    int green = ptr[3*i+1];
    int red   = ptr[3*i+2];

    // process pixel 
}

As said in the documentation , this approach, while being very simple, can boost the performance of a simple element-operation by 10-20 percents , especially if the image is rather small and the operation is quite simple. 文档中所述,这种方法虽然非常简单,但可以将简单元素操作的性能提高10%到20% ,尤其是在图像很小且操作非常简单的情况下。

PS: For faster need, you will need to take full use of GPU to process each pixel in parallel. PS:为了更快地需要,您将需要充分利用GPU来并行处理每个像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenCV / C ++ - 编辑所有孤立的像素 - OpenCV / C++ - Edit all isolated pixels 在C ++中的SQL Server上运行选择查询的最快方法是什么? - What is the fastest way to run a select query on SQL Server in C++? 在 C++ 中转置矩阵的最快方法是什么? - What is the fastest way to transpose a matrix in C++? 在OpenCV C ++中将图像的所有白色像素更改为透明 - Change all white pixels of image to transparent in OpenCV C++ 在C ++中将数据输出到磁盘的最快方法是什么? - All things equal what is the fastest way to output data to disk in C++? 在 c++ 中的模式匹配之前删除一行中所有字符的最快方法是什么? - What is the fastest way to remove all characters in a line up until a pattern match in c++? C ++-在地图中搜索具有相同值的所有键的最快方法是什么? - C++ - What is the fastest way to search for all the keys with same value in a map? 在C ++中,用另一个字符串替换字符串中所有出现的子字符串的最快方法是什么? - In C++, what's the fastest way to replace all occurrences of a substring within a string with another string? 在 C++ 中,从大型文本文件集合中读取所有单词的最快方法是什么? - In C++ what is the fastest way to read all the words from a collection of large text files? 在 Python 中使用 C/C++ 的最快方法是什么? - What's the fastest way to use C/C++ in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM