[英]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.