2017-03-08 104 views

回答

0

MATLAB有自己的執行Otsu thresholding稱爲multithresh。在你的情況下,代碼以獲得分割圖像應該是這樣的:

im=rgb2gray(I); % convert image to grayscale 
thresh = multithresh(im); % find one threshold (using Otsu method) 

segmented_im = imquantize(im, thresh); % segment image 
imagesc(segmented_im); % show segmented image 

我沒有測試過,所以我不知道這將如何在圖像上進行。

編輯:

我測試了它,並預期這是行不通的。其中一個問題是,當像素強度有明顯的雙峯分佈時,Otsu的方法效果很好。你的形象缺乏這種雙重性。灰度轉換後imhist(im)調用導致這個(由我添加的註釋):

enter image description here

正如你所看到的,分佈幾乎是三峯,並通過multithresh選擇的閾值是第一位的,而你想要第二個。我想到的第一個解決方法是(特別是如果數據集中的所有圖像與您發佈的圖像相似,即具有相似的亮度分佈),則使multithresh輸出兩個閾值,然後選擇最後一個(最高):

thresholds = multithresh(im, 2); 
thresh = thresholds(end); 

然後按上面所述進行圖像分割。這第二個方法導致該分段:

enter image description here

EDIT 2(把他們放在一起):

實際上輸出segmented_im不是二進制圖像,但標籤圖像。很容易將其轉換爲二進制圖像。我將直接在包含該下一個代碼段的所有的代碼:

im=rgb2gray(I); % convert image to grayscale 
thresholds = multithresh(im, 2); % find two thresholds using Otsu 
thresh = thresholds(end); % select larger one 

segmented_im = imquantize(im, thresh); % segment image 

segmented_im(segmented_im == 1) = 0; % make background black (0) 
segmented_im(segmented_im == 2) = 255; % make foreground white (255) 

binary_im = im2bw(segmented_im); % make binary (logical) image 

imshow(binary_im); % show binary image 

binary_im IL,所述邏輯矩陣假(0)爲背景,以及用於前景真(1)。 segmented_im是一個雙精度矩陣,0代表背景,255代表前景。我希望這符合你的目的!

+0

它不起作用! –

+0

我編輯了答案。問題可能在於像素強度的非雙峯分佈,這是標準單閾值Otsu方法很好地工作所需要的。 – UJIN

+0

先生,這是一個二進制圖像?使用Otsu方法,白色的地面和黑色背景在哪裏? –

相關問題