Issue
This image have two contours. I can find both with opencv findcontour
function. What I want to know is how to judge which contour is line and which contour is curve ? Can anybody please tell how to do it?
Solution
Start by assuming you have a line, and apply some basic algebra.
First find the slope of the line and the y-intercept. The slope of a line is defined as the change in y divided by the change in x. So given two points (x0,y0), (x1,y1):
slope = (y0-y1) / (x0-x1)
The y-intercept is found using the slope-intercept equation (y=mx+b)
and solving for b:
y = mx + b
b = y - mx
So
y_intercept = y0 - slope * x0
Once you have the slope and the y-intercept, you just need to loop through the points of your contour and see if all of the points fall on the same line. If they do, you have a line; if they don't, you have a curve.
Since I don't know what language you're working with, here is the whole process in pseudocode:
// First assume you have a line - find the slope and y-intercept
slope = (point[0].y - point[1].y) / (point[0].x - point[1].x);
y_intercept = point[0].y - (slope * point[0].x);
// Using slope-intercept (y = mx + b), see if the other points are on the same line
for (n = 0 to numPoints)
{
if ((slope * point[n].x + y_intercept) != point[n].y)
{
// You've found a point that's not on the line - as soon as you
// find a point that's not on the line, you know that the contour
// is not a straight line
}
}
Note that you'll be dealing with floating-point numbers here, so you'll have to take that into account in the if
condition - you can't directly compare floating point numbers for equality, so you'll need to round them to some acceptable degree of accuracy. I left that out to keep the pseudocode simple.
Answered By - Jeff Loughlin Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.