Time for action – cropping and resizing an image, then saving it as BMP
To get a better grasp of the three functions we presented in the previous three sections, we can use the example of a very common image processing drill, which is to select and crop a part of an image we would like to keep and then resize it to our desired dimensions and save it as a new image. We'll use our previous image as a start, so our first move is to load it. The whole process is described in the following steps:
- Load the image using
imread
and save it in the variableimg
:>> img = imread('my_image.bmp');
- Crop the image using
imcrop
and save the result in the variablecropped
:>> cropped = imcrop(img);
- Now, resize the resulting image to double its size using
imresize
and display it usingimshow
:>> cropped2 = imresize(cropped,2); >> imshow(cropped2);
- Now, let's see the sizes of the two images from steps 2 and 3 using
size
:>> size(cropped) ans= 114 196 >> size(cropped2) ans= 228 392
As we see, the sizes are OK (cropped2
is double the size of cropped
, 114 x 196 versus 228 x 392). Now, we can save our final image as a BMP using imwrite
:
>> imwrite(cropped2,'cropped_image.bmp','BMP');
You should now be able to see the resulting BMP image in the Current Folder window.
What just happened?
Well, you just performed one of the most common processes in the everyday life of an amateur photographer. You started by loading your image into the workspace of MATLAB in step 1, proceeded with selecting and cropping a rectangular area of your choice in step 2, went on to resize it to double its original size and check the resulting image size, and finally, saved your cropped and resized result to a BMP image file. Now, you are ready to move on to harder tasks.
Have a go hero – tailoring an image to suit your needs
Let's now suppose that we would like to process a photo from our holidays, by rotating it 90 degrees to have the proper orientations, cropping a specific area we want to keep, rotating this area as much as needed so that it is not crooked, and then resize it to 360 rows (say, we want to fit it in a specific space with 360 pixels of height). At the end, we would like to save our result in high quality JPEG, embedding a comment that reads I just finished Chapter 1
. How would you accomplish all these things?
First of all, don't panic! All these steps have already been covered in this chapter, so it's just a matter of using the right functions in the right order.
You should start with the first and easiest step, which is loading and displaying our photograph into the Workspace window. The result would look something like the following screenshot:
Now that your photo has been saved in a variable, you can rotate it so that it has the correct orientation. Then, use imcrop
and select the sunny area of the image with the mouse.
However, the resulting image is tilted! So, let's try to fix it. Here comes the trial and error process. You must try the various possible small angle values in imrotate
, so that you find the result you are happy with. This image should look fine if you rotate it clockwise by an angle of about 5 degrees. Optionally, we can display our steps in the same figure and get the following result:
As you can see from the last image, while our final image is not tilted, it has black borders caused by the rotation by an angle that is not a multiple of 90. This means that you need yet another cropping step, to keep the image part that we actually want. When you have edited the image to your liking, proceed in resizing the image using imresize
, and declare the number of rows you want the image to have in the second input. Finally, you can save the image in the predefined format using imwrite
.
Tip
The default method for rotation used in imrotate
is nearest neighbor. This method produces rather suboptimal results, when rotating an image by an angle that is not a multiple of 90 degrees. If you want to produce a better result, you could try entering a different method as a third input, like this:
>> img90c2 = imrotate(img90c,-5, 'bicubic');
Pop quiz – image processing in MATLAB
Q1. MATLAB is a very good choice for image processing purposes for various reasons. Try to answer which of the following reasons are valid:
- MATLAB represents images as matrices and treats most variables as such.
- MATLAB is open source and can be used by anyone.
- MATLAB has a set of toolboxes that offer a variety of powerful and useful tools for image manipulation.
- Function
imrotate
rotates an image in a clockwise function.