分水岭算法设置删除所有连接的组件

时间:2016-08-18 21:33:00

标签: matlab image-processing image-segmentation watershed

我使用分水岭算法尝试分割触摸核。典型图片可能如下所示:enter image description here 或者:enter image description here

我尝试使用此代码应用分水岭算法:

show(RGB_img)


%Convert to grayscale image
I = rgb2gray(RGB_img);

%Take structuring element of a disk of size 10, for the morphological transformations
%Attempt to subtract the background from the image: top hat is the
%subtraction of the open image from the original


%Morphological transformation to subtract background noise from the image
%Tophat is the subtraction of an opened image from the original. Remove all
%images smaller than the structuring element of 10
I1 = imtophat(I, strel('disk', 10));

%Increases contrast
I2 = imadjust(I1);
%show(I2,'contrast')
%Assume we have background and foreground and assess thresh as such 
level = graythresh(I2);
%Convert to binary image based on graythreshold
BW = im2bw(I2,level);
show(BW,'C');



BW = bwareaopen(BW,8);
show(BW,'C2');

BW = bwdist(BW) <= 1;
show(BW,'joined');
%Complement because we want image to be black and background white
C = ~BW;
%Use distance tranform to find nearest nonzero values from every pixel
D = -bwdist(C);

%Assign Minus infinity values to the values of C inside of the D image
%   Modify the image so that the background pixels and the extended maxima
%   pixels are forced to be the only local minima in the image (So you could
%   hypothetically fill in water on the image

D(C) = -Inf;

%Gets 0 for all watershed lines and integers for each object (basins)
L = watershed(D);
show(L,'L');

%Takes the labels and converts to an RGB (Using hot colormap)
fin = label2rgb(L,'hot','w');

% show(fin,'fin');
im = I;

%Superimpose ridgelines,L has all of them as 0 -> so mark these as 0(black)
im(L==0)=0;

clean_img = L;
show(clean_img)

C = ~BW;之后无论出于何种原因,整个图像都会变暗。这个相同的代码块已经处理了一些其他的图像,所有这些图像都更加坚固,并且#34;或不像这些颗粒状。但是,我认为我用BW = bwdist(BW) <= 1;补偿了这一点。我已经尝试了很多,但我真的不知道发生了什么。任何帮助都会很棒!

聚苯乙烯。这是BW = bwareaopen(BW,8);之后的图片 Image description here

1 个答案:

答案 0 :(得分:0)

在礼帽之前,你应该进行关闭和开口以减少噪音。

如果在嘈杂的图像上执行区域打开,最终可能会在黑白图像上显示结果。

所以它会是:

  1. 结束并开启
  2. 顶帽
  3. 如果仍有需要开放区域
  4. 阈值
  5. 分别找到内部和外部标记的侵蚀和扩张
  6. 流域(从不使用没有标记的分水岭)。