Search code examples
matlabrotationimage-rotation

Can we rotate an image in MATLAB filled with background color of original image?


By default, MATLAB function imrotate rotate image with black color filled in rotated portion. See this, http://in.mathworks.com/help/examples/images_product/RotationFitgeotransExample_02.png

We can have rotated image with white background also.

Question is, Can we rotate an image (with or without using imrotate) filled with background of original image?

Specific to my problem: Colored image with very small angle of rotation (<=5 deg.)


Solution

  • Here's a naive approach, where we simply apply the same rotation to a mask and take only the parts of the rotated image, that correspond to the transformed mask. Then we just superimpose these pixels on the original image. I ignore possible blending on the boundary.

    A = imread('cameraman.tif');
    angle = 10;
    T = @(I) imrotate(I,angle,'bilinear','crop');
    %// Apply transformation
    TA = T(A);
    mask = T(ones(size(A)))==1;
    A(mask) = TA(mask);
    %%// Show image
    imshow(A);
    

    Cameraman rotated