Qt (linear) blur

Is there a simple solution to add motion blur to the image in Qt? Didn't find a good lesson on blur. I need something really simple that I can understand, and it would be very nice if I could change the blur.

+4
source share
1 answer

Qt does not have a motion blur filter. I looked at the QGraphicsBlurEffect code; it uses QPixmapBlurEffect , which itself uses an internal helper method called expblur (exponential blur).

expblur itself uses the one-dimensional blur effect (motion blur in the X direction, qt_blurrow method) twice. It rotates the image between the two blurs 90 degrees, and then rotates it back.

So, Qt has a motion blur effect, but this is only internal. Therefore, you need to write your own effect. To do this, check out the qt_blurrrow code, which you can find in the Qt sources in src/gui/qpixmapfilter.cpp . This will give you good quality for the filter, as it is an exponential blur filter instead of a linear one.

If you don't want to delve into the Qt source code, grab this pseudocode at the beginning:

 foreach pixel (x, y) in image { for dx = -r to r with x+dx within image { for dy = -r to r with y+dy within image { Add pixel (x+dx, y+dy) of the source image with ↩ factor (matrix[dx, dy]) to the target image. } } } 

where the matrix can be defined as follows (for horizontal motion blur with a radius of 2):

 0 0 0 0 0 0 0 0 0 0 0.1 0.2 0.4 0.2 0.1 0 0 0 0 0 0 0 0 0 0 

Please note that the sum of all entries must be 1 or you need to divide the colors by the sum of the entries in the matrix.

The construction of this matrix for a given radius r and angle α is difficult if you want to allow arbitrary angles for α (not only 90 degrees steps). [EDIT: see Comment 3 for easy creation of such a matrix.]

+2
source

Source: https://habr.com/ru/post/1413404/


All Articles