Counting the number of pixels of each line in an image

For a project I need to find the point distance between lines in an image. The method I was thinking about was to convert the image to binary and count the number of white pixels on a black background in each line. I have attached my code and it works well provided the line is perfectly straight.

By the way, this is a sample image that will be analyzed. I am planning to binary / run a line detection algorithm to find the boundaries of the two green stripes in the wire as the distance between them is a concern.

(http://i.imgur.com/Q5Ef0eJ.jpg)

im = imread('http://i.imgur.com/lc8ESac.png'); %// Read image
imBinary = double(im2bw(im)); %// Just in case - Convert to binary, 
                              %// then make double for sum
histogram = sum(imBinary,2); %// Compute row-wise histograms
stem(1:size(imBinary,1), histogram); %// Plot this histogram
xlabel('Row number');
ylabel('White pixel count');
grid;
diffs = diff([0; histogram]);
threshold = 100; %// Define threshold here 
rows = find(diffs >= threshold);

      

This code returns a histogram and the spacing between the peaks indicates the spacing between the lines.

To fix the curved lines problem, I am thinking of using a method that parses the columns of the image (e.g. 100px), then it loops around the entire image (column 0-100, then 101-200 to the width of the image).

I have pseudocode here:

for (loop through rows) 
    for (loop for coloumns) 
        count pixels at p(row, col) 
    end loop for columns 
    column counter = column counter - 1
    if column counter <= 0 then save the number of pixels counter and set column counter = 10
end row loop 

      

However, as a major in ChemE, coding is not my strong point and I struggle with loops a lot.

I just want to account for the above code inside this loop so that it can break a curved line into many pseudo lines in order to be able to calculate the "mean" or series of discrete distances between lines.

Many thanks!

+3


source to share


2 answers


I've never run Matlab in my life, so I can't tell you the exact lines of code in Matlab ... however I can tell you how to remove horizontal lines in general terms.

You will simply collapse the image with the appropriate core that is horizontally oriented, so for your needs the core will be:

0       0       0
0.33    0.33    0.33
0       0       0

      

Thus, each pixel in the output image becomes the average of itself and its left and right neighbors.

Start Image

enter image description here

ImageMagick command:

convert lines.png -convolve "0,0,0,0.33,0.33,0.33,0,0,0" -threshold 99.99% horiz.jpg

      

Final image



enter image description here

By the way, your lines are actually 5 pixels, so you may need to run a larger kernel to remove them, say 9x9, where all coefficients are zero, except for the middle (5th) line, where they are all 1/9 (i.e. e. 0.111).

I worked with ImageMagick to resize your image to a vertical column one pixel wide with this command:

convert lines.png -resize 1x844! -threshold 50% txt:

      

which gives the following result showing the white lines are at offsets 149,218,476 lines from the top.

...
0,144: (0,0,0)  #000000  black
0,145: (0,0,0)  #000000  black
0,146: (0,0,0)  #000000  black
0,147: (255,255,255)  #FFFFFF  white
0,148: (255,255,255)  #FFFFFF  white
0,149: (255,255,255)  #FFFFFF  white    <- Centre of your line
0,150: (255,255,255)  #FFFFFF  white
0,151: (255,255,255)  #FFFFFF  white
0,152: (0,0,0)  #000000  black
0,153: (0,0,0)  #000000  black
0,154: (0,0,0)  #000000  black
...
0,213: (0,0,0)  #000000  black
0,214: (0,0,0)  #000000  black
0,215: (0,0,0)  #000000  black
0,216: (255,255,255)  #FFFFFF  white
0,217: (255,255,255)  #FFFFFF  white
0,218: (255,255,255)  #FFFFFF  white    <- Centre of your line
0,219: (255,255,255)  #FFFFFF  white
0,220: (255,255,255)  #FFFFFF  white
0,221: (0,0,0)  #000000  black
0,222: (0,0,0)  #000000  black
0,223: (0,0,0)  #000000  black
...
0,471: (0,0,0)  #000000  black
0,472: (0,0,0)  #000000  black
0,473: (0,0,0)  #000000  black
0,474: (255,255,255)  #FFFFFF  white
0,475: (255,255,255)  #FFFFFF  white
0,476: (255,255,255)  #FFFFFF  white    <- Centre of your line
0,477: (255,255,255)  #FFFFFF  white
0,478: (255,255,255)  #FFFFFF  white
0,479: (0,0,0)  #000000  black
0,480: (0,0,0)  #000000  black
0,481: (0,0,0)  #000000  black
...

      

Here is the "squidged" image - on a red background so you can see it:

enter image description here

0


source


collapse your histogram:

h_accu = conv(histogram,(1:10)/10);
figure;plot(h_accu)

      



here 10

served as the size of the window, it should be adjusted according to the straightness of your line.

btw, a more general way to find hough transform strings .

0


source







All Articles