1.灰度變換增強程序:
% GRAY TRANSFORM
clc;
I=imread('pout.tif');
imshow(I);
J=imadjust(I,[0.3 0.7],[0 1],1);?
figure;
imshow(J);
J=imadjust(I,[0.3 0.7],[0 1],0.5);?
%output values.
figure;
imshow(J);
J=imadjust(I,[0.3 0.7],[0 1],1.5);? ? % if GAMMA is greater than 1,the mapping si weighted toward lower (darker)
figure;
imshow(J);
J=imadjust(I,[0.3 0.7],[0 1],1.5);?
%output values.
figure;
imshow(J)
J=imadjust(I,[0.3 0.7],[0 1],1);? ? % If TOP<BOTTOM,the output image is reversed,as in a photographic negative.
figure;
imshow(J);
?
figure;
imshow(J)
J=imadjust(I,[0.3 0.7],[0 1],1);?
figure;
imshow(J);
?
2.直方圖灰度變換
%直方圖灰度變換
[X,map]=imread('forest.tif');
I=ind2gray(X,map);%把索引圖像轉換為灰度圖像
imshow(I);
title('原圖像');
improfile%用鼠標選擇一條對角線,顯示線段的灰度值
figure;subplot(121)
plot(0:0.01:1,sqrt(0:0.01:1))
axis square
title('平方根灰度變換函數')
subplot(122)
maxnum=double(max(max(I)));%取得二維數組最大值
J=sqrt(double(I)/maxnum);%把數據類型轉換成double,然后進行平方根變換
%sqrt函數不支持uint8類型
J=uint8(J*maxnum);%把數據類型轉換成uint8類型
imshow(J)
title('平方根變換后的圖像')
[X,map]=imread('forest.tif');
I=ind2gray(X,map);%把索引圖像轉換為灰度圖像
imshow(I);
title('原圖像');
improfile%用鼠標選擇一條對角線,顯示線段的灰度值
figure;subplot(121)
plot(0:0.01:1,sqrt(0:0.01:1))
axis square
title('平方根灰度變換函數')
subplot(122)
maxnum=double(max(max(I)));%取得二維數組最大值
J=sqrt(double(I)/maxnum);%把數據類型轉換成double,然后進行平方根變換
%sqrt函數不支持uint8類型
J=uint8(J*maxnum);%把數據類型轉換成uint8類型
imshow(J)
title('平方根變換后的圖像')
3.直方圖均衡化程序舉例
% HISTGRAM EAQUALIZATION
clc;
% Clear command window
I=imread('tire.tif');
% reads the image in tire.tif into I
imshow(I);
% displays the intensity image I with 256 gray levels
figure;
%creates a new figure window
imhist(I);
% displays a histogram for the intensity image I
J=histeq(I,64);
% transforms the intensity image I,returning J an intensity
figure;
%image with 64 discrete levels
imshow(J);
figure;
imhist(J);
J=histeq(I,32);
%transforms the intensity image ,returning in % J an intensity
figure;
%image with 32 discrete levels
imshow(J);
figure;
imhist(J);
clc;
% Clear command window
I=imread('tire.tif');
% reads the image in tire.tif into I
imshow(I);
% displays the intensity image I with 256 gray levels
figure;
%creates a new figure window
imhist(I);
% displays a histogram for the intensity image I
J=histeq(I,64);
% transforms the intensity image I,returning J an intensity
figure;
%image with 64 discrete levels
imshow(J);
figure;
imhist(J);
J=histeq(I,32);
%transforms the intensity image ,returning in % J an intensity
figure;
%image with 32 discrete levels
imshow(J);
figure;
imhist(J);
4.直方圖規定化程序舉例
% HISTGRAM REGULIZATION
clc;
clc;
%Clear command window
I=imread('tire.tif');
%reads the image in tire.tif into I
J=histeq(I,32);
%transforms the intensity image I,returning in
%J an intensity image with 32 discrete levels
[counts,x]=imhist(J);
%displays a histogram for the intensity image I
Q=imread('pout.tif');
%reads the image in tire.tif into I
figure;
imshow(Q);
figure;
imhist(Q);
M=histeq(Q,counts);
%transforms the intensity image Q so that the
%histogram of the output image M approximately matches counts
figure;
imshow(M);
figure;
imhist(M);
I=imread('tire.tif');
%reads the image in tire.tif into I
J=histeq(I,32);
%transforms the intensity image I,returning in
%J an intensity image with 32 discrete levels
[counts,x]=imhist(J);
%displays a histogram for the intensity image I
Q=imread('pout.tif');
%reads the image in tire.tif into I
figure;
imshow(Q);
figure;
imhist(Q);
M=histeq(Q,counts);
%transforms the intensity image Q so that the
%histogram of the output image M approximately matches counts
figure;
imshow(M);
figure;
imhist(M);
空域濾波增強部分程序
1.線性平滑濾波
I=imread('eight.tif');
J=imnoise(I,'salt & pepper',0.02);
subplot(221),imshow(I)
title('原圖像')
subplot(222),imshow(J)
title('添加椒鹽噪聲圖像')
K1=filter2(fspecial('average',3),J)/255;%應用3*3鄰域窗口法
subplot(223),imshow(K1)
title('3x3窗的鄰域平均濾波圖像')
K2=filter2(fspecial('average',7),J)/255;%應用7*7鄰域窗口法
subplot(224),imshow(K2)
title('7x7窗的鄰域平均濾波圖像')
I=imread('eight.tif');
J=imnoise(I,'salt & pepper',0.02);
subplot(221),imshow(I)
title('原圖像')
subplot(222),imshow(J)
title('添加椒鹽噪聲圖像')
K1=filter2(fspecial('average',3),J)/255;%應用3*3鄰域窗口法
subplot(223),imshow(K1)
title('3x3窗的鄰域平均濾波圖像')
K2=filter2(fspecial('average',7),J)/255;%應用7*7鄰域窗口法
subplot(224),imshow(K2)
title('7x7窗的鄰域平均濾波圖像')
?
?
2.中值濾波器
MATLAB中的二維中值濾波函數medfit2來進行圖像中椒鹽躁聲的去除
%IMAGE NOISE REDUCTION WITH MEDIAN FILTER
clc;
hood=3;%濾波窗口
[I,map]=imread('eight.tif');
imshow(I,map);
noisy=imnoise(I,'salt & pepper',0.05);
figure;
imshow(noisy,map);
filtered1=medfilt2(noisy,[hood hood]);
figure;
imshow(filtered1,map);
hood=5;
filtered2=medfilt2(noisy,[hood hood]);
figure;
imshow(filtered2,map);
hood=7;
filtered3=medfilt2(noisy,[hood hood]);
figure;
imshow(filtered3,map);
MATLAB中的二維中值濾波函數medfit2來進行圖像中椒鹽躁聲的去除
%IMAGE NOISE REDUCTION WITH MEDIAN FILTER
clc;
hood=3;%濾波窗口
[I,map]=imread('eight.tif');
imshow(I,map);
noisy=imnoise(I,'salt & pepper',0.05);
figure;
imshow(noisy,map);
filtered1=medfilt2(noisy,[hood hood]);
figure;
imshow(filtered1,map);
hood=5;
filtered2=medfilt2(noisy,[hood hood]);
figure;
imshow(filtered2,map);
hood=7;
filtered3=medfilt2(noisy,[hood hood]);
figure;
imshow(filtered3,map);
3. 4鄰域8鄰域平均濾波算法
% IMAGE NOISE REDUCTION WITH MEAN ALGORITHM
clc;
[I,map]=imread('eight.tif');
noisy=imnoise(I,'salt & pepper',0.05);
myfilt1=[0 1 0;1 1 1;0 1 0];%4鄰域平均濾波模版
myfilt1=myfilt1/9;%對模版歸一化
filtered1=filter2(myfilt1,noisy);
imshow(filtered1,map);
myfilt2=[1 1 1;1 1 1;1 1 1];
myfilt2=myfilt2/9;
filtered2=filter2(myfilt2,noisy);
figure;
imshow(filtered2,map);
% IMAGE NOISE REDUCTION WITH MEAN ALGORITHM
clc;
[I,map]=imread('eight.tif');
noisy=imnoise(I,'salt & pepper',0.05);
myfilt1=[0 1 0;1 1 1;0 1 0];%4鄰域平均濾波模版
myfilt1=myfilt1/9;%對模版歸一化
filtered1=filter2(myfilt1,noisy);
imshow(filtered1,map);
myfilt2=[1 1 1;1 1 1;1 1 1];
myfilt2=myfilt2/9;
filtered2=filter2(myfilt2,noisy);
figure;
imshow(filtered2,map);
頻域增強程序舉例
1.低通濾波器
% LOWPASS FILTER
clc;
[I,map]=imread('eight.tif');
noisy=imnoise(I,'gaussian',0.05);
imshow(noisy,map);
myfilt1=[1 1 1;1 1 1;1 1 1];
myfilt1=myfilt1/9;
filtered1=filter2(myfilt1,noisy);
figure;
imshow(filtered1,map);
myfilt2=[1 1 1;1 2 1;1 1 1];
myfilt2=myfilt2/10;
filtered2=filter2(myfilt2,noisy);
figure;
imshow(filtered2,map);
myfilt3=[1 2 1;2 4 2; 1 2 1];
myfilt3=filter2(myfilt3,noisy);
figure;
imshow(filtered3,map);
% LOWPASS FILTER
clc;
[I,map]=imread('eight.tif');
noisy=imnoise(I,'gaussian',0.05);
imshow(noisy,map);
myfilt1=[1 1 1;1 1 1;1 1 1];
myfilt1=myfilt1/9;
filtered1=filter2(myfilt1,noisy);
figure;
imshow(filtered1,map);
myfilt2=[1 1 1;1 2 1;1 1 1];
myfilt2=myfilt2/10;
filtered2=filter2(myfilt2,noisy);
figure;
imshow(filtered2,map);
myfilt3=[1 2 1;2 4 2; 1 2 1];
myfilt3=filter2(myfilt3,noisy);
figure;
imshow(filtered3,map);
2.布特沃斯低通濾波器圖像實例
I=imread('saturn.png');
J=imnoise(I,'salt & pepper',0.02);
subplot(121),imshow(J)
title('含噪聲的原圖像')
J=double(J);
f=fft2(J);
g=fftshift(f);
[M,N]=size(f);
n=3;d0=20;
n1=floor(M/2);n2=floor(N/2);
for i=1:M;
for j=1:N;
d=sqrt((i-n1)^2+(j-n2)^2);
h=1/(1+0.414*(d/d0)^(2*n));
g(i,j)=h*g(i,j);
end
end
g=ifftshift(g);
g=uint8(real(ifft2(g)));
subplot(122),imshow(g)
title('三階Butterworth濾波圖像')
I=imread('saturn.png');
J=imnoise(I,'salt & pepper',0.02);
subplot(121),imshow(J)
title('含噪聲的原圖像')
J=double(J);
f=fft2(J);
g=fftshift(f);
[M,N]=size(f);
n=3;d0=20;
n1=floor(M/2);n2=floor(N/2);
for i=1:M;
for j=1:N;
d=sqrt((i-n1)^2+(j-n2)^2);
h=1/(1+0.414*(d/d0)^(2*n));
g(i,j)=h*g(i,j);
end
end
g=ifftshift(g);
g=uint8(real(ifft2(g)));
subplot(122),imshow(g)
title('三階Butterworth濾波圖像')
色彩增強程序舉例
1.真彩色增強實例:
%真彩色圖像的分解
clc;
RGB=imread('peppers.png');
subplot(221),imshow(RGB)
title('原始真彩色圖像')
subplot(222),imshow(RGB(:,:,1))
title('真彩色圖像的紅色分量')
subplot(223),imshow(RGB(:,:,2))
title('真彩色圖像的綠色分量')
subplot(224),imshow(RGB(:,:,3))
title('真彩色圖像的藍色分量')
%真彩色圖像的分解
clc;
RGB=imread('peppers.png');
subplot(221),imshow(RGB)
title('原始真彩色圖像')
subplot(222),imshow(RGB(:,:,1))
title('真彩色圖像的紅色分量')
subplot(223),imshow(RGB(:,:,2))
title('真彩色圖像的綠色分量')
subplot(224),imshow(RGB(:,:,3))
title('真彩色圖像的藍色分量')
2.偽彩色增強舉例:
I=imread('cameraman.tif');
imshow(I);
X=grayslice(I,16);%thresholds the intensity image I using
%threshold values 1/16,2/16,…..,15/16,returning an indexed %image in X
figure;
imshow(X,hot(16));
?
I=imread('cameraman.tif');
imshow(I);
X=grayslice(I,16);%thresholds the intensity image I using
%threshold values 1/16,2/16,…..,15/16,returning an indexed %image in X
figure;
imshow(X,hot(16));
?
3.假彩色增強處理程序舉例
[RGB]=imread('ghost.bmp');
imshow(RGB);
RGBnew(:,:,1)=RGB(:,:,3);
RGBnew(:,:,2)=RGB(:,:,1);
RGBnew(:,:,3)=RGB(:,:,2);
figure;
subplot(121);
imshow(RGB);
subplot(122);
imshow(RGBnew);
[RGB]=imread('ghost.bmp');
imshow(RGB);
RGBnew(:,:,1)=RGB(:,:,3);
RGBnew(:,:,2)=RGB(:,:,1);
RGBnew(:,:,3)=RGB(:,:,2);
figure;
subplot(121);
imshow(RGB);
subplot(122);
imshow(RGBnew);
?