2017-08-29 51 views
0

我二值化的圖像時,有一個問題:二值化圖像不具有良好的效果

enter image description here

圖中的助行器的二值化後丟失。有誰可以提供幫助嗎?這裏是我使用的代碼:

clc; 
clear; 
video = VideoReader('C:\Users\Small_Bird\Desktop\ch02_20170323193606~2.avi'); 
nFrames = video.NumberOfFrames; 
H = video.Height; 
W = video.Width; 

Rate = video.Preallocate movie structure. 

for frameNum = 3500:nFrames 
    P = read(video,frameNum); 
    grayImage=rgb2gray(P); 
    cannyEdge=edge(grayImage,'canny'); 
    [m,n]=size(grayImage); 
    for i=1:m 
     for j=1:n 
      if 1==cannyEdge(i,j) 
       h(i,j)=grayImage(i,j)+3; 
      else 
       h(i,j)=grayImage(i,j); 
      end 
     end 
    end 

    thresh=graythresh(h); 
    I2=im2bw(h,thresh); 
    subplot(2,2,1); 
    imshow(grayImage),title('original image'); 
    subplot(2,2,2); 
    imshow(cannyEdge),title('image after extracting edge'); 
    subplot(2,2,3); 
    imshow(h),title('image after strengthening edge'); 
    subplot(2,2,4); 
    imshow(I2),title('image after binaryzation'); 
end 
+0

代碼顯示出來。 –

回答

1

問題是im2bw閾值的選擇。您正在使用函數graythresh計算整個圖像的整體閾值,結果顯示只能成功地將圖像的黑色部分與圖像的灰色或更高部分分開。您需要選擇一個更高的閾值,可以是用於所有圖像的絕對值,也可以是每個圖像的某些特徵計算得出的值。

如果你有MATLAB版本R2016a或更新您計算使用'adaptive' method使用本地自適應閾值或者adaptthreshim2binarize(更換爲新版本中的功能im2bw)選項。這可能會給您比單純的全球門檻更好的結果。

+0

它似乎包括所有的邊緣黑點像素。但是,非常感謝! –