Professional Documents
Culture Documents
2 Histogram Equalization
5 Log Transformation
6 Gamma Transformation
7 Contrast Stretching
17 Interpolation or Resizing
Lab Experiments
Experiment 1: Convert RGB image to binary image
img = imread('img/camrgb.jpeg');
gray = rgb2gray(img);
binary = gray; [row,col]=size(binary);
figure;
subplot(2,2,1); imshow(img); title('Original Image'); subplot(2,2,2); imshow(gray); title('Gray
Image'); subplot(2,2,3); imshow(binary); title('Binary Image');
Experiment 2: Histogram Equalization
img = imread('img/camrgb.jpeg');grayimg = rgb2gray(img)
his = histeq(grayimg);figure;
subplot(2,2,1);imshow(grayimg);title('Original Gray Image');
subplot(2,2,2);imhist(grayimg);title('Original Histogram');
subplot(2,2,3);imshow(his);title('Equalized Image'); subplot(2,2,4);imhist(his);title('Equalized
Histogram');
Experiment 3: RGB to Binary Transformation using Threshold
img = imread('img/camrgb.jpeg');thval = graythresh(img);
thimg = im2bw(img,thval);figure;
subplot(1,2,1); imshow(img); title('Original Image'); subplot(1,2,2); imshow(thimg);
title('Threshold Image');
Experiment 4: RGB to negative image transformation
ori_img = imread('img/camrgb.jpeg');
L = 256;
neg_img = (L-1)-ori_img;figure;
subplot(1,2,1); imshow(ori_img);title('Original Image'); subplot(1,2,2);
imshow(neg_img);title('Negative Image');
Experiment 5: Log Transformation
figure;
subplot(1,2,1); imshow(ori_img); title('Original Image'); subplot(1,2,2); imshow(new_img);
title('Log Transform Image');
Experiment 6: Gamma Transformation
figure;
subplot(1,2,1); imshow(ori_img); title('Original Image'); subplot(1,2,2); imshow(new_img);
title('Gamma Transform Image');
Experiment 7: Contrast Stretching
ori_img = imread('img/cam.jpeg');new_img = ori_img;
s1 = 20;
s2 = 150;
r1 = 100;
r2 = 150;
L = 256;
alfa = s1/r1;
beta = (s2-s1)/(r2-r1);
gamma = ((L-1)-s2)/((L-1)-r2);
[x,y,z] = size(new_img);
for i=1:x
for j=1:y
for k=1:z
if new_img(i,j,k)<=r1r = new_img(i,j,k);
new_img(i,j,k) = alfa*r;
elseif new_img(i,j,k)>r1 && new_img(i,j,k)<=r2r = new_img(i,j,k);
new_img(i,j,k) = (beta*(r-r1))+s1;else
r = new_img(i,j,k);
new_img(i,j,k) = (gamma*(r-r2)+s2);endif
endforendfor endfor
figure;
subplot(1,2,1); imshow(ori_img); title('Original Image'); subplot(1,2,2); imshow(new_img);
title('Enhanced Image');
Experiment 8: Gray Level Slicing
[row,col] = size(ori_img);
subplot(2,2,1); imshow(ori_img); title('Original Image');subplot(2,2,3); imshow(ori_img);
title('Original Image');
% Slicing Without Backgroundfor i=1:row
for j=1:col
if ori_img(i,j) >= r1 && ori_img(i,j)<=r2
ori_img(i,j) = (L-1);else
ori_img(i,j) = 0;endif
endforendfor
subplot(2,2,2); imshow(ori_img); title('Enhanced Image without Background');
% Slicing With Backgroundfor i=1:row
for j=1:col
if img(i,j) >= r1 && img(i,j)<=r2img(i,j) = (L-1);
else
img(i,j) = img(i,j);endif
endforendfor
subplot(2,2,4); imshow(img); title('Enhanced Image With Background');
Experiment 9: Bit Plane Slicing
ori_img = imread('img\salt.jpg');
noisy_img = ori_img;
x = rand(size(noisy_img)); noisy_img(x(:)>0.95)=255; %adding salt noiseminf =
@(x)min(x(:));
filtered_img = nlfilter(noisy_img,[3,3],minf);
ori_img = imread('img\cam.jpg');
noisy_img = imnoise(ori_img,'salt & pepper', 0.05); %adding salt and pepper noisemedf =
@(x)median(x(:));
filtered_img = nlfilter(noisy_img,[3,3],medf);
ori_img = imread('img/saltnoise.jpg');
mask1 = (1/9)*[1,1,1;1,1,1;1,1,1];
mask2 = (1/25)*[1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;1,1,1,1,1];
filtered_img1 = imfilter(ori_img,mask1);filtered_img2 = imfilter(ori_img,mask2);
ori_img = imread('img/cam.jpg');
mask = [-1,-1,-1;-1,8,-1;-1,-1,-1];
filtered_img = imfilter(ori_img,mask);
ori_img = imread('img\cam.jpg');
a = 2;
mask = [-1,-1,-1; -1,a+8,-1; -1,-1,-1];
filtered_img = imfilter(ori_img,mask);
ori_img = imread('img\lady.png');
resized_img1 = imresize(ori_img,[512,312],'nearest');resized_img2 =
imresize(ori_img,[512,253],'bilinear');