Search code examples
imagematlabbinarynoise-reduction

How can I remove white lines which is larger then Threshold value in binary image ? (matlab)


I have a binary image which I want to remove white lines larger then Threshold value (like 50 pixel).

original image:

enter image description here

input and output image :

enter image description here

My idea:

I want to count white pixels which located in each rows and if (( number of white pixel > threshold value )) then remove that line.

please Edit and complete my code.

  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

Solution

  • There is only a small part missing, you must also check if you reached the end of the line:

        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
    

    Updated code regarding the comment:

    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)