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.
int posCntr = 0;
int negCntr = 0;
var diffY = new List<List<int>>();
var diff2Y = new List<List<int>>();
var diffSeq = new List<List<int>>();
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++)
{
diffY[n].Add(validContours[n][i + 1].Y - validContours[n][i].Y);
if (diffY[n][i] >= 0)
{
posCntr++;
if (negCntr != 0)
{
diffSeq[n].Add(negCntr);
negCntr = 0;
}
}
else
{
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)
{
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];
}
}