Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Multimedia Computing Lab. Ruaa Hussam Al-Mallah/Lecturer Asst.

Computer Technical Eng. Dept.


4th Class

Experiment No.7
Rotating and Cropping of images
Objective:
 To learn rotate and crop image.
Introduction:
 Image rotate
To rotate an image, use the imrotate function. When you rotate an image, you specify
the image to be rotated and the rotation angle, in degrees. If you specify a positive
rotation angle, imrotate rotates the image counterclockwise; if you specify a negative
rotation angle, imrotate rotates the image clockwise.
B = imrotate(A,ANGLE)
 Cropping an Image
To extract a rectangular portion of an image, use the imcrop function. Using imcrop, you
can specify the crop region interactively using the mouse or programmatically by
specifying the size and position of the crop region.
I2 = imcrop(I)

Procedure:
This example rotates an image 35° counterclockwise:
%ex5.m
clear all;
clc;
I = imread(‘babby.jpg');
size(I); % 812*812
J = imrotate(I,35);
size(J); % 1133*1133

subplot(121),imshow(I),title('Original');
subplot(122),imshow(J),title('Rotatated by 35');

By default, imrotate creates an output image large enough to include the entire original

EXP. No. 7: Rotating and Cropping of images Page 27


Multimedia Computing Lab. Ruaa Hussam Al-Mallah/Lecturer Asst.
Computer Technical Eng. Dept.
4th Class

image. Pixels that fall outside the boundaries of the original image are set to 0 and
appear as a black background in the output image. You can, however, specify that the
output image be the same size as the input image, using the 'crop' argument.

clc
clear all;
I = imread(babby.jpg');
size(I); % 812*812
J = imrotate(I,35,'crop');
size(J); % 812*812
subplot(121),imshow(I),title('Original');
subplot(122),imshow(J),title('Rotatated by 35 croped');

Output:

For example:
%ex7.m
clear all;
clc;
close all;
I = imread('Lab7_6.jpg');
J = imrotate(I,35);
J2= imcrop(J);
subplot(131),imshow(I),title('Original');
subplot(132),imshow(J),title('Rotatated by 35');
subplot(133),imshow(J2),title('Rotatated by 35 croped');

EXP. No. 7: Rotating and Cropping of images Page 28


Multimedia Computing Lab. Ruaa Hussam Al-Mallah/Lecturer Asst.
Computer Technical Eng. Dept.
4th Class

Define the region then click Crop Image.

You can choose Copy Position, this will give you the position to crop and you can use it
in your code, for example our cropped region from previous example is as follows:

I = imread(babby.jpg'); J
= imrotate(I,35);
J2= imcrop(J,[214.5 325.5 681 468]);
subplot(131),imshow(I),title('Original');
subplot(132),imshow(J),title('Rotatated by 35');
subplot(133),imshow(J2),title('Rotatated by 35 croped');

Report

1) Write a matlab code to implement the following function, do not use imrotate or rot90
functions:
B = imrotate(A,90)

Use cameraman image

EXP. No. 7: Rotating and Cropping of images Page 29

You might also like