Repository: opencv
Issue: Some basic operations on image are slow
Labels: auto-transferred,feature,priority: low,category: core

Transferred from http://code.opencv.org/issues/2648

```
|| Michael Burdinov on 2012-12-19 14:04
|| Priority: Low
|| Affected: None
|| Category: core
|| Tracker: Feature
|| Difficulty: None
|| PR: 
|| Platform: None / None
```
## Some basic operations on image are slow

```
Basic operations that works with single image are much slower than similar operations that works with multiple images.

For example addWeighted() function is about 3.5 times faster than multiplying image by scalar. This is extremely weird, since addWeighted performs two multiplications of images by scalars.

Adding scalar to image that should be at least as fast as adding two images, is actually 5 times slower.

Applying abs() to image is 2.5 slower than simple loop over pixels that apply abs() to each pixel.

And so on...

Tests were performed with 1000x1000 8-bit image. I used OpenCV 2.4.2. Tested systems: Windows XP service pack 2 Visual Studio 2005 and Windows XP service pack 3 Visual Studio 2010 (results are not the same but similar).
```
## History
##### Vadim Pisarevsky on 2012-12-19 19:02

```
abs() may take allocation of a temporary image when it's used as a part of complex expression. absdiff is more appropriate function if you want better performance. Besides, for 8-bit unsigned image abs(x) == x, right? In general, basic functions are added for completeness; for top performance you can still use manually-written loops.
-   Target version set to 3.0
-   Priority changed from Normal to Low
-   Tracker changed from Bug to Feature
```
##### Michael Burdinov on 2012-12-20 11:50

```
Vadim Pisarevsky wrote:
> abs() may take allocation of a temporary image when it's used as a part of complex expression. absdiff is more appropriate function if you want better performance. Besides, for 8-bit unsigned image abs(x) == x, right? In general, basic functions are added for completeness; for top performance you can still use manually-written loops.

1) When function that performs certain operation twice is few times faster than function that performs the same operation once, something inside definitely went wrong. I don't know whether the problem is some flags, or un-required memory allocations, or anything else.
2) OpenCV is known for highly optimized low level operations with images. I remember that in the past high performance was achieved by use of IPP. Later IPP was replaced with SSE2, TBB and such. But claims that those operations are very efficient remained. Anyway it is weird that simple loop without any optimizations at all is much faster than existing function. This is especially problematic when this happens in most basic functions, that used countless times by any OpenCV user. Improving performance of those function should have high impact on performance of almost any code that uses OpenCV.
3) abs() wasn't used as part of any complex expression so there shouldn't be any temporary images. Also from what I see in online documentation "abs(A) is equivalent to absdiff(A, Scalar::all(0), C)". This means that they are expected to have same time performance. And for 8-bit signed image abs(x) is not always equal to x.
```
##### Michael Burdinov on 2012-12-30 07:49

```
And just for the record: absdiff( image1, Scalar, image1) is 5 times slower than absdiff( image1, image2, image1). So the problem exists in absdiff as well.
```