7.1 邊緣分割技術
7.1.1圖像中的線段
1 %檢測圖像中的線段 2 clear all; close all; 3 I=imread('gantrycrane.png'); 4 I=rgb2gray(I); %轉換為灰度圖像 5 h1=[-1, -1. -1; 2, 2, 2; -1, -1, -1]; %模板 6 h2=[-1, -1, 2; -1, 2, -1; 2, -1, -1]; 7 h3=[-1, 2, -1; -1, 2, -1; -1, 2, -1]; 8 h4=[2, -1, -1; -1, 2, -1; -1, -1, 2]; 9 J1=imfilter(I, h1); %線段檢測 10 J2=imfilter(I, h2); 11 J3=imfilter(I, h3); 12 J4=imfilter(I, h4); 13 J=J1+J2+J3+J4; %四條線段疊加 14 figure; 15 subplot(121); imshow(I); 16 subplot(122); imshow(J);
7.1.2微分算子
Roberts算子
Prewit算子
Soble算子
1 %----------Roberts算子---------- 2 clear all; close all; 3 I=imread('rice.png'); 4 I=im2double(I); 5 [J, thresh]=edge(I, 'roberts', 35/255); %邊緣檢測 6 figure; 7 subplot(121); imshow(I); 8 subplot(122); imshow(J); 9 10 %----------Prewit算子---------- 11 clear all; close all; 12 I=imread('cameraman.tif'); 13 I=im2double(I); 14 [J, thresh]=edge(I, 'prewitt', [], 'both'); 15 figure; 16 subplot(121); imshow(I); 17 subplot(122); imshow(J); 18 19 %----------Soble算子---------- 20 clear all; close all; 21 I=imread('gantrycrane.png'); 22 I=rgb2gray(I); 23 I=im2double(I); 24 [J, thresh]=edge(I, 'sobel', [], 'horizontal'); 25 figure; 26 subplot(121); imshow(I); 27 subplot(122); imshow(J); 28 29 % 采用fspecial()產生預定義的模板 30 clear all; close all; clc; 31 format rat; 32 hsobel=fspecial('sobel') %sobel算子 33 hprewitt=fspecial('prewitt') %prewitt算子 34 hlaplacian=fspecial('laplacian') %laplacian算子 35 hlog=fspecial('log', 3) %log算子 36 format short; %設置輸出數據格式 37 38 % 采用fspecial()和imfilter()提取圖像的邊緣 39 clear all; close all; 40 I=imread('cameraman.tif'); 41 I=im2double(I); 42 h=fspecial('laplacian'); 43 J=imfilter(I, h, 'replicate'); 44 K=im2bw(J, 80/255); %變成二值圖像 45 figure; 46 subplot(121); imshow(J); 47 subplot(122); imshow(K);
7.1.3 Canny算子
Canny算子具有低誤碼率、高精度和抑制虛假邊緣等優點
1 %采用Canny算子對含有噪聲的圖像進行邊緣檢測 2 clear all; close all; 3 I=imread('rice.png'); 4 I=im2double(I); 5 J=imnoise(I, 'gaussian', 0, 0.01); 6 [K, thresh]=edge(J, 'canny'); %canny算子檢測邊緣 7 figure; 8 subplot(121); imshow(J); 9 subplot(122); imshow(K);
7.1.4 LOG算子
首先采用Gaussian函數對圖像進行平黃,然后采用Laplacian算子根據二階導數過零點來檢測圖像邊緣,稱為LOG算子
拉普拉斯算子是一種不依賴於邊緣方向的二階微分算子,是標量,具有旋轉不變的性質;
對圖像的噪聲比較敏感;
優點:定位精度高,抗干擾能力強,連續性好
調用格式:
1 %采用log算子對含有噪聲的圖像進行邊緣檢測 2 clear all; close all; 3 I=imread('cameraman.tif'); 4 I=im2double(I); 5 J=imnoise(I, 'gaussian', 0, 0.005); 6 [K, thresh]=edge(J, 'log', [], 2.3); %log算子檢測邊緣 7 figure; 8 subplot(121); imshow(J); 9 subplot(122); imshow(K);
7.2 閾值分割技術
7.2.1 全局閾值
1 %采用全局閾值對圖像進行分割 2 I=imread('rice.png'); 3 J=I>120; %閾值為120 4 [width, height]=size(I); 5 for i=1:width 6 for j=1:height 7 if (I(i, j)>130) %閾值為130 8 K(i, j)=1; 9 else 10 K(i, j)=0; 11 end 12 end 13 end 14 figure; 15 subplot(121); imshow(J); 16 subplot(122); imshow(K);
1 %采用im2bw()進行彩色圖像分割 2 [X, map]=imread('trees.tif'); 3 J=ind2gray(X, map); %索引圖像轉換為灰度圖像 4 K=im2bw(X, map, 0.4); %圖像分割 5 figure; 6 subplot(121); imshow(J); 7 subplot(122); imshow(K);
7.2.2 Otsu閾值分割
該算法是在灰度直方圖的基礎上采用最小二乘法原理推導出來的,具有統計意義上的最佳分割。基本原理是以最佳閾值將圖像的灰度值分為兩個部分,使兩部分之間的方差最大,即具有最大的分離性。
1 %采用Otsu算法進行圖像分割 2 I=imread('coins.png'); 3 I=im2double(I); 4 T=graythresh(I); %獲取閾值 5 J=im2bw(I, T); %圖像分割 6 figure; 7 subplot(121); imshow(I); 8 subplot(122); imshow(J);
7.2.3 迭代式閾值分割
1 %采用迭代式閾值進行圖像分割 2 I=imread('cameraman.tif'); 3 I=im2double(I); 4 T0=0.01; %參數T0 5 T1=(min(I(:))+max(I(:)))/2; 6 r1=find(I>T1); 7 r2=find(I<=T1); 8 T2=(mean(I(r1))+mean(I(r2)))/2; 9 while abs(T2-T1)<T0 %迭代求閾值 10 T1=T2; 11 r1=find(I>T1); 12 r2=find(I<=T1); 13 T2=(mean(I(r1))+mean(I(r2)))/2; 14 end 15 J=im2bw(I, T2); %圖像分割 16 figure; 17 subplot(121); imshow(I); 18 subplot(122); imshow(J);
7.3 區域分割技術
7.3.1 區域生長法
一種串行區域分割的圖像分割方法。區域生長的基本思想是將具有相似性質的像素集合起來構成區域。區域增長方法根據同一物體區域內像素的相似性質來聚集像素點的方法,從初始區域開始將相鄰的即便有同樣性質的像素或其他區域歸並到目前的區域中從而逐步增長區域,直至沒有可以歸並的點或其他的小區域為止。區域內像素的相似度量可以包括平均灰度值、紋理和顏色等信息。
可用來分割比較復雜的圖像,如自然景物。缺點是采用迭代的方法,空間和時間開銷比較大,往往會照成過度分割,即將圖像分割成過多的區域。
區域生長的好壞取決於第三類,第一類為初始種子的選取,第二類為生長規則,第三類為終止條件。
7.3.2 分水嶺分割
借鑒了形態學理論。將一幅圖像堪稱是一個地形圖,灰度值對應地形的高度值,高灰度值對應着山峰,低灰度值對應山谷。水總是朝着地勢低的地方流動,直到某個局部低窪處,這個低窪就是盆地。最終所有的水會處於不同的盆地,盆地之間的山脊稱為分水嶺。
1 %采用分水嶺算法分割圖像 2 I=imread('circbw.tif'); 3 J=watershed(I, 8); %分水嶺分割 4 figure; 5 subplot(121); imshow(I); 6 subplot(122); imshow(J);