1、用Prewitt算子檢測圖像的邊緣
I = imread('bacteria.BMP');
BW1 = edge(I,'prewitt',0.04);???????????? % 0.04為梯度閾值
figure(1);
imshow(I);
figure(2);
imshow(BW1);
2、用不同σ值的LoG算子檢測圖像的邊緣
I = imread('bacteria.BMP');
BW1 = edge(I,'log',0.003); % σ=2
imshow(BW1);title('σ=2')
BW1 = edge(I,'log',0.003,3); % σ=3
figure, imshow(BW1);title('σ=3')
3、用Canny算子檢測圖像的邊緣
I = imread('bacteria.BMP');
imshow(I);
BW1 = edge(I,'canny',0.2);
figure,imshow(BW1);
4、圖像的閾值分割
I=imread('blood1.tif');
imhist(I);????????? % 觀察灰度直方圖, 灰度140處有谷,確定閾值T=140
I1=im2bw(I,140/255); % im2bw函數需要將灰度值轉換到[0,1]范圍內
figure,imshow(I1);
5、用水線閾值法分割圖像
afm = imread('afmsurf.tif');figure, imshow(afm);
se = strel('disk', 15);
Itop = imtophat(afm, se); % 高帽變換
Ibot = imbothat(afm, se); % 低帽變換
figure, imshow(Itop, []);?? % 高帽變換,體現原始圖像的灰度峰值
figure, imshow(Ibot, []);?? % 低帽變換,體現原始圖像的灰度谷值
Ienhance = imsubtract(imadd(Itop, afm), Ibot);% 高帽圖像與低帽圖像相減,增強圖像
figure, imshow(Ienhance);
Iec = imcomplement(Ienhance); % 進一步增強圖像
Iemin = imextendedmin(Iec, 20); figure,imshow(Iemin) % 搜索Iec中的谷值
Iimpose = imimposemin(Iec, Iemin);
wat = watershed(Iimpose); % 分水嶺分割
rgb = label2rgb(wat); figure, imshow(rgb); % 用不同的顏色表示分割出的不同區域
6、對矩陣進行四叉樹分解
I = [ 1???? 1???? 1???? 1???? 2???? 3???? 6???? 6
1???? 1???? 2???? 1???? 4???? 5???? 6???? 8
1???? 1???? 1???? 1??? 10??? 15???? 7???? 7
1???? 1???? 1???? 1??? 20??? 25???? 7???? 7
20??? 22??? 20??? 22???? 1???? 2???? 3???? 4
20??? 22??? 22??? 20???? 5???? 6???? 7???? 8
20??? 22??? 20??? 20???? 9??? 10??? 11??? 12
22??? 22??? 20??? 20??? 13??? 14??? 15??? 16];
S = qtdecomp(I,5);
full(S)
7、將圖像分為文字和非文字的兩個類別
I=imread('4-11.jpg');
I1=I(:,:,1);
I2=I(:,:,2);
I3=I(:,:,3);
[y,x,z]=size(I);
d1=zeros(y,x);
d2=d1;
myI=double(I);
I0=zeros(y,x);
for i=1:x
for j=1:y
%歐式聚類
d1(j,i)=sqrt((myI(j,i,1)-180)^2+(myI(j,i,2)-180)^2+(myI(j,i,3)-180)^2);
d2(j,i)=sqrt((myI(j,i,1)-200)^2+(myI(j,i,2)-200)^2+(myI(j,i,3)-200)^2);
if (d1(j,i)>=d2(j,i))
I0(j,i)=1;
end
end
end
figure(1);
imshow(I);
% 顯示RGB空間的灰度直方圖,確定兩個聚類中心(180,180,180)和(200,200,200)
figure(2);
subplot(1,3,1);
imhist(I1);
subplot(1,3,2);
imhist(I2);
subplot(1,3,3);
imhist(I3);
figure(4);
imshow(I0);
8、形態學梯度檢測二值圖像的邊緣
I=imread('wrod213.bmp');
imshow(I);
I=~I;??????? % 腐蝕運算對灰度值為1的進行
figure, imshow(I);
SE=strel('square',3); % 定義3×3腐蝕結構元素
J=imerode(~I,SE);
BW=(~I)-J;??????? % 檢測邊緣
figure,imshow(BW);
9、形態學實例——從PCB圖像中刪除所有電流線,僅保留芯片對象
I=imread('circbw.tif');
imshow(I);
SE=strel('rectangle',[40 30]); % 結構定義
J=imopen(I,SE);??????????? % 開啟運算
figure,imshow(J);