數字圖像處理(源碼)

 
數字圖像處理
1.
灰度變換是圖像增強的一種重要手段,使圖像對比度擴展,圖像更加清晰,特徵更加明顯。
灰度級的直方圖給出了一幅圖像概貌的描述,通過修改灰度直方圖來得到圖像增強。
1計算出一幅灰度圖像的直方圖
clear
close all
I=imread('004.bmp');
imhist(I)
title('
實驗一(1直方圖');
2對灰度圖像進行簡單的灰度線形變換
figure
subplot(2,2,1)
imshow(I);
title('
試驗2-灰度線性變換');
subplot(2,2,2)
histeq(I);
3看其直方圖的對應變化和圖像對比度的變化
原圖像 f(m,n) 的灰度範圍 [a,b] 線形變換爲圖像 g(m,n),灰度範圍[a,b]
公式: g(m,n)=a+(b-a)* f(m,n) /(b-a)
figure
subplot(2,2,1)
imshow(I)
J=imadjust(I,[0.3,0.7],[0,1],1);
title('
實驗一(3)用 g(m,n)=a+(b-a)* f(m,n) /(b-a)進行變換');
subplot(2,2,2)
imshow(J)
subplot(2,2,3)
imshow(I)
J=imadjust(I,[0.5 0.8],[0,1],1);
subplot(2,2,4)
imshow(J)
(4) 圖像二值化(選取一個域值,將圖像變爲黑白圖像)
figure
subplot(2,2,1)
imshow(I)
J=find(I<150);
I(J)=0;
J=find(I>=150);
I(J)=255;
title('
實驗一(4)圖像二值化 ( 域值爲150 ');
subplot(2,2,2)
imshow(I)
clc;
I=imread('14499.jpg');
bw=im2bw(I,0.5);%
選取閾值爲0.5
figure;
imshow(bw) %
顯示二值圖象

2. 圖象處理變換
1傅立葉變換
熟悉其概念和原理,實現對一幅灰度圖像的快速傅立葉變換,並求其變換後的係數分佈.
2離散餘弦變換
熟悉其概念和原理,實現對一幅灰度和彩色圖像作的離散餘弦變換,選擇適當的DCT係數閾值對其進行DCT反變換.
%
圖象的FFT變換
clc;
I=imread('005.bmp');
subplot(1,2,1)
imshow(I);
title('
原圖');
subplot(1,2,2)
imhist(I);
title('
直方圖');
colorbar;
J=fft2(I);
figure;
subplot(1,2,1)
imshow(J);
title('FFT
變換結果');
subplot(1,2,2)
K=fftshift(J);
imshow(K);
title('
零點平移');
figure;
imshow(log(abs(K)),[]),colormap(jet(64)),colorbar;
title('
係數分佈圖');
%
圖象的DCT變換
RGB=imread('005.bmp');
figure;
subplot(1,2,1)
imshow(RGB);
title('
彩色原圖');
a=rgb2gray(RGB);
subplot(1,2,2)
imshow(a);
title('
灰度圖');
figure;
b=dct2(a);
imshow(log(abs(b)),[]),colormap(jet(64)),colorbar;
title('DCT
變換結果');
figure;
b(abs(b)<10)=0;
% idct
c=idct2(b)/255;
imshow(c);
title('IDCT
變換結果');

3. 小波變換
實驗內容:熟悉小波變換的概念和原理,熟悉matlab小波工具箱主要函數的使用.利用二維小波分析對一幅圖象作2層小波分解,並在此基礎上提取各層的低頻信息實現圖像的壓縮.
程序如下:
clc
close all
clear
a=imread('005.bmp');
subplot(1,2,1);
imshow(a);
title('
原始圖象');
I=rgb2gray(a);
subplot(1,2,2);
imshow(I);
title('
原始圖象的灰度圖');
%
進行二維小波變換
[a,b] = wavedec2(I, 2, 'bior3.7');
%
提取各層低頻信息
figure;
c = appcoef2( a, b, 'bior3.7', 1 );
subplot(1,2,1);
imshow(c, []);
title('
一層小波變換結果');
d = appcoef2( a, b, 'bior3.7', 2 );
subplot(1,2,2);
imshow(d, []);
title('
二層小波變換結果');

4. 模板運算
一、實驗內容:
1)平滑:平滑的目的是模糊和消除噪聲。平滑是用低通濾波器來完成,在空域中全是正值。
2)銳化:銳化的目的是增強被模糊的細節。銳化是用高通濾波器來完成,在空域中,接近原點處爲正,在遠離原點處爲負。
利用模板進行圖象增強就是進行模板卷積。
1
利用二個低通鄰域平均模板(3×39×9)對一幅圖象進行平滑,驗證模板尺寸對圖象的模糊效果的影響。
2
利用一個低通模板對一幅有噪圖象(GAUSS白噪聲)進行濾波,檢驗兩種濾波模板(分別使用一個5×5的線性鄰域平均模板和一個非線性模板:3×5中值濾波器)對噪聲的濾波效果。
3
選擇一個經過低通濾波器濾波的模糊圖象,利用sobelprewitt水平邊緣增強高通濾波器(模板)對其進行高通濾波圖象邊緣增強,驗證模板的濾波效果。
4
選擇一幅灰度圖象分別利用一階Sobel算子和二階Laplacian算子對其進行邊緣檢測,驗證檢測效果。
二、實驗步驟:
1
、利用低通鄰域平均模板進行平滑:
I=imread('girl.bmp');
subplot(1,3,1);
imshow(I);
title('
原圖');
J=fspecial('average');
J1=filter2(J,I)/255;
subplot(1,3,2);
imshow(J1);
title('3*3
濾波');
K=fspecial('average',9);
K1=filter2(K,I)/255;
subplot(1,3,3);
imshow(K1);
title('9*9
濾波');
2
、中值濾波和平均濾波
I=imread('girl.bmp');
J=imnoise(I,'gaussian',0,0.01);
subplot(2,2,1);
imshow(I);
title('
原圖');
subplot(2,2,2);
imshow(J);
title('noise');
K=fspecial('average',5);
K1=filter2(K,J)/255;
subplot(2,2,3);
imshow(K1);
title('average');
L=medfilt2(J,[3 5]);
subplot(2,2,4);
imshow(L);
title('medium');
3
、高通濾波邊緣增強
I=imread('girl.bmp');
subplot(2,2,1);
imshow(I);
title('original pic');
J=fspecial('average',3);
J1=conv2(I,J)/255;
%J1=filter2(J,I)/255;
subplot(2,2,2);
imshow(J1);
title('3*3lowpass');
K=fspecial('prewitt');
K1=filter2(K,J1)*5;
subplot(2,2,3);
imshow(K1);
title('prewitt');
L=fspecial('sobel');
L1=filter2(L,J1)*5;
subplot(2,2,4);
imshow(L1);
title('sibel');
4
、邊緣檢測
分別用sobellaplacian算子來進行,程序如下:
I=imread('girl.bmp');
subplot(1,3,1);
imshow(I);
title('original pic');
K=fspecial('laplacian',0.7);
K1=filter2(K,I)/100;
subplot(1,3,2);
imshow(K1);
title('laplacian');
L=fspecial('sobel');
L1=filter2(L,I)/200;
subplot(1,3,3);
imshow(L1);
title('sibel');

5. 圖像分割
實驗目的:1學習邊緣檢測
2
學習灰度閥值分割
實驗內容:
1
、分別用sobelLaplacian-Gaussian方法對一幅灰度圖像進行邊緣提取,2、給出對比結果
i=imread('eight.tif');
figure;
subplot(2,2,1);
imshow(i);
title('
原始圖像');
subplot(2,2,3);
imshow(i);
title('
原始圖像');
i1=edge(i,'sobel');
subplot(2,2,2);
imshow(i1);
title('sober
方法提取的邊緣');
i2=edge(i,'log');
subplot(2,2,4);
imshow(i2);
title('Laplacian-Gaussian
方法提取的邊緣');
比較提取邊緣的效果可以看出,sober算子是一種微分算子,對邊緣的定位較精確,但是會漏去一些邊緣細節。而Laplacian-Gaussian算子是一種二階邊緣檢測方法,它通過尋找圖象灰度值中二階過零點來檢測邊緣並將邊緣提取出來,邊緣的細節比較豐富。通過比較可以看出Laplacian-Gaussian算子比sober算子邊緣更完整,效果更好。
3
、利用雙峯法對一幅灰度圖像進行灰度分割處理
i=imread('eight.tif');
subplot(1,2,1);
imhist(i);
title('
原始圖像直方圖');
thread=130/255;
subplot(1,2,2);
i3=im2bw(i,thread);
imshow(i3);
title('
分割結果');
根據原圖像的直方圖,發現背景和目標的分割值大約在130左右,並將灰度圖像轉爲二值圖像,分割效果比較理想。

6. 圖像壓縮與編碼
實驗目的:學習JPEG壓縮編碼
實驗內容:
一.實現基本JPEG的壓縮和編碼分三個步驟:
1
首先通過DCT變換去除數據冗餘;
2
使用量化表對DCT係數進行量化;
3
對量化後的係數進行Huffman編碼。
具體源程序由主程序及兩個子程序(DCT量化、Huffman編碼)組成:
1
.主程序
I=imread('autumn.tif');
yiq=rgb2ntsc(I);
my=[16 11 10 16 24 40 51 61;12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 56;14 17 22 29 51 87 80 62;
18 22 37 56 68 109 103 77;24 35 55 64 81 104 113 92;
49 64 78 87 103 121 120 101;72 92 95 98 112 100 103 99];
miq=[17 18 24 47 99 99 99 99;18 21 26 66 99 99 99 99;
24 26 56 99 99 99 99 99;47 66 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;99 99 99 99 99 99 99 99];
I1=yiq(:,:,1);I2=(:,:,2);
[m n]=size(I1);
t1=8;ti1=1;
while(t1
t1=t1+8;ti1=ti1+1;
end
t2=8;ti2=1;
while(t2
t2=t2+8;ti2=ti2+1;
end
times=0;
for k=0:ti1-2
for j=0:ti2-2
dct8x8(I1(k*8+1:k*8+8,j*8+1:j*8+8),my,times*64+1);
dct8x8(I2(k*8+1:k*8+8,j*8+1:j*8+8),miq,times*64+1);
times=times+1;
end
block(I2(k*8+1:k*8+8,j*8+1:t2),[8 8], 'dctmtx(8)');
end
for j=0:ti2-2
dct8x8(I1(k*8+1:t1,j*8+1:j*8+8),times*64+1);
times=times+1;
end
dct8x8(I1(k*8+1:t1,j*8+1:t2),times*64+1);
2
function dct8x8(I,m,s) %定義DCT量化子程序
T=inline('dctmtx(8)');
y=blkproc(I,[8 8],T);
y=round(y./m);
p=1;te=1;
while(p<=64)
for q=1:te
y1(s+p)=y(te-q+1,q);p=p+1;
end
for q=te:-1:1
y1(s+p)=y(te-q+1,q);p=p+1;
end
end
f=haffman(y1);
c(s:s+64,1)=f(:,1);c(s:s+64,2)=f(:,2);c(s:s+64,3)=f(:,3)
3
function c=haffman(I) %定義Huffman編碼子程序
[m,n]=size(I);
p1=1;s=m*n;
for k=1:m
for h=1:n
f=0;
for b=1:p1-1
if(c(b,1)==I(k,h)) f=1;break;end
end
if(f==0) c(p1,1)=I(k,h);p1=p1+1;end
end
end
for g=1:p1-1
p(g)=0;c(g,2)=0;
for k=1:m
for h=1:n
if(c(g,1)==I(k,h)) p(g)=p(g)+1;end
end
end
p(g)=p(g)/s;
end
pn=0;po=1;
while(1)
if(pn>=1.0) break;
else
[pm p2]=min(p(1:p1-1));p(p2)=1.1;
[pm2,p3]=min(p(1:p1-1));p(p3)=1.1;
pn=pm+pm2;p(p1)=pn;
tree(po,1)=p2;tree(po,2)=p3;
po=po+1;p1=p1+1;
end
end
for k=1:po-1
tt=k;m1=1;
if(or(tree(k,1)<9,tree(k,2)<9))
if(tree(k,1)<9)
c(tree(k,1),2)=c(tree(k,1),2)+m1;
m2=1;
while(tt
m1=m1*2;
for h=tt:po-1
if(tree(h,1)==tt+g)
c(tree(k,1),2)=c(tree(k,1),2)+m1;
m2=m2+1;tt=h;break;
elseif(tree(h,2)==tt+g)
m2=m2+1;tt=h;break;
end
end
end
c(tree(k,1),3)=m2;
end
tt=k;m1=1;
if(tree(k,2)<9)
m2=1;
while(tt
m1=m1*2;
for h=tt:po-1
if(tree(h,1)==tt+g)
c(tree(k,2),2)=c(tree(k,2),2)+m1;
m2=m2+1;tt=h;break;
elseif(tree(l,2)==tt+g)
m2=m2+1,tt=h;break;
end
end
end
c(tree(k,2),3)=m2;
end
end
end
二.JPEG2000採用小波變換編碼,
小波變換壓縮編碼實現程序爲
load wbarb;
subplot(2,2,1),image(X);colormap(map)
title('
原始圖象');
[c,s]=wavedec2(X,2, 'bior3.7');
thr=20;
ca1=appcoed2(c,s, 'bior3.7',1);
ch1=detcoef2('h',c,s,1);
cv1=detcoef2('v',c,s,1);
cd1=detcoef2('d',c,s,1);
a1=wrcoef2('a',c,s, 'bior3.7',1);
h1=wrcoef2('h',c,s, 'bior3.7',1);
v1=wrcoef2('v',c,s, 'bior3.7',1);
d1=wrcoef2('d',c,s, 'bior3.7',1);
c1=[a1,h1,v1,d1];
ca1=appcoed2(c,s, 'bior3.7',1);
ca1=wcodemat(ca1,440, 'mat',0);
ca1=0.5*ca1
subplot(2,2,2),image(ca1)
title('
壓縮圖象一')
ca2=appcoed2(c,s, 'bior3.7',2);
ca2=wcodemat(ca2,440, 'mat',0);
ca2=0.5*ca2;
subplot(2,2,3),image(ca2)
title('
壓縮圖象二')

7. 應用KL變換進行圖象的特徵提取
一、實驗要求:應用KL變換進行圖象的特徵提取。熟悉MATLAB的相關命令。
二、實驗目的:掌握如何應用KL變換進行圖象的特徵提取。
三、實驗內容:選擇一幅稍大的灰度圖象(最好用紋理圖象),按下面步驟進行實驗:
1)應用9×9的窗口對上述圖象進行隨機抽樣,共抽樣200塊子圖象;
2)將所有子圖象按列相接變成一個81維的行向量;
3)對所有200個行向量進行KL變換,求出其對應的協方差矩陣的特徵向量和特徵值,按降序排列特徵值以及所對應的特徵向量;
4)選擇前40個最大特徵值所對應的特徵向量作爲主元,將原圖象塊向這40個特徵向量上投影,所獲得的投影係數就是這個子塊的特徵向量。
5)求出所有子塊的特徵向量。
四、實驗結果:
源程序如下:
clear
close all
clc
M=rand(200,200);
I=imread('a.bmp');
[mx,my]=size(I);
imshow(I);
title('
原始圖象');
for i=1:200
for j=1:199
if(ceil(M(i,j)*mx)
x(i)=ceil(M(i,j)*mx);
y(i)=ceil(M(i,j+1)*my);
end
end
end
for i=1:200
I1(:,:,i)=imcrop(I,[x(i),y(i),8,8]);
I2(:,i)=reshape(I1(:,:,i),1,81);
end
I2=double(I2);
C=I2'*I2/200;
[a,s]=eig(C);
A=a(161:200,:);
U=A*I2';
figure;
imshow(U);
title('
特徵向量');
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章