导致黑色图像的分水岭算法中的负值
问题描述:
我正在使用分水岭算法尝试和分割触摸核。典型的图像可能看起来像: 或本:导致黑色图像的分水岭算法中的负值
我想使用此代码应用分水岭算法:
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或一些较小的负数。这是有解决方法,如果是这样的话,我可以在我的代码中更改什么以获得此算法的工作?我已经试验了一大堆,我不知道发生了什么。任何帮助将是伟大的!
答
问题出在您的show
命令。正如你在评论中所说的那样,它使用了imshow
。如果你直接尝试imshow
,你会看到你也会得到一个黑色的图像。但是,如果您将其称为适当限制:
imshow(clean_img,[min(clean_img(:)), max(clean_img(:))])
您会看到您期望看到的所有内容。
一般来说,我通常更喜欢imagesc出于这个原因。 imshow
对于代表什么范围做出了任意的判断,而且我通常不会为跟上它而烦恼。我认为在你的情况下,你的最终形象是uint16
,所以imshow
选择代表范围[1, 65025]
。由于所有像素值都低于400,因此它们在该范围内肉眼看起来很黑。
'show'命令的作用是什么?我不认识它。这是2016年的事情吗? –
@tasosPapastylianou显示是我写的一个小函数 - 它实际上只封装了'figure','imshow',并向图中添加了一个标签。它应该与'imshow' – Sam