2016-08-22 52 views
0

我正在使用分水嶺算法嘗試和分割觸摸核。典型的圖像可能看起來像: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或一些較小的負數。這是有解決方法,如果是這樣的話,我可以在我的代碼中更改什麼以獲得此算法的工作?我已經試驗了一大堆,我不知道發生了什麼。任何幫助將是偉大的!

+0

'show'命令的作用是什麼?我不認識它。這是2016年的事情嗎? –

+0

@tasosPapastylianou顯示是我寫的一個小函數 - 它實際上只封裝了'figure','imshow',並向圖中添加了一個標籤。它應該與'imshow' – Sam

回答

2

問題出在您的show命令。正如你在評論中所說的那樣,它使用了imshow。如果你直接嘗試imshow,你會看到你也會得到一個黑色的圖像。但是,如果您將其稱爲適當限制:

imshow(clean_img,[min(clean_img(:)), max(clean_img(:))]) 

您會看到您期望看到的所有內容。

一般來說,我通常更喜歡imagesc出於這個原因。 imshow對於代表什麼範圍做出了任意的判斷,而且我通常不會爲跟上它而煩惱。我認爲在你的情況下,你的最終形象是uint16,所以imshow選擇代表範圍[1, 65025]。由於所有像素值都低於400,因此它們在該範圍內肉眼看起來很黑。

+1

同樣的顯示,你也可以只執行'imshow(clean_img,[]);',它將自動計算最小值和最大值並分別縮放到[0,1]。 – rayryeng

+0

非常感謝,這有所幫助。但是,我現在正在使用另一種算法,其中'L = watershed(I_mod);'在後面: 'I_eq_c = imcomplement(I_eq)在'L'之後,圖像的像素全部設置爲1,因此全部是黑色的,有什麼想法? – Sam

+0

還有一個更通用的方法來分割這些?即使教程代碼使用的是複雜的圖像,我似乎也會得到不準確的結果。 – Sam