1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
public void DetectionContours(string Fichier)
{
Image<Bgr, byte> sourceImage = new Image<Bgr, byte>(Fichier);
Image<Gray, byte> grayImage = sourceImage.Convert<Gray, byte>();
Image<Gray, byte> binaryImage = grayImage.ThresholdBinary(new Gray(128), new Gray(255));
List<Point> contourPoints = new List<Point>();
using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
{
CvInvoke.FindContours(grayImage, contours, null, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
// Parcours des contours et récupération des points
for (int i = 0; i < contours.Size; i++)
{
VectorOfPoint contour = contours[i];
Point[] contourArray = contour.ToArray();
contourPoints.AddRange(contourArray);
}
}
if (AreSegmentsContained(contourPoints, 1000) == true)
{
richTextBox1.Text = "Forme OK";
}
else
{
richTextBox1.Text = "Forme NON OK";
}
}
bool AreSegmentsContained(List<Point> contour, double segmentLength)
{
for (int i = 0; i < contour.Count; i++)
{
Point pointA =contour[i];
Point pointB = contour[(i + 1) % contour.Count]; // Point suivant (ou le premier point si nous sommes à la fin)
// Calcul du vecteur orthogonal à la tangente au contour
int tangentX = -(pointB.Y - pointA.Y);
int tangentY = pointB.X - pointA.X;
// Normalisation du vecteur orthogonal
double length = Math.Sqrt(tangentX * tangentX + tangentY * tangentY);
tangentX = (int)(tangentX / length);
tangentY = (int)(tangentY / length);
// Création des deux segments perpendiculaires
int segment1StartX = pointA.X + tangentX * (int)(segmentLength / 2);
int segment1StartY = pointA.Y + tangentY * (int)(segmentLength / 2);
int segment1EndX = segment1StartX - tangentY * (int)segmentLength;
int segment1EndY = segment1StartY + tangentX * (int)segmentLength;
int segment2StartX = pointA.X - tangentX * (int)(segmentLength / 2);
int segment2StartY = pointA.Y - tangentY * (int)(segmentLength / 2);
int segment2EndX = segment2StartX - tangentY * (int)segmentLength;
int segment2EndY = segment2StartY + tangentX * (int)segmentLength;
List<Point> segment1Points = BresenhamLine(segment1StartX, segment1StartY, segment1EndX, segment1EndY);
List<Point> segment2Points = BresenhamLine(segment2StartX, segment2StartY, segment2EndX, segment2EndY);
// Vérification si tous les points des segments sont inclus dans le contour
bool segment1Contained = segment1Points.All(p => contour.Contains(p));
bool segment2Contained = segment2Points.All(p => contour.Contains(p));
if (segment1Contained && segment2Contained)
{
return true; // Les deux segments sont contenus dans le contour
}
}
return false; // Les deux segments ne peuvent pas être contenus dans le contour
}
List<Point> BresenhamLine(int x0, int y0, int x1, int y1)
{
List<Point> points = new List<Point>();
bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
if (steep)
{
(x0, y0) = (y0, x0);
(x1, y1) = (y1, x1);
}
bool swapped = false;
if (x0 > x1)
{
(x0, x1) = (x1, x0);
(y0, y1) = (y1, y0);
swapped = true;
}
int dx = x1 - x0;
int dy = Math.Abs(y1 - y0);
int error = dx / 2;
int ystep = (y0 < y1) ? 1 : -1;
int y = y0;
for (int x = x0; x <= x1; x++)
{
if (steep)
points.Add(new Point(y, x));
else
points.Add(new Point(x, y));
error -= dy;
if (error < 0)
{
y += ystep;
error += dx;
}
}
if (swapped)
points.Reverse();
return points;
}
double CalculateDistance(Point p1, Point p2)
{
int dx = p2.X - p1.X;
int dy = p2.Y - p1.Y;
return Math.Sqrt(dx * dx + dy * dy);
} |
Partager