在opencv(c++)中,在所有像素上运行方法的最快方法是什么
what is the fastest way to run a method on all pixels in opencv (c++)
我在opencv中的每个像素上都有几个任务要做。我正在使用这样一个结构:
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 }
}
这是有效的,但我想知道是否有更快的方法可以做到这一点?该解决方案不使用任何SIMD或OpenCV的任何并行处理。
在opencv中对图像的所有像素运行方法的最佳方式是什么?
如果Mat
是连续的,即矩阵元素连续存储,每行末尾没有间隙,可以使用Mat::isContinuous()
引用,则可以将它们视为长行。因此,你可以这样做:
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
}
正如文档中所说,这种方法虽然非常简单,但可以将简单元素操作的性能提高10-20%,尤其是在图像很小且操作非常简单的情况下。
PS:为了更快的需求,你需要充分利用GPU来并行处理每个像素。
相关文章:
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 使用基类中的派生方法运行线程,而无需使用模板
- 如何在 C 中使用空的 main() 方法运行函数?
- 如何同时运行 2 种方法
- 使用 c 或 c++ 运行本地服务器的最佳方法
- 类方法 - 数据结构中 For 循环的运行时错误
- 使用本机 JNI 静态方法实现C++ Java 运行时错误
- C ++中的方法覆盖:是编译时还是运行时多态性?
- 还有其他方法可以为乘法表编写循环以获取运行时值吗?
- 如何在长时间运行的方法中等待信号?
- 为什么以相同的数量插入到集合中,基于不同的方法具有不同的运行时间?
- 在另一个 QThread 上运行成员方法时,无法将事件发送到其他线程拥有的对象
- 是否有一种方法可以在运行时停止循环重复一次不止一次
- 是否有一种可接受的运行线程或按顺序执行的方法
- Leetcode:为什么在提交和运行代码上,字符串类的“at”方法的行为不同
- 是否有任何可能的方法将事件(自定义)附加到在运行时创建的对象?[C 构建器]
- MFC多线程程序可以以并行方法运行
- QTGraphicsView从主方法运行到外部后立即关闭
- QtConcurrent::使用重载静态方法运行
- 编译器本身使用并行计算方法运行