-1

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
JustinBlaber
  • 4,629
  • 2
  • 36
  • 57
Karo Amini
  • 51
  • 1
  • 9

1 Answers1

1

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)
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • thanks indeed for your help, could u plz tell me why I'm getting error in this example? 2end example image: https://www.dropbox.com/s/n5sdx26drbsu6ay/sample.jpg?dl=0 – Karo Amini Jul 21 '15 at 20:51
  • 1
    @KaroAmini: `count` must be reset after each row and you swapped row and col. The later did not cause problems in the first case because your image was squared. – Daniel Jul 21 '15 at 21:12
  • , I do appreciate you for what you have done for me.i put you in trouble. – Karo Amini Jul 21 '15 at 21:22