Reputation:
I have an image as follow:
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.
Upvotes: 3
Views: 899
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
Reputation: 1651
I present three approaches for this problem:
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:
2:
3:
All valid lines:
Innermost lines:
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
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