天天看點

基于分水嶺算法的圖像分割-Matlab版本

簡介

分水嶺算法是一種圖像區域分割法,分割的過程中将圖檔轉化為灰階圖,然後将灰階值看作是海拔,然後向較低點注水,這種基于地形學的解釋,我們着重考慮三種點:

基于分水嶺算法的圖像分割-Matlab版本

1)極小值點,該點對應一個盆地的最低點,當我們在盆地裡滴一滴水的時候,由于重力作用,水最終會彙聚到該點。注意:可能存在一個最小值面,該平面内的都是極小值點。

2)盆地的其它位置點,該位置滴的水滴會彙聚到局部最小點。

3)盆地的邊緣點,是該盆地和其它盆地交接點,在該點滴一滴水,會等機率的流向任何一個盆地。

基于分水嶺算法的圖像分割-Matlab版本

明白上述三種點之後,我們開始往盆地的極小值點注水,然後随着注水的深入,每一個極小值點慢慢的向外擴充,然後知道兩個盆地的水彙合,彙合處就是我們需要的分水嶺。

從下圖可以直覺了解一下,首先這三塊區域都含有極小值點

基于分水嶺算法的圖像分割-Matlab版本

然後逐漸填充就能獲得分水嶺(即分界線)

基于分水嶺算法的圖像分割-Matlab版本

得到分界線就能完成圖像分割

代碼實作

clear, close all;
clc;
%1.讀取圖像并求取圖像的邊界。
 
rgb = imread('1.png');%讀取原圖像
I = rgb2gray(rgb);%轉化為灰階圖像
figure; subplot(121)%顯示灰階圖像
imshow(I)
text(732,501,'Image courtesy of Corel','FontSize',7,'HorizontalAlignment','right')
hy = fspecial('sobel');%sobel算子,應用sobel算子銳化圖像
hx = hy';
Iy = imfilter(double(I), hy, 'replicate');%濾波求y方向邊緣
Ix = imfilter(double(I), hx, 'replicate');%濾波求x方向邊緣
gradmag = sqrt(Ix.^2 + Iy.^2);%求摸
subplot(122); imshow(gradmag,[]), %顯示梯度
title('Gradient magnitude (gradmag)')
 
%2. 直接使用梯度模值進行分水嶺算法:(往往會存在過的分割的情況,效果不好)
 
L = watershed(gradmag);%直接應用分水嶺算法
Lrgb = label2rgb(L);%轉化為彩色圖像
figure; imshow(Lrgb), %顯示分割後的圖像
title('Watershed transform of gradient magnitude (Lrgb)')%過分割現象
 
%3.分别對前景和背景進行标記:本例中使用形态學重建技術對前景對象進行标記,首先使用開操作,開操作之後可以去掉一些很小的目标。
%開和閉這兩種運算可以除去比結構元素小的特定圖像細節,同時保證不産生全局幾何失真。
%開運算可以把比結構元素小的突刺濾掉,切斷細長搭接而起到分離作用;
%閉運算可以把比結構元素小的缺口或孔填充上,搭接短的間斷而起到連接配接作用。
se = strel('disk', 20);%圓形結構元素,STREL('disk',R,N),R is the specified radius, When N is greater than 0, the disk-shaped structuring
                       %element is approximated by a sequence of N
Io = imopen(I, se);%形态學開操作
figure; subplot(121)
imshow(Io), %顯示執行開操作後的圖像
title('Opening (Io)')
Ie = imerode(I, se);%對圖像進行腐蝕,基本參數:待處理的輸入圖像以及結構元素對象
Iobr = imreconstruct(Ie, I);%形态學重建
subplot(122); imshow(Iobr), %顯示重建後的圖像
title('Opening-by-reconstruction (Iobr)')
Ioc = imclose(Io, se);%形态學關操作,首先膨脹,然後腐蝕,兩個操作使用同樣的結構元素
figure; subplot(121)
imshow(Ioc), %顯示關操作後的圖像
title('Opening-closing (Ioc)')
Iobrd = imdilate(Iobr, se);%對圖像進行膨脹,基本參數:待處理的輸入圖像和結構元素對象。
Iobrcbr = imreconstruct(imcomplement(Iobrd), ...
    imcomplement(Iobr));%形态學重建
Iobrcbr = imcomplement(Iobrcbr);%圖像求反
subplot(122); imshow(Iobrcbr), %顯示重建求反後的圖像,figure4
title('Opening-closing by reconstruction (Iobrcbr)')
%As you can see by comparing Iobrcbr with Ioc, 
%reconstruction-based opening and closing are more 
%effective than standard opening and closing at removing 
%small blemishes without affecting the overall 
%shapes of the objects. Calculate the regional maxima 
%of Iobrcbr to obtain good foreground markers. 
fgm = imregionalmax(Iobrcbr);%局部極大值
figure; imshow(fgm), %顯示重建後局部極大值圖像,figure5
title('Regional maxima of opening-closing by reconstruction (fgm)')
I2 = I; %前景标記圖與原圖疊加
I2(fgm) = 255;%局部極大值處像素值設為255
figure; imshow(I2), %在原圖上顯示極大值區域,figure6
title('Regional maxima superimposed on original image (I2)')
se2 = strel(ones(5,5));%結構元素
fgm2 = imclose(fgm, se2);%關操作
fgm3 = imerode(fgm2, se2);%腐蝕
fgm4 = bwareaopen(fgm3, 20);%開操作
I3 = I;
I3(fgm4) = 255;%前景處設定為255
figure; subplot(121)
imshow(I3)%顯示修改後的極大值區域,figure7
title('Modified regional maxima')
bw = im2bw(Iobrcbr, graythresh(Iobrcbr));%轉化為二值圖像
subplot(122); imshow(bw), %顯示二值圖像,figure7
title('Thresholded opening-closing by reconstruction')
 
%4. 進行分水嶺變換并顯示:

DL = watershed(Iobrcbr);%分水嶺變換
figure
imshow(DL)
title('分水嶺')
labels = imdilate(DL==0,ones(3,3));
I4 = labeloverlay(I,labels);
figure
imshow(I4)
title('分水嶺邊界線')

%% 
% 将标簽矩陣顯示為彩色圖像。标簽矩陣
Lrgb = label2rgb(DL,'jet','w','shuffle');
figure
imshow(Lrgb)
title('分水嶺Label')
%%
figure
imshow(I)
hold on
himage = imshow(Lrgb);
himage.AlphaData = 0.2; % 使用透明方式将此僞顔色标簽矩陣疊加到原始強度圖像上
title('彩色Label')           

複制

基于分水嶺算法的圖像分割-Matlab版本