Time for action – rotating an image and displaying the result
Let's start rotating and displaying the image. Assuming you have solved the problem of the image being in a visible path, you should follow these steps:
- Open the image. Let's use the previous one:
>> img = imread('my_image.bmp');
- Now, rotate the image using
imrotate
. Let's try rotating the image by 90, 180, and 270 degrees, storing each result in a different variable:>> img90 = imrotate(img,90); >> img180 = imrotate(img,180); >> img270 = imrotate(img,270);
- Hopefully, you have typed all commands correctly, so now you should be able to see four different matrices in the Workspace window, each one containing a rotated version of the original image. Now let's try to display all the images in the same window. In the Command Window, type:
>> figure >> subplot(2,2,1) >> imshow(img) >> title('Original Image') >> subplot(2,2,2) >> imshow(img90) >> title('Image Rotated 90 degrees') >> subplot(2,2,3) >> imshow(img180) >> title('Image Rotated 180 degrees') >> subplot(2,2,4) >> imshow(img270) >> title('Image Rotated 270 degrees')
You should now be able to see a window displaying the original (not rotated or rotated by 0 degrees) image and its three aforementioned rotated versions, like the following screenshot:
What just happened?
The process you just performed can be split into two parts; the first part is the image rotation process, which took place in step 2, producing three rotated versions of your original image. The function imrotate
can take two inputs, an image and an angle by which the image will be rotated. The angle need not necessarily be a multiple of 90. It could be any arbitrary angle, between -360 and 360 degrees. Also note that the rotation is performed in a counter-clockwise fashion. If you wish to perform clockwise rotation, you should use a negative angle as a second input to imrotate
.
The second part of the process described previously, is for displaying the produced images. The subplot
command splits the window that opens into m
rows and n
columns and then activates the subwindow that resides in the r-th position. Following this notation, the subplot function should be called as subplot(m,n,r)
. In our example, we used two rows and two columns; hence the first two inputs were equal to two. The third input was changed each time, so that the images displayed after each subplot (using imshow
) would be placed in a different subwindow. Another thing worth noting is that the subwindows in a subplot are numbered in a column-wise fashion (that is, 1 is for the first row-first column, 2 is for the first row-second column, and so on). Finally, for clarity of the displayed information,we have added a title over each displayed image, using the title
function, with the message we want entered as a string input.
Tip
If you want to display your images in different windows, you should replace each subplot(m,n,r)
command with figure. This way, you would end up with four open windows for the example illustrated previously.
Performing image mirroring
In order to perform image mirroring, we will use one of the following functions: fliplr
, flipud
, and flipdim
. If you want to mirror a grayscale image, the first two functions can be used. The first one, fliplr
, is used to mirror an image about its vertical axis. This means that the columns of the image will be interchanged, so that the first column becomes the last and vice versa. Accordingly, flipud
can be used to mirror an image about the horizontal axis. These two functions only work when the input matrix is 2-dimensional (that is, a grayscale image). When we have to deal with color images, we need to use flipdim
because it can also accept a second input declaring the dimension that will be flipped.