(EE440) Lab1Report Kien Cuong Hung
(EE440) Lab1Report Kien Cuong Hung
Report
Names:
- Huynh Nhu Kien
- Ha Xuan Cuong
- Nguyen Phuc Bao Hung
Class: 10ECE
1/ Linear stretching:
Code:
%-----------linear stretching---------im = imread('3_1.jpg'); % Read image
% im=imread('3_2.jpg');
subplot(221);
imshow(im),title('Original Image'); % Display the image
hsv= rgb2hsv(im);
% Convert the image from RGB colormap to HSV colormap
V= hsv(:,:,3);
% Assign the intensity of the HSV image to V
subplot(222),imhist(V),title('Histogram of the Intensity');
V= uint8(255*(V-min(V(:)))/(max(V(:))-min(V(:)))); % Algorithm and scale
% the Intensity from [0 1] to [0 255]
V=double(V)/255; % Rescale the Intensity back to [0 1]
hsv(:,:,3)=V;
% Reassign V into
newIm= hsv2rgb(hsv); % Convert the image back to RGB colormap
subplot(223);
imshow(newIm),title('Stretched Image');
subplot(224),imhist(V),title('Histogram of new Image Intensity');
2/ Histogram Equalization:
Code:
%------------ Histogram equalization--------------im=imread('3_1.jpg');
% im=imread('3_2.jpg');
hsv=rgb2hsv(im); % Convert the image to HSV colormap
V=hsv(:,:,3);
% Assign the Intensity of the image to V
V=uint8(255*V); % Scale the intensity from [0 1] to [0 255]
[nk,x]=imhist(V);
subplot(221);imhist(V),title('Histogram of the intensity');
subplot(222);imshow(im),title('Original Image');
MN=numel(V); % number of elements in V
sk=round(255/MN*(cumsum(nk))); % Calculate the cdf of V
for i=1:MN
for j=1:256
if(V(i)==j)
V(i)=sk(j); % Replace each Intensity value with a new one after
% taking cumulative sum algorithm
break
end
end
end
subplot(223);imhist(V),title('Histogram of new Image Intensity');
V=double(V)/255; % Convert the Intensity back to scale [0 1]
hsv(:,:,3)=V; % Reassign the Intensity to the HSV image
NewIm=hsv2rgb(hsv); % Convert the HSV image to RGB image
subplot(224);imshow(NewIm),title('Equalized Image');
3/ Histogram Matching:
Code:
clc; clear;
%----------------choose an image to enhance-------------------------input=imread('3_1.jpg');
% input=imread('3_2.jpg');
%----------------histogram specification---------------------------hsv=rgb2hsv(input);
V=hsv(:,:,3);
NV=uint8(255*V);
V=uint8(255*V);
[nk,x]=imhist(V);
MN=numel(V);
sk=round(255/MN*(cumsum(nk)));
z=0:1:255;
d=normpdf(z,100,60);
gz=round(255*(cumsum(d)));
% original histogram
% number of values in V
% transformation function sk
% desired histogram
% transformation function G(z)
% r to s
for i=1:MN
for j=1:256
if(V(i)==(j-1))
V(i)=sk(j);
break
end
end
end
% s to z
for i=1:MN
j=1;
if(V(i)<=gz(j))
V(i)=j-1;
else
while(V(i)>gz(j)&&j<256)
j=j+1;
end
if (abs(V(i)-gz(j-1))<abs(V(i)-gz(j)))
% find gz closest to sk
V(i)=j-2;
else V(i)=j-1;
end
end
end
hsv(:,:,3)=double(V)/255;
NewIm=hsv2rgb(hsv);
figure(1);
subplot(311);imhist(NV);title('Original histogram');axis tight;
subplot(312); stem(d*MN); title('Desire histogram');axis tight;
subplot(313); imhist(V); title('New histogram');axis tight;
figure(2);
subplot(211);imshow(input);title('Original image');
subplot(212);imshow(NewIm);title('New image');
Image 3_1.jpg
1/ Linear stretching:
Result:
2/ Histogram equalization:
Result:
3/ Histogram Matching:
Result:
Image 3_2.jpg
1/ Linear stretching:
Result:
2/ Histogram equalization:
Result:
3/ Histogram Matching:
Result:
Comments:
The first method doesnt affect to image 3_2.jpg because the aim of this
method is stretch the histogram range of the image from certain range to
[0 255], and its histogram range is yet belonged to [0 255]
In 3 methods, the third one ( Histogram Matching) is the best enhancement
method ( for both images) because we can control the desired histogram,
then if we choose a suitable histogram, we will get the best result for
enhancement.