0% found this document useful (0 votes)
59 views

IP Lab Final

The document describes performing various image processing techniques in MATLAB, including image subtraction, negative image generation, image cropping, histogram equalization, and KL transform. Specifically, it loads images, performs the techniques (subtracting images, inverting pixel values, cropping regions, equalizing histograms, decorrelating image blocks via KL transform), and displays the original and processed images side by side for comparison.

Uploaded by

Anonymous r9vxXs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

IP Lab Final

The document describes performing various image processing techniques in MATLAB, including image subtraction, negative image generation, image cropping, histogram equalization, and KL transform. Specifically, it loads images, performs the techniques (subtracting images, inverting pixel values, cropping regions, equalizing histograms, decorrelating image blocks via KL transform), and displays the original and processed images side by side for comparison.

Uploaded by

Anonymous r9vxXs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

Image Subtraction
clc;
clear all;
close all;
a=imread('C:\Users\HP\Desktop\mat image\mona.jpg');
b=imread('C:\Users\HP\Desktop\mat image\mona.jpg');

c=a-b;

subplot(1,3,1), imshow(a); title('First Image');


subplot(1,3,2), imshow(b); title('Second Image');
subplot(1,3,3), imshow(c); title('Subtracted Image');
1. Image Subtraction

clc;
clear all;
close all;
First_Image=imread('lenna.jpg');
Second_Image=imread('lenna.jpg');

Sub_image = First_Image - Second_Image;

subplot(1,3,1), imshow(First_Image); title('First Image');


subplot(1,3,2), imshow(Second_Image); title('Second Image');
subplot(1,3,3), imshow(Sub_Image); title('Subtracted Image');
2. Negative Image

clc;
clear all;
close all;
original_image = imread('C:\Users\HP\Desktop\mat image\btkit.jpg');
gray_image=rgb2gray(original_image);

complemented_image = imcomplement(gray_image);

subplot(1,3,1), imshow(original_image); title('Original Image');


subplot(1,3,2), imshow(gray_image); title('Gray Image');
subplot(1,3,3), imshow(complemented_image); title('Image Complement');
2. Negative Image

clc;
clear all;
close all;
original_image = imread('nituk.jpg');
gray_image=rgb2gray(original_image);

complemented_image = imcomplement(gray_image);

subplot(1,3,1), imshow(original_image); title('Original Image');


subplot(1,3,2), imshow(gray_image); title('Gray Image');
subplot(1,3,3), imshow(complemented_image); title('Image Complement');
3. Image Cropping

clc,clear all;
a= imread('ein.jpg');
disp('Enter the Rows and Columns value to crop the image:')
row_start=input('row start:');
row_end=input('row end:');
col_start=input('column start:');
col_end=input('column end:');
cropped_image = a(row_start:row_end,col_start:col_end,:);
subplot(1,2,1), imshow(a), title('original image');
subplot(1,2,2), imshow(cropped_image), title('cropped image');
3. Image Cropping

clc,clear all, close all:


a= imread('Lenna.jpg');
disp('Enter the Rows and Columns value to crop the image:')
row_start=input('row starts:');
row_end=input('row ends:');
col_start=input('column starts:');
col_end=input('column ends:');
cropped_image = a(row_start:row_end,col_start:col_end,:);
subplot(1,2,1), imshow(a), title('original image');
subplot(1,2,2), imshow(cropped_image), title('cropped image');
4. Histogram Equalization

clc,clear all, close all;


a=imread('C:\Users\HP\Desktop\mat image\lenna.jpg');
subplot(1,2,1),imshow(a),title('original image');
r= size(a,1);
c=size(a,2);
ah=uint8(zeros(r,c));
n=r*c;
f=zeros(256,1);
pdf=zeros(256,1);
cdf=zeros(256,1);
cum=zeros(256,1);
out=zeros(256,1);
for i=1:r
for j=1:c
value=a(i,j);
f(value+1)=f(value+1)+1;
pdf(value+1)=f(value+1)/n;
end
end
sum=0; L=255;
for i=1:size(pdf)
sum=sum+f(i);
cum(i)=sum;
cdf(i)=cum(i)/n;
out(i)=round(cdf(i)*L);
end
for i=1:r
for j=1:c
ah(i,j)=out(a(i,j)+1);
end
end
subplot(1,2,2), imshow(ah),title('Image after histeq');
4. Histogram Equalization

clc,clear all, close all;


a=imread('lion.jpg');
subplot(1,2,1),imshow(a),title('original image');
r= size(a,1);
c=size(a,2);
ah=uint8(zeros(r,c));
n=r*c;
f=zeros(256,1);
pdf=zeros(256,1);
cdf=zeros(256,1);
cum=zeros(256,1);
out=zeros(256,1);
for i=1:r
for j=1:c
value=a(i,j);
f(value+1)=f(value+1)+1;
pdf(value+1)=f(value+1)/n;
end
end
sum=0; L=255;
for i=1:size(pdf)
sum=sum+f(i);
cum(i)=sum;
cdf(i)=cum(i)/n;
out(i)=round(cdf(i)*L);
end
for i=1:r
for j=1:c
ah(i,j)=out(a(i,j)+1);
end
end
subplot(1,2,2), imshow(ah),title('Image after histeq');
5. KL transform

clc,close all, clear all;


im=imread('C:\Users\HP\Desktop\mat image\apj.jpg');
im2=rgb2gray(im(1:fix(size(im,1)/8)*8,1:fix(size(im,2)/8)*8,:));
image = double(im2)/255; figure(1)
subplot(121)
imagesc(image);colormap gray; axis image
title('Original')

x = im2col(image,[8 8],'distinct');
mx = mean(x');
z = x - mx';

Cz = cov(z');

[V,D] = eig(Cz);
y = V'*z;

dr = 20;
y(1:dr,:) = zeros(dr,size(y,2));
z2 = inv(V')*y;
x2 = z2 + mx';
figure(1)
subplot(122)
imagesc(col2im(x2,[8 8],[fix(size(im,1)/8)*8 fix(size(im,2)/8)*8],'distinct'));colormap gray;
axis image
title('Compressed')
5. KL Transform
clc,close all, clear all;
im=imread('ktm.jpg');
im2=rgb2gray(im(1:fix(size(im,1)/8)*8,1:fix(size(im,2)/8)*8,:));
image = double(im2)/255; %convert to double and normalize to [0,1]
figure(1)
subplot(121)
imagesc(image);colormap gray; axis image
title('Original')
%% Evaluate covariance
x = im2col(image,[8 8],'distinct'); % convert to 8x8 blocks, each block in column
mx = mean(x'); % calculate mean and save it for further processing
z = x - mx'; % zero-mean variable
%histogram(z(:),'Normalization','pdf')
Cz = cov(z'); % covariance matrix of
%% Decorrelation - KLT
[V,D] = eig(Cz); % Eigen-matrix of covariace
y = V'*z; % Values in y are decorrelated, Cy = diag(D)
% compression: drop dr rows
dr = 20;
y(1:dr,:) = zeros(dr,size(y,2));
%% Restore
z2 = inv(V')*y;
x2 = z2 + mx';
figure(1)
subplot(122)
imagesc(col2im(x2,[8 8],[fix(size(im,1)/8)*8 fix(size(im,2)/8)*8],'distinct'));colormap gray;
axis image
title('Compressed')

You might also like