Example Simulate Motion Blur

From BoofCV
Revision as of 20:25, 8 October 2021 by Peter (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Simulates linear motion blur by creating a PSF. This is useful when you want to generate a test set of synthetic images under blurred conditions.

Example Code:

Concepts:

  • Convolution
  • Simulation

Relevant Examples:

Example Code

/**
 * Shows you how to simulate motion blur in an image and what the results look like.
 *
 * @author Peter Abeles
 */
public class ExampleSimulateMotionBlur {
	public static void main( String[] args ) {
		// Load a chessboard image since it's easy to see blur with it
		GrayF32 image = UtilImageIO.loadImage(UtilIO.fileExample("calibration/mono/Sony_DSC-HX5V_Chess/frame03.jpg"), true, ImageType.SB_F32);
		GrayF32 blurred = image.createSameShape();

		var panel = new ListDisplayPanel();

		for (int degrees : new int[]{0, 25, -75}) {
			double radians = UtilAngle.degreeToRadian(degrees);
			for (double lengthOfMotion : new double[]{5, 15, 30}) {
				// Create the PSF (i.e. Kernel) that models motion blur with constant velocity
				// radians is the motion blur's direction
				Kernel2D_F32 kernel = MotionBlurOps.linearMotionPsf(lengthOfMotion, radians);
				GConvolveImageOps.convolve(kernel, image, blurred, BorderType.EXTENDED);

				// Visualize results
				BufferedImage visualized = ConvertBufferedImage.convertTo(blurred, null);
				panel.addImage(visualized, String.format("linear: angle=%d motion=%.0f", degrees, lengthOfMotion));
			}
		}

		ShowImages.showWindow(panel, "Simulated Motion Blur", true);
	}
}