# Work with operations pixel by pixel and loops

Hello All.

I heard in this forum before that it isnt recomended work or make an operation pixel by pixel and its more convenient use a function defined in opencv headers and librarys. But that opencv's functions have to work pixel by pixel or there is another way?

For example in this post.

I needed to copy a half image and first I did it with a for().

Why is not recomended use a loop?

Thanks in advance. its a theorical question.

edit retag close merge delete

copy half an image :use a rect :

        Mat mat = (Mat_<uchar>(2, 4) << 5, 6, 0, 4, 0, 1, 9, 9);
Mat mat2;
cout << "mat =" << mat <<  endl;
mat(Rect(0, 0, 4, 1)).copyTo(mat2);
cout << "mat2 =" << mat2 << endl;


results :

mat =[  5,   6,   0,   4;
0,   1,   9,   9]
mat2 =[  5,   6,   0,   4]


Many functions are optimized with a parallel loop or opencl but not all functions. Sometimes pixel loop are faster than opencv function. If you find this case write a post then somebody will make a Pull request to improve function or better do PR yourself you are welcome

( 2018-04-07 03:31:22 -0600 )edit

Thanks for the reply. I will check the opencv function but for some proccess I like to use my own loops. Its more easy and faster for me than go to documentation and check the function parameters and Id like to perform pthreads by myselft. Thanks a lot.

( 2018-04-07 18:41:56 -0600 )edit

Sort by ยป oldest newest most voted

In my personal view, there are a lot of parallel computations at the bottom, if you use built-in function in opencv. For instance, numpy is used in opencv-python. Which is quite faster than operation pixel by pixel. You can simply compare the time of copying an image pixel by pixel and that by opencv.

more

( 2018-04-07 18:42:06 -0600 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Seen: 211 times

Last updated: Apr 07 '18