Sunday, March 20, 2016

Perform Arithmetic operations on an Image

Exercise 1) Write a MATLAB program to perform the following.
1)     Read cameraman.tif and rice.png image and display the image
2)     Add the two images and display.
3)     Subtract the two images and display
4)     Add a constant value of 50 to one of the images and display
5)     Subtract a constant value 100 from one of the images and display.
6)     Obtain negative of one of the image. (Subtract 255 from the image) and display.
Create a binary image of size 256 x 256 as shown below and save as ‘mask.tif 


                8)Perform I= uint8(I1).*uint8(M)  ,where I1 is cameraman.tif and M is mask.tif 
                    Images.

clear
close all;

a=imread('cameraman.tif');
b=imread('rice.png');
c=a+b;                  % addition
d=a-b;                  % subtraction
e=a.*b;                 % multiplication
f=b+50;                % adding a constant
g=a-100;              % subtracting a constant
h=255-b;              % negative of a image

%% creating a mask and saving it
i=zeros([256 256]);
j = 64:192;
k = 64:192;
i(j,k) = i(j,k)+1;
imwrite(i,'mask.tif');

l=uint8(a).*uint8(i);
subplot(331),imshow(a),title('Image 1')
subplot(332),imshow(b),title('Image 2')
subplot(333),imshow(c),title('Addition')
subplot(334),imshow(d),title('Subtraction')
subplot(335),imshow(e),title('Multiplication')
subplot(336),imshow(f),title('Adding a constant(50) to Image 2')
subplot(337),imshow(g),title('Subtracting a constant(100) from Image 1')
subplot(338),imshow(h),title('Negative of Image 2')
subplot(339),imshow(l),title('Masking of Image 1')

Fig 1. Arithmetic operations on image using code


Exercise 2) Write the above program using imadd() function for addition, imsubtract() for Subtraction, immultiply() for multiplication operations. Use imcomplement() function to get complement of an image.
clear
close all;

a=imread('cameraman.tif');
b=imread('rice.png');

c=imadd(a,b);                   % addition
d=imsubtract(a,b);           % subtraction
e=immultiply(a,b);                         % multiplication
f=imadd(b,100);                              % adding a constant
g=imsubtract(a,50);                        % subtracting a constant
h=imcomplement(b);                     % complement of image

subplot(331),imshow(a),title('Image 1')
subplot(332),imshow(b),title('Image 2')
subplot(333),imshow(c),title('Addition')
subplot(334),imshow(d),title('Subtraction')
subplot(335),imshow(e),title('Multiplication')
subplot(336),imshow(f),title('Adding a constant(100) to Image 2')
subplot(337),imshow(g),title('Subtracting a constant(50) from Image 1')
subplot(338),imshow(h),title('Complement of Image 2')


Fig. 2: Arithmetic operations on image using built-in functions


No comments:

Post a Comment