Program to demonstrate Arithmetic operations on Digital Images

Write a MATLAB program to perform the following. Use subplots and display the image with a title:
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.
7) 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.

a=imread ('rice.png');
b = imresize (a, [400,400]);
figure(),subplot(1,2,1);
imshow(a);
title('Displaying rice.png');

c=imread ('cameraman.tif');
d= imresize (c, [400,400]);
subplot(1,2,2);imshow(c);
title('Displaying cameraman.tif');

% Adding two Images
figure();
e=imadd (b,d);
subplot(1,3,1);
imshow(b); title('rice.png');
subplot(1,3,2);
imshow(d); title('cameraman.tif');
subplot(1,3,3);
imshow(e); title('Added Image');

% Subtracting two Images
figure();
f=imsubtract(b,d);
subplot(1,3,1);
imshow(b); title('rice.png');
subplot(1,3,2);
imshow(d); title('cameraman.tif');
subplot(1,3,3);
imshow(f); title('Subtracted Image');

% Adding constant to an Image
figure();
g = imadd(a,50);
subplot(1,2,1);
imshow(b); title('rice.png');
subplot(1,2,2),
imshow(g); title('Image after adding a constant value 50 ');

% Subtracting constant from an Image
figure();
h = imsubtract(a,50);
subplot(1,2,1);
imshow(b); title('rice.png');
subplot(1,2,2),
imshow(h); title('Image after subtracting a constant value 100 ');

% Negative of Image
figure();
i = 255-a;
subplot(1,2,1);
imshow(b); title('rice.png');
subplot(1,2,2),
imshow(i); title('negative of rice.png image ');

% Mask Generation
mask=zeros(256,256);
mask(64:192,64:192)=1;

figure,subplot(1,2,1);
imshow(mask);
title('Mask.tif');
k=imcomplement(mask);

subplot(1,2,2);
imshow(k);
title('mask.png binary image');

I1=imread('cameraman.tif');

% Multiplication of two Images
figure();
I= uint8(I1).*uint8(mask);
subplot(1,3,1);
imshow(I1); title('cameraman.tif');
subplot(1,3,2);
imshow(mask); title('Mask.tif');
subplot(1,3,3);
imshow(I); title('Multiplying two 8-bit image');

Now that we have seen how basic arithmetic operations are carried out on Digital Images, let us explore some of the in-biult MATLAB functions which are used for the same purpose. Here’s a program to demonstrate the use of in-built arithmetic functions.

Write the above program using:

  • imadd() function for addition
  • imsubtract() for Subtraction
  • immultiply() for multiplication operations
  • imcompliment() function to get compliment of an image

i1=imread('cameraman.tif'); % To read image 1
i2=imread('rice.png'); % To read image 2
figure();
subplot(231); imshow(i1); title('Image 1'); % Original image1
subplot(232); imshow(i2); title('Image 2'); % Original image2

add1=imadd(i1,i2); % Addition of images
subplot(233); imshow(add1); title('Image1+Image2');

sub1=imsubtract(i1,i2); % Subtraction of images
subplot(234); imshow(sub1); title('image1-image2');

imc1=imcomplement(i1); % Complement of image
subplot(235); imshow(imc1); title('Complement i1');

imc2=imcomplement(i2);
subplot(236); imshow(imc2); title('Complement i2');

mask=zeros(256,256);
mask(64:192,64:192)=1;

figure();
subplot(131); imshow(mask); title('Mask');
subplot(132); imshow(i1); title('Cameraman');

mul=immultiply(uint8(i1),uint8(mask)); % Multiplication of images
subplot(133); imshow(mul); title('Image1*Mask');

In our next article we will see the application of these arithmetic operation in DIP.
Check out the article on Noise Removal in Digital Images by Averaging here.
Go back to Adjacency connectivity and component labelling