MATLAB: How to rotate an object in an image and make a movie of the consecutive rotations

imageImage Processing Toolboxmovieobjectrotate

I have an application where I would like to rotate an object in an image and make a movie out of it. The object might be an item in a picture. I would like to have an example of how this could be done.

Best Answer

There are some examples of image morphology (object detection) in the Image Processing Toolbox. This should help you make a binary mask for the object, and use it to perform operations on the desired pixels from your image. The demos can be found by typing "ipexindex" (stands for Image Processing Example Index) at the MATLAB prompt.
You will then need to perform some image transformations, specifically, rotations. In the same set of examples listed above, you can find some detailed transformation demos.
After you obtain the desired portion of the image and perform a rotation on it, you will need to insert it back into the original image. This can be done using image arithmetic, IMADD, IMMULTIPLY, etc. Each rotation can be stored in a 4D matrix, and played back as a movie.
Study the following code for an example of this process. At any point in the example, you can call IMSHOW on the image being processed to see exactly what's going on. This has been done once in the example as a demonstration.
% Read image
I = imread('eight.tif');
% Create the image mask by thresholding the image
% in BW so quarter regions have good contrast
BW = im2bw(I,graythresh(I));
% Invert the image so that the quarter regions are
% the "true" part of the mask, that is, the 1's
BW2 = ~BW;
% Close the small black holes in the image mask
% using a structuring element
se = strel('disk',4);
BWclosed = imclose(BW2,se);
% Get a single quarter mask using a region of
% interest around the desired quarter
c = [222 272 300 270 221 194];
r = [21 21 75 121 121 75];
ROI = roipoly(I,c,r);
mask = BWclosed & ROI;
% Here's the quarter mask
figure, imshow(mask)
% Find the center of the quarter mask
% so we can shift the image and mask there
% to perform rotations more easily
[mcx mcy] = find(bwmorph(mask,'shrink',inf));
xshift = size(I,1)/2-mcx;
yshift = size(I,2)/2-mcy;
maskShifted = circshift(mask,[xshift yshift]);
Ishifted = circshift(I,[xshift yshift]);
% Loop through angles of rotation, and store rotations
% First initialize the multiframe image
angle = 15;
multiImage = zeros([size(I) 1 floor(360/angle)]);
for n = 1:(360/angle)
theta = n*angle;
% Perform the rotations
maskRot = imrotate(maskShifted,theta,'nearest','crop');
Irot = imrotate(Ishifted,theta,'bilinear','crop');
% Snag the quarter from the rotated image using the rotated mask
justQuarter = immultiply(maskRot,Irot);
% Insert into multiframe image
% First erase the existing data we are replacing
erasedQuarter = immultiply(~maskRot,Ishifted);
% Then insert the new rotated quarter data
multiImage(:,:,1,n) = imadd(erasedQuarter,justQuarter);
end
% Shift back to the original position
multiImage = circshift(multiImage,[-xshift -yshift]);
% Make the movie (will simultaneously play the movie once slow)
figure('doublebuffer','on')
mov = immovie(multiImage,gray(256));
% Play the movie (will play fast once, and then at specified FPS)
movie(mov,10,40);
For more information, see the index of examples referenced above, as well as the documentation on each of the commands in the example.
NOTE: This is only one method for performing this type of operation. As the image type, image data, and application vary, you will need to utilize other algorithms and functions in the Image Processing Toolbox that are more specific to the application.