2015-07-21 150 views
-1

我有一個二進制圖像,我想刪除白線大於閾值(如50像素)。如何在二值圖像中去除大於閾值的白線? (matlab)

原始圖像:

enter image description here

輸入和輸出的圖像:

enter image description here

我的想法:

我要算位於每一個行,如果白色像素((白色像素數>閾值))然後刪除該行。

編輯完成我的代碼。

close all;clear all;clc; 

    I =imread('http://www.mathworks.com/matlabcentral/answers/uploaded_files/34446/1.jpg'); 
    I=im2bw(I); 
    figure, 
    imshow(I);title('original'); 
    ThresholdValue=50; 
    [row,col]=size(I); 
    count=0;  % count number of white pixel 
    indexx=[]; % determine location of white lines which larger.. 
    for i=1:col 
     for j=1:row 

      if I(i,j)==1 
       count=count+1; %count number of white pixel in each line 
     % I should determine line here 
     %need help here 
      else 
       count=0; 
       indexx=0; 
      end 
      if count>ThresholdValue 
      %remove corresponding line 
      %need help here 
      end 
     end 
    end 
+0

@Andy Jones我應該刪除我的問題?我有一個problrm,有人幫我解決它。 –

回答

1

只有一小部分丟失,您還必須檢查是否達到行的末尾:

if count>ThresholdValue 
     %Check if end of line is reached 
     if j==row || I(i,j+1)==0 
      I(i,j-count+1:j)=0; 
     end 
    end 

有關評論

更新代碼:

I =imread(pp); 
I=im2bw(I); 
figure, 
imshow(I);title('original'); 
ThresholdValue=50; 
[row,col]=size(I); 
count=0;  % count number of white pixel 
indexx=[]; % determine location of white lines which larger.. 
for i=1:row %row and col was swapped in the loop 
    count=0; %count must be retest at the beginning of each line 
    for j=1:col %row and col was swapped in the loop 

     if I(i,j)==1 
      count=count+1; %count number of white pixel in each line 
      % I should determine line here 
      %need help here 
     else 
      count=0; 
      indexx=0; 
     end 
     if count>ThresholdValue 
      %Check if end of line is reached 
      if j==col || I(i,j+1)==0 
       I(i,j-count+1:j)=0; 
      end 
     end 
    end 
end 
imshow(I) 
+0

非常感謝您的幫助,您能否告訴我爲什麼我在這個例子中出錯? 2end示例圖像:https://www.dropbox.com/s/n5sdx26drbsu6ay/sample.jpg?dl=0 –

+1

@KaroAmini:'count'必須在每行之後重置,並且您交換了行和列。後者在第一種情況下不會造成問題,因爲您的圖像是平方的。 – Daniel

+0

,我非常感謝你爲我所做的一切,讓你陷入困境。 –