Matlab 分水岭算法中的负值导致黑色图像
我用分水岭算法来分割接触的细胞核。典型图像可能如下所示: 或者这个: 我尝试将分水岭算法应用于以下代码:Matlab 分水岭算法中的负值导致黑色图像,matlab,image-processing,watershed,Matlab,Image Processing,Watershed,我用分水岭算法来分割接触的细胞核。典型图像可能如下所示: 或者这个: 我尝试将分水岭算法应用于以下代码: 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 th
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)
show(RGB_img)
%转换为灰度图像
I=rgb2gray(RGB_img);
%取一个大小为10的圆盘的结构元素,进行形态变换
%尝试从图像中减去背景:top hat是
%从原始图像中减去打开的图像
%从图像中减去背景噪声的形态学变换
%Tophat是从原始图像中减去打开的图像。全部删除
%小于结构元素10的图像
I1=imtophat(I,strel('disk',10));
%增加对比度
I2=imadjust(I1);
%显示(I2,“对比度”)
%假设我们有背景和前景,并以此评估thresh
级别=灰度阈值(I2);
%基于灰度阈值的二值图像转换
BW=im2bw(I2级);
显示(BW,'C');
BW=BWAREOPEN(BW,8);
显示(BW,'C2');
BW=bwdist(BW),因此将其标记为0(黑色)
im(L==0)=0;
清洁度=L;
表演(清洁)
在
C=~BW之后代码>整个图像变暗。我相信这是因为图像像素都是-inf或更小的负数。这是一种解决方法,如果是这样的话,我可以在我的代码中做些什么来让这个算法工作呢?我已经做了很多实验,但我真的不知道发生了什么。任何帮助都会很好 问题在于您的show
命令。正如您在评论中所说,这在引擎盖下使用了imshow
。如果您直接尝试imshow
,您也会看到一张黑色图像。但是,如果您使用适当的限制来称呼它:
imshow(clean_img,[min(clean_img(:)), max(clean_img(:))])
你会看到你期望看到的一切
一般来说,出于这个原因,我更喜欢imagescimshow
对要表示的范围进行任意判断,我通常都懒得跟上。我认为在您的情况下,您的最终图像是uint16
,因此imshow
选择表示范围[1,65025]
。由于您的所有像素值都低于400,因此在该范围内,它们在肉眼看来是黑色的。问题在于您的show
命令。正如您在评论中所说,这在引擎盖下使用了imshow
。如果您直接尝试imshow
,您也会看到一张黑色图像。但是,如果您使用适当的限制来称呼它:
imshow(clean_img,[min(clean_img(:)), max(clean_img(:))])
你会看到你期望看到的一切
一般来说,出于这个原因,我更喜欢imagescimshow
对要表示的范围进行任意判断,我通常都懒得跟上。我认为在您的情况下,您的最终图像是uint16
,因此imshow
选择表示范围[1,65025]
。由于所有像素值都低于400,因此在该范围内,它们在肉眼看来是黑色的。show
命令的作用是什么?我不认识它。它是2016a的东西吗?@tasospacastylanou show是我写的一个小函数-它实际上只封装了figure
,imshow
,并向figure添加了一个标签。它应该是与imshow
相同的显示,show
命令做什么?我不认识它。它是2016a的东西吗?@tasospacastylanou show是我写的一个小函数-它实际上只封装了figure
,imshow
,并在figure上添加了一个标签。它应该与imshow
相同,但你也可以只做imshow(clean_img,[])
,它将自动计算最小值和最大值,并分别缩放到[0,1]
。非常感谢,这很有帮助。然而,我现在使用另一种算法,L=流域(I_mod)代码>位于:I_eq\u c=imcomplete(I_eq)代码>和I_mod=imimposem(I_eq_c,~bw4 | mask_em)
在L
之后,图像的像素都设置为1,因此再次变为黑色,有什么想法吗?还有没有更通用的方法来分割这些像素?即使教程代码使用了复杂的图像,我似乎也得到了不稳定的结果,没有一种通用的分割方法。还有人在做细胞分割博士。尝试使用imopen
而不是imtophat
,您将获得与您的案例更相关的结果,或许您可以从中吸取教训。一种方法是首先使用粗略的阈值粗略地分离形状,并且仅对“连接”单元应用分水岭算法;您可以将这些结构识别为未通过某种“凸度”测量阈值(例如面积/表面)的结构感谢@rayryeng,我倾向于避免imshow
,所以我一直忘了这一点!:pYou也可以只做imshow(clean_img,[])
,它将自动计算最小值和最大值,并分别缩放到[0,1]
。非常感谢,这很有帮助。然而,我现在使用另一种算法,L=流域(I_mod)代码>位于:I_eq\u c=imcomplete(I_eq)代码>和I_mod=imimposem(I_eq_c,~bw4 | mask_em)
在L
之后,图像的像素都设置为1,因此再次变为黑色,有什么想法吗?还有没有更通用的方法来分割这些像素?即使教程代码使用了复杂的图像,我似乎也得到了不稳定的结果,没有一种通用的分割方法。还有人在做细胞分割博士。尝试使用imopen
而不是imtophat
,您将获得与您的案例更相关的结果,或许您可以从中吸取教训。一种方法是首先使用粗略的阈值粗略地分离形状,并且仅对“连接”单元应用分水岭算法;您可以将这些结构识别为未通过某种“凸度”测量阈值(例如面积/表面)的结构