TEL 432 E: Digital Image Processing
TEL 432 E: Digital Image Processing
TEL 432 E
-Homework 2-
Histogram equalization is used to equally distribute the number of pixels between grey levels. It stretches or compresses the image such that (A: input image, B:output image): Pixel values that occur frequently in A occupy a bigger dynamic range in B, i.e., get stretched and become more visible. Pixel values that occur infrequently in A occupy a smaller dynamic range in B, i.e., get compressed and become less visible.
Histogram equalization is not ideal, i.e., in general B will have aflatter histogram than A, but pB(l) is not guaranteed to be uniform (flat). (Cited from Lecture notes of Onur G. Guleryuz, Dept. of ECE, Polytechnic Univ., Brooklyn, NY) Histogram Matching allow us to specify which pixel values we want to express or depress, in other words it lets us to choose any histogram shape. Histogram Equalization This code is used for histogram equalization. MatLab Code clc; clear all; colormap('gray'); L=256; x0=imread('6010d.tif'); x0=double(x0); %input image [m,n]=size(x0); len=m*n; %number of pixels x=reshape(x0,len,1); %convert to [len:1] xpdf=hist(x,[0:L-1]); % pdf, 1 x L xpdf=xpdf/len; %Normalize it to get nk/n (eq 3.3-%7)....(len %is equal to sum(xpdf), number of pixels...) sk=xpdf*triu(ones(L));%CDF (eq 3.3-8) y0=zeros(m,n); for k=0:L-1 if (xpdf(k+1) > 0) list=find(x0 ==k); %find value in input image
y=reshape(y0,len,1); %convert to [len:1] ypdf=hist(y,[0:L-1])/len; % pdf, 1 x L figure(1)subplot(311),stem([0:L1],xpdf,'.'),title('histogram, original') subplot(312),stem([0:L-1],sk,'.'),title('transformation') subplot(313),stem([0:L-1],ypdf,'.'),title('histogram, equalized') figure(2); colormap('gray'); image(y0); y0=uint8(y0); imwrite(y0,'6010d_eq.tif','TIFF'); Output Code presented above outputs these results. Output Image: Input Image:
Histogram Specification: This code is used to match histogram of input image with a desired histogram. MatLab Code: clc; clear all; colormap('gray'); L=256; x0=imread('Lvr2i.tif'); x0=double(x0); %input image [m,n]=size(x0); len=m*n; %number of pixels x=reshape(x0,len,1); %convert to [len:1] xpdf=hist(x,[0:L-1]); % pdf, 1 x L xpdf=xpdf/len;%Normalize it to get nk/n (eq 3.3-7)....(len is equal to sum(xpdf), number %of pixels...) sk=xpdf*triu(ones(L));%CDF (eq 3.3-8), (eq 3.3-13)
%Histogram Specification zd_pdf(1:L)=1; %desired histogram of output image ext = sin(0:pi/255:pi); zd_pdf=zd_pdf+ext; %desired histogram of output image zd_pdf = zd_pdf / sum(zd_pdf); %normalize zk=zd_pdf*triu(ones(L)); %G(z), CDF (eq 3.3-14) %iteration(eq 3.3-17) % hist. matching mapping=zeros(256); z0=zeros(m,n); for q=1:L for p=mapping(q)+1:L if ((zk(p)-sk(q)) >= 0) mapping(q) = p; list=find(x0 == q-1); a=size(list);%find value %in input image z0(list)=p; %map sk value for each k valued %pixel break; end end end z=reshape(z0,len,1); %convert to [len:1] zpdf=hist(z,[0:L-1])/len; % pdf, 1 x L figure(1),subplot(411),stem([0:L1],xpdf,'.'),title('histogram, original') subplot(412),stem([0:L-1],zd_pdf,'.'),title('desired pdf') subplot(413),stem([0:L-1],zk,'.'),title('transformation') subplot(414),stem([0:L-1],zpdf,'.'),title('histogram, matched') figure(2); colormap('gray'); image(z0); z0=uint8(z0); imwrite(z0,'Lvr2i_match.tif','TIFF');
Output This code outputs an image and several plots given below. Output Image: Input Image: