📄 exp01.m
字号:
% % % % % % % % % % % % % % % %
% Program for : %
% 1) ZOOMING , %
% 2) SHRINKING & %
% 3) GRAY LEVEL RESOLUTION %
% the image %
% % % % % % % % % % % % % % % %
clc;
close all;
clear all;
% READING THE IMAGE
A = imread('img_01.bmp');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%CONVERTING THE IMAGE INTO GRAY SCALE USING DYNAMIC RANGE 0 TO 255 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CONVERTING 'unit8' VALUES TO 'double' WITH DYNAMIC RANGE [0 255]
A_img = im2double(A)* 255;
% Separating RGB Components
R = A_img(:,:,1);
G = A_img(:,:,2);
B = A_img(:,:,3);
% Converting to Gray Scale Image
Y = 0.299*R + 0.587*G + 0.114*B;
% Rounding of Y
Y = round (Y);
% Displaying original image and its gray scale version
figure;
imshow(Y/255); title('Gray Scale Image');
% Writing the gray scale image
imwrite(Y/255,'a1.bmp');
[R C] = size (Y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%ZOOMING USING PIXEL REPLICATION METHOD
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = input(' Enter the Zooming Factor : ');
for y = 1:R
for x = 1:C
a1 = Y(x,y);
a2 = a1;
m = (a2-a1)/(z-1);
for b = 0:(z-1)
Y_new((x*z)-(z-1)+b,y) = a1 + m*b;
end
end
end
for x = 1:(C*z)
for y = 1:R
a1 = Y_new(x,y);
a2 = a1;
m = (a2-a1)/(z-1);
for b = 0:(z-1)
Y1a(x,((y*z)-(z-1)+b)) = a1 + m*b;
end
end
end
figure;
imshow(Y1a/255); title('Zoom by Pixel Replication');
imwrite(Y1a/255,'a2.bmp');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ZOOMING USING BILINEAR INTERPOLATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[R C] = size (Y);
z1 = input(' Enter the Zooming Factor : ');
i=1;
for y=1:C
j=1;
for x=1:R
a1=Y(x,y);
if x==R
a2=a1;
else
a2=Y(x+1,y);
end
slope= (a2-a1)/z1;
for d=0:(z1-1)
Y11(j,i) = slope*d + a1;
j=j+1;
end
end
i=i+1;
end
[R1 C1] = size (Y11);
i=1;
for x=1:R1
j=1;
for y=1:C1
a1=Y11(x,y);
if y==C1
a2=a1;
else
a2=Y11(x,y+1);
end
slope= (a2-a1)/z1;
for d=0:(z1-1)
Y1b(i,j) = slope*d + a1;
j=j+1;
end
end
i=i+1;
end
% Displaying zoomed image
figure;
imshow(Y1b/255); title('Zoomed Image');
% writing the gray scale image
imwrite(Y1b/255,'zoom_1.bmp');
%%%%%%%%%%%%%%%%%%%%%%%%
%% SHRINKING OF IMAGE %%
%%%%%%%%%%%%%%%%%%%%%%%%
shrink = 4;
i=1;
for x=1:shrink:R
j=1;
for y=1:shrink:C
Y2(i,j) = Y(x,y);
j=j+1;
end
i=i+1;
end
figure;
subplot(1,2,1);imshow(Y2/255); title('Shrink Image');
subplot(1,2,2); imshow(Y/255); title('Gray Scale Image');
imwrite(Y2/255,'a3.bmp');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%ZOOMING OF SHRUNKEN IMAGE %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = 4;
[R1 C1] = size (Y2);
for y = 1:R1
for x = 1:C1
a1 = Y2(x,y);
a2 = a1;
m = (a2-a1)/(z-1);
for b = 0:(z-1)
Y_new((x*z)-(z-1)+b,y) = a1 + m*b;
end
end
end
for x = 1:(C1*z)
for y = 1:R1
a1 = Y_new(x,y);
a2 = a1;
m = (a2-a1)/(z-1);
for b = 0:(z-1)
Y3(x,((y*z)-(z-1)+b)) = a1 + m*b;
end
end
end
figure;
imshow(Y3/255); title('Zooming of shrukan image by Pixel Replication');
imwrite(Y3/255,'a5.bmp');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CHANGING GRAYSCALE RESOLUTION %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
input('Press ENTER for imge with Changing Grayscale Resolution; ');
h=zeros(1,256); %Array of zeros(1 X 256)
n=input('enter no of bits: ');
k=1;
for i=0:(2^n-1)
for j=1:256/(2^n)
h(1,k)=i;
k=k+1;
end
end
h=h*(256/2^n);
for x=1:R
j=1;
for y=1:C
Y4(i,j)=h(1,Y(x,y)+1);
j=j+1;
end
i=i+1;
end
figure;
subplot(1,2,1); imshow(Y4/255); title('Image with changed grayscale resolution');
subplot(1,2,2); imshow(Y/255); title('Gray Scale Image');
imwrite(Y4/255,'a4.bmp');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -