Main Content

Specify Fill Values in Geometric Transformation Output

This example shows how to specify the fill values used by imwarp when it performs a geometric transformation.

When you perform a transformation, there are often pixels in the output image that are not part of the original input image. These pixels must be assigned some value, called a fill value. By default, imwarp sets these pixels to zero and they display as black. You can specify a different value using the FillValues name-value argument. If the image being transformed is a grayscale image, specify a scalar value that specifies a shade of gray. If the image being transformed is an RGB image, you can use either a scalar value or a 1-by-3 vector. If you specify a scalar, imwarp uses that shade of gray for each plane of the RGB image. If you specify a 1-by-3 vector, imwarp interprets the values as an RGB color value.

Read a color image into workspace.

rgb = imread("onion.png");

Specify an amount of translation, and create a geometric transformation object that represents a translation transformation.

translation = [15 40];
tform = transltform2d(translation);

Create a 2-D spatial referencing object. This object specifies aspects of the coordinate system of the output space so that the area needing fill values is visible. By default, imwarp sizes the output image to be just large enough to contain the entire transformed image but not the entire output coordinate space.

Rout = imref2d(size(rgb)); 
Rout.XWorldLimits(2) = Rout.XWorldLimits(2)+translation(1); 
Rout.YWorldLimits(2) = Rout.YWorldLimits(2)+translation(2); 
Rout.ImageSize = Rout.ImageSize+translation;

Perform the transformation using the imwarp function.

cb_rgb = imwarp(rgb,tform,"OutputView",Rout);
imshow(cb_rgb)

Now perform the transformation again, this time specifying a fill value.

fillValue = [187 192 57];
cb_fill = imwarp(rgb,tform,"OutputView",Rout,"FillValues",fillValue);
imshow(cb_fill)

See Also

| | |

Related Topics