DIP lab.4 L5

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

Digital Image Processing Laboratory

Lab.4: Arithmetic and Logical Operations on Images, Image Quantization


and Gray-scale Image Modification.
❖ Arithmetic Operations:
 Arithmetic and logic operations: performed on a pixel-by-pixel basis; arithmetic
operations: add, subtract, multiply, divide; logic operations: AND, OR, NOT
1- Addition: used to combine information in two images.
Syntax:
Z=imadd (X, Y);
2- Subtraction: used for motion detection and background subtraction.
Syntax:
Z=imsubtract (X, Y);
Z = imabsdiff (X, Y) % Absolute difference of two images
3- Multiplication: used to brighten or darken an image.
Syntax:
Z=immultiply (X, Y);
Y may be constant or image
4- Division: used to darken or brighten an image.
Syntax:
Z=imdivide (X, Y);
Y may be constant or image
➢ [Task 1: Performing Arithmetic Operations on Images]
• Execute the following script in MATLAB, then write your observations:

X=imread('rice.png');
Y=imread('cameraman.tif');
k=imadd(X,Y);
A=immultiply(Y,2);
B=imdivide(Y,2);
imshow(X),title('image X')
figure,imshow(Y),title('image Y')
figure,imshow(K),title('adding X and Y')
figure,imshow(A),title('multiply y by 2')
figure,imshow(B),title('dividing y by 2')
❖ Logical Operations:
1- AND: used to combine two images or for image masking.
Syntax:
Z=bitand (X, Y);
2- OR: used to combine two images or for image masking
Syntax:
Z=bitor (X, Y);
3- NOT: creates a negative on an image by performing a logical NOT on each bit.
Syntax:
Z=imcomplement (X);
➢ [Task 2: Performing Logical Operations on Images]
• Execute the following script in MATLAB, then write your observations:

clc,clear,close all
X=imread('cameraman.tif');
[I ,area]=imcrop(X);
Y=ones(size(X))*255;
A=area(1); B=area(2);W=area(3); H=area(4);
Y(A:A+H,B:B+W)=0; %mask for OR operation
Y=uint8(Y);
Z=bitor(X,Y);
D=imcomplement(Y); %mask for AND operation
F=bitand(X,D);
imshow(X),title('original image');
figure,imshow(Z),title('result of ORING operation of the image and
the mask')
figure,imshow(F),title('result of ANDING operation of the image and
the mask complement')
❖ Image Quantization:

Image quantization is the process of reducing the image data by removing some of the detail information by
mapping groups of data points to a single point.
Gray-level reduction: reducing the number of gray levels, typically from 256 levels for 8-bit per pixel data

to fewer than 8 bits, can be performed with AND or OR masks (see examples in the text book).

➢ [Task 3: Gray-level reduction with AND mask]


• Write the following function in MATLAB:
function [y] = quantization( I,no_of_levels )
% A function to perform image quantization to some level
% I is the input image of type uint8
% no_of_levels is the number of desired gray levels

I=uint8(I);
k=log(no_of_levels)/log(2);
k=8-k;
mask=0;
for i=k:7
mask=mask+power(2,i);
end
z=ones(size(I),class(I))*mask;
y=bitand(z,I);
end
• Write the following script to test the function above as follow, then write your observations:
clc,clear all,close all
X=imread('cameraman.tif');
y1=quantization(X,64);
y2=quantization(X,32);
y3=quantization(X,16);
y4=quantization(X,8);
imshow(X)
figure,imshow(y1)
figure,imshow(y2)
figure,imshow(y3)
figure,imshow(y4)
❖ Histogram equalization
It is a technique where the histogram of the resultant image is as flat as possible
The histogram equalization process for digital images consists of four steps: (1) find the
running sum of the histogram values, (2) normalize the values from step (1) by dividing by
the total number of pixels, (3) multiply the values from Step 2 by the maximum gray-level
value and round, and (4) map the gray-level values to the results from Step 3 using a oneto-
one correspondence.
➢ [Task 4: Histogram Equalization using MATLAB Function]
• Write the following code in a MATLAB script:
close all;
clear all;
[n,p]=uigetfile('Choose Poorly scanned Image');
myimage=imread([p n]);
if(size(myimage,3)==3)
myimage =rgb2gray(myimage);
end
newimage= histeq(myimage);
figure;
subplot(2,2,1);imshow(myimage); title('Original image');
subplot(2,2,2);imshow(newimage); title('Histogram equalized mage');
subplot(2,2,3);imhist(myimage); title('Histogram for original mage');
subplot(2,2,4);imhist(newimage); title('Histogram for equalized
mage');
➢ [Task 5: Histogram Equalization without using MATLAB Function]

• Write the following code in a MATLAB script, then explain what you have seen
close all;
clear all;
[n,p]=uigetfile();
data=imread([p n]);
if(size(data,3)==3)
data=rgb2gray(data);
end
subplot(2,2,1);imshow(data); title('Original image');
[rows cols]=size(data);
myhist=zeros(1,256);
% Calculation of histogram
for i=1:rows
for j=1:cols
m=double(data(i,j));
myhist(m+1)=myhist(m+1)+1;
end
end
subplot(2,2,2);bar(myhist); title('Histogram of original image');
sum=0;
%Cumulative values
for i=0:255
sum=sum+myhist(i+1);
sum_of_hist(i+1)=sum;
end
%Dm=input('Enter no. of gray levels in output image: ');
MAX=255;
for i=1:rows
for j=1:cols
n=double(data(i,j));
data(i,j)=sum_of_hist(n+1)*MAX/sum;
end
end
%Calculation of histogram for equalised image
for i=1:rows
for j=1:cols
m=double(data(i,j));
myhist(m+1)=myhist(m+1)+1;
end
end
subplot(2,2,3);bar(myhist);title('Equalised Histogram');
subplot(2,2,4);imshow(data); title('Image after histogram
equalisation');

Home Work
1- Develop the function quantization in task 3 (that maps gray levels to the beginning of the range ) so that it
accept another input to map the gray levels to the beginning , middle, or end of the range.
2- Develop the script in task 2 so that it can be used also for color images.

Eng./ Maged Albadany

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy