PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 6, 2022

[FIXED] How to judge the contour is line or curve by opencv?

 May 06, 2022     algorithm, image, image-processing, image-segmentation, opencv     No comments   

Issue

Test image

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing