Watershed算法中的负值导致黑色图像

时间:2016-08-22 16:35:27

标签: matlab image-processing 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;之后整个图像变暗。我相信这是因为图像像素都是-inf或一些较小的负数。这是有一种解决方法,如果是这样,我可以改变我的代码,以使这个算法工作?我已经尝试了很多,我真的不知道发生了什么。任何帮助都会很棒!

1 个答案:

答案 0 :(得分:2)

问题在于您的generateDivisorsTraditional命令。正如您在评论中所说,它使用了Inner。如果您直接尝试show,您会看到您也会看到一张黑色图片。但是,如果您使用适当的限制来调用它:

imshow

你会看到你期望看到的一切。

一般来说,我通常更喜欢imagesc。 imshow对于代表什么范围做出任意判断,我通常不会为了跟上它而烦恼。我认为在您的情况下,您的结束图片为imshow(clean_img,[min(clean_img(:)), max(clean_img(:))]) ,因此imshow选择代表范围uint16。由于您的所有像素值都低于400,因此在该范围内肉眼看起来是黑色的。