Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
Hello everyone,

For my image processing project i need to find curve points in a contour.

In the example image shown below, detected contour is shown in Red. Now i need to find the curve points/coordinates (indicated with blue arrows) on horizontal and/or vertical axis.

http://imageshack.com/a/img540/7162/3NEzhC.jpg[^]

The curves may not necessarily be perfect ellipses.

Can anyone please let me know a good approach to do so.
Some reference code block would be much appreciated but an explanation alone would also be helpful.
Posted
Updated 3-Aug-15 2:51am
v4
Comments
Afzaal Ahmad Zeeshan 2-Aug-15 7:29am    
That is even algorithm-less problem. Only the algorithm that generates the shape would be able to find a solution. Do you have that?
Sergey Alexandrovich Kryukov 2-Aug-15 8:06am    
Exactly. The inquirer has to show us the code sample performing the recognition, as simple as possible.
—SA
Abhishrek 2-Aug-15 7:35am    
Thanks for the comment, but no algorithm is generating is the shape. Contours are extracted from life time images.
And, there can be some algorithms to do so. One lousy trick can be to use GetConvexityDefacts() then using "defect.StartPoint" & "defect.EndPoint".
Sergey Alexandrovich Kryukov 2-Aug-15 8:09am    
This is nonsense. "are extracted" means there is an algorithm. And it follows some model used to represent the recognition result, such as "convex hull". But your example is different. Is it just the set of overlapping ellipsis? But then the problem simply does not exist, you already have all you need. Doesn't it look like you only look at the resulting rendering image, not the data returned by the algorithm?
—SA
Abhishrek 2-Aug-15 8:48am    
How i extracted the contour is irrelevant to this question.
The question is related to post processing of contour.
But if you still want to know, i did the regular 3 step contour extracting process.
Color filter -> Threshold Binarization -> Extract contour

1 solution

Hello Everyone


I studied a bit and found that taking the first order derivative of the contour (for x and for y separately) can solve this problem.


The first order derivative is used to find Local Maxima and Minima. On the curve points the value of the first order derivative changes from positive to negative (for a peak) and from negative to positive (for a valley).


A basic definition of the first-order derivative of a one-dimensional function f(x) is the difference


df/dx = f(x + 1) - f(x)


To determine valley and peaks (curve points on horizontal axis), only Y value of the contour points is required. Similarly to determine curve points on vertical axis, only X value of the contour points is required.


I am putting here a small part of code used to determine only valleys (Minima) in a contour.


Any suggestion to improve the approach or the code is very much welcome.


C#
int posCntr = 0;
int negCntr = 0;
var diffY = new List<List<int>>();
var diff2Y = new List<List<int>>();
var diffSeq = new List<List<int>>();
// Process each detected contour
for (int n = 0; n < validContours.Count; n++)
{
    posCntr = 0;
    negCntr = 0;
    diffY.Add(new List<int>());
    diffSeq.Add(new List<int>());
    for (int i = 0; i < validContours[n].Total - 1; i++)
    {
        // Taking first derivative of contour
        // df/dy = f(y+1) - f(y);
        diffY[n].Add(validContours[n][i + 1].Y - validContours[n][i].Y);
        // count continuous positive and negative values and add them to a list
        // list will have consecutive positives, negatives, positives, negatives, ...
        if (diffY[n][i] >= 0)
        {
            posCntr++;
            if (negCntr != 0)
            {
                diffSeq[n].Add(negCntr);
                negCntr = 0;
            }
        }
        else //if (diffY[n][i] < 0)
        {
            negCntr++;
            if (posCntr != 0)
            {
                diffSeq[n].Add(posCntr);
                posCntr = 0;
            }
        }
    }
    if (negCntr != 0)
    {
        diffSeq[n].Add(negCntr);
        negCntr = 0;
    }
    else if (posCntr != 0)
    {
        diffSeq[n].Add(posCntr);
        posCntr = 0;
    }
}
int cntr;
for (int n = 0; n < validContours.Count; n++)
{
    cntr = 0;
    for (int i = 0; i < diffSeq[n].Count - 1; i = i + 2)
    {
        // Check if the consequtive pos, neg seq counts are above a threshold level
        // if they are, means we have found a Minima
        if (diffSeq[n][i]     >= Constants.CURVE_SZ && 
            diffSeq[n][i + 1] >= Constants.CURVE_SZ)
            Console.WriteLine(validContours[n][cntr + diffSeq[n][i]].ToString());
        cntr += diffSeq[n][i] + diffSeq[n][i+1];
    }
}
 
Share this answer
 
Comments
Abhishrek 5-Aug-15 6:43am    
where validContours is defined as:
var validContours = new List < Contour < Point > > ( );
Sergey Alexandrovich Kryukov 5-Aug-15 10:43am    
Sure, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900