user6112462
user6112462

Reputation:

How can I find the line in a binary image?

I have an image as follow:

original image

How can I use a built-in function to find the line within the white area? In fact, I would like to obtain the vector of the line. The shown below is an example of a possible output image.

image with detected lines

Upvotes: 3

Views: 899

Answers (3)

david muñoz
david muñoz

Reputation: 31

I'll just post you the code (I hope this helps even if it isn't strictly the answer):

I = image;
line = [];
count = 0;
for i=1:size(I)(2)
  if sum(image(:,i) == 0) == 0
     count += 1;
     line(count) = i;
  end
end

Upvotes: 3

code-lukas
code-lukas

Reputation: 1651

I present three approaches for this problem:

  1. Standard Hough Line Transform
  2. Probabilistic Hough Line Transform
  3. My own approach with one additional boundary condition

To 3: As you are searching for perpendicular lines inside your mask, you can simply check which colums contain only binary 1s (as in: 'Where can I draw a straight line from top to bottom, without ever touching a 0). If there are multiple occurences, take the innermost possible line. This approach is very fast and produces results very similar to the ones you requested.

I did this in Python, in case it helps, I'll post the code.

Results:

1:

Results for approach 1

2:

Results for approach 2

3:

All valid lines:

Results for 3

Innermost lines:

Results for approach 3

I changed the colors for my approach to ensure better visibility, please note that your mask has white borders which are as well detected.

The cyan is equivalent to the binary mask in your example, the white lines to the red ones in your example, but with your color scheme it was rather hard to see.

Upvotes: 3

the devops guy
the devops guy

Reputation: 117

use the image as a 2D array; the algorithm will be something like that:

we have image[][] a 2D array representing the image.

boolean canBeAdded;
for(0<=j<=image.width){
     canBeAdded=true;
     for(0<=i<=image.height){
         if(image[i][j]!=#FFFFFF){
              canBeAdded=false;
         }
     }
     if(canBeAdded){
        // (0,j) is the starting point of the line 
     }
}

Upvotes: 1

Related Questions