Motion blur¶
| Fact | Value |
|---|---|
| Status | Implemented and tested |
| Family | Linear spatial filters |
| Engine | Browser |
| Type | Linear, spatial, convolution |
| Output | Image |
| Operator shown as | dynamic-kernel (exact) |
| Support radius | ceil((length / 2) * max(|cos(angle)|, |sin(angle)|) - 0.5) |
| Code | web/src/core/filters/linear/motion-blur.ts, motionBlurFilter |
| Operator code | web/src/core/filters/linear/motion-blur.ts, motionKernel |
| Channels | RGB, Brightness only, Color only |
Overview¶
Motion blur smears the image along a straight line, as if the camera moved during the exposure. The kernel is the path of one bright point, so the Inspector shows exactly what each point becomes.
How it works¶
- Draw a short line through the pixel, with the chosen length and angle.
- Give each neighbor a weight by how much of the line passes through it.
- Multiply each neighbor by its weight and add the results.
- Write the sum as the new pixel value.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input channel value
- \(I_{\text{out}}\): Output channel value
- \(M(i, j)\): Kernel weight at offset i to the right and j down
- \(\ell(i, j)\): Length of the line segment inside the unit square of cell (i, j)
- \(L\): Length of the motion in pixels
- \(\theta\): Angle of the motion, counterclockwise from the x axis
- \(r\): Kernel radius in pixels, the smallest one that holds the whole segment
The kernel is a line segment of length L through the center of the window, at angle theta. The weight of each cell is the length of the segment that passes through that cell's square, divided by L. The weights add up to 1. A slanted line gets smooth, anti-aliased weights instead of a jagged staircase.
A camera that moves at constant speed in a straight line spreads each point of light evenly along that line. The kernel is this spread, the point spread function. PixelNexus computes the weight of each kernel cell as the exact length of the line inside the cell's square, divided by the total length. The clipping uses the method of Liang and Barsky. A line through a cell corner gives that cell no weight.
Kernel or operator¶
Two kernels from the filter code: a horizontal line of length 5, and a diagonal line of length 4 at 45 degrees. The diagonal passes through cell corners, so only the three cells on the diagonal get weight.
Algorithm¶
- Find the smallest square kernel that holds a segment of length L at angle theta.
- For each cell, clip the segment to the cell's square and measure the length inside.
- Divide every length by the total, so the weights add up to 1.
- Pad the image with the border mode and correlate it with the kernel.
- Round to 8 bits. Keep alpha unchanged.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Length length |
9 | 1 to 101 px | Length of the motion, in pixels. |
Angle angle |
0 | 0 to 180 deg | Direction of the motion. 0 is horizontal and 90 is vertical. Angles turn counterclockwise. |
Border mode border |
Reflect 101 (default) | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Channels channelScope |
RGB | RGB, Brightness only, Color only | Which part of the color image the filter changes. |
Linear light linearLight |
off | On or off | Blur linear light values instead of sRGB encoded values. |
Implementation¶
motionKernel builds the kernel. The filter correlates the image with it and skips zero weights. The support radius is the smallest one that holds the whole line, so a horizontal line of length 9 needs a radius of 4. The filter runs in the browser engine, in Web Workers. The studio splits the image into strips. Each strip gets a halo as wide as the support radius, so the strips give exactly the pixels of a whole-image run. The shared filter framework in web/src/core/filters/run.ts handles the border mode, the channel scope, alpha and the final rounding to 8 bits.
Worked example¶
With length 3 and angle 0 the kernel is a row of three equal weights. For the pixel with value 90, between 60 and 150, the result is (60 + 90 + 150) / 3 = 100.
Complexity¶
- Time: \(O(N\,k)\)
- Memory: \(O(N)\)
N is the number of pixels per channel and k is the number of nonzero kernel weights, about L to 2L. Zero weights are skipped.
Artifacts and limitations¶
Limitations
- Models straight motion at constant speed only. Real camera shake follows curved paths.
- Other programs build motion kernels in other ways, so their results can differ slightly.
Artifacts
- Bright points become streaks with sharp ends.
- Edges across the motion stay sharp, while edges along it blur.
Tests¶
The kernel is compared with an independent Python version of the same definition for six lengths and angles. The filter output is compared with scipy.ndimage.correlate using that kernel, for five settings and three border modes, within 1e-4 relative to the value.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 5.
- Liang, Y.-D. and Barsky, B. A. A new concept and method for line clipping. ACM Transactions on Graphics, 3(1), 1984. DOI 10.1145/357332.357333.
- SciPy documentation, scipy.ndimage.correlate. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.correlate.html