using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Diagnostics;
using Emgu.CV.Util;
namespace RRI
{
public partial class Form1 : Form {
public Boolean un, deux; public int comptage1, comptage2,objet;
public double
optimal1, optimal2;
public double surfaceImage,surfaceObjet1;
public double[] surfaceObjet=new double[100];
public Form1()
{
InitializeComponent(); un = false; deux = false;
comptage1 = comptage2 =objet= 0;
surfaceObjet1 = 0.0;
label17.Text = "";
label18.Text = "";
}
public void PerformShapeDetection()
{
if (FileName1.Text != String.Empty)
{
StringBuilder msgBuilder = new StringBuilder("Performance:
");
//Load the image from file and resize it for display
Image<Bgr, Byte> img =
new Image<Bgr, byte>(FileName1.Text)
.Resize(400, 400, Emgu.CV.CvEnum.Inter.Linear, true);
//Convert the image to grayscale and filter out the noise
UMat uimage = new UMat();
CvInvoke.CvtColor(img, uimage, ColorConversion.Bgr2Gray);
//use image pyr to remove noise UMat pyrDown = new UMat();
CvInvoke.PyrDown(uimage, pyrDown); CvInvoke.PyrUp(pyrDown,
uimage);
Page | 48
//Image<Gray, Byte> gray = img.Convert<Gray,
Byte>().PyrDown().PyrUp();
#region circle detection
Stopwatch watch = Stopwatch.StartNew();
double cannyThreshold = 180.0;
double circleAccumulatorThreshold = 120;
CircleF[] circles = CvInvoke.HoughCircles(uimage,
HoughType.Gradient,
2.0, 20.0, cannyThreshold, circleAccumulatorThreshold, 5);
watch.Stop();
msgBuilder.Append(String.Format("Hough circles - {0} ms; ",
watch.ElapsedMilliseconds));
#endregion
#region Canny and edge detection watch.Reset();
watch.Start();
double cannyThresholdLinking = 120.0;
UMat cannyEdges = new UMat();
CvInvoke.Canny(uimage, cannyEdges, cannyThreshold,
cannyThresholdLinking);
LineSegment2D[] lines = CvInvoke.HoughLinesP(
cannyEdges,
1, //Distance resolution in pixel-related units
Math.PI / 45.0, //Angle resolution measured in radians.
20, //threshold
30, //min Line width
10); //gap between lines
watch.Stop();
msgBuilder.Append(String.Format("Canny & Hough lines -
{0} ms; ", watch.ElapsedMilliseconds));
#endregion
#region Find triangles and rectangles
watch.Reset(); watch.Start();
List<Triangle2DF> triangleList = new
List<Triangle2DF>(); List<RotatedRect> boxList = new
List<RotatedRect>(); //a box is a rotated rectangle
using (VectorOfVectorOfPoint contours = new
VectorOfVectorOfPoint())
{
CvInvoke.FindContours(cannyEdges, contours, null,
RetrType.List,
ChainApproxMethod.ChainApproxSimple);
int count = contours.Size;
for (int i = 0; i < count; i++)
{
using (VectorOfPoint contour = contours[i])
using (VectorOfPoint approxContour = new VectorOfPoint())
{
CvInvoke.ApproxPolyDP(contour, approxContour,
CvInvoke.ArcLength(contour, true) * 0.05, true);
if (CvInvoke.ContourArea(approxContour, false) > 250)
//only consider contours with area greater than 250
{
if (approxContour.Size == 3) //The contour has 3
vertices, it is a triangle
{
Point[] pts = approxContour.ToArray();
}
Page | 49
triangleList.Add(new Triangle2DF(
pts[0],
pts[1],
pts[2]
));
}
else if (approxContour.Size == 4) //The contour has 4
vertices.
{
#region determine if all the angles in the contour
are within [80, 100] degree
bool isRectangle = true;
Point[] pts = approxContour.ToArray(); LineSegment2D[] edges
=
PointCollection.PolyLine(pts, true);
for (int j = 0; j < edges.Length; j++)
{
double angle = Math.Abs(
edges[(j + 1) %
edges.Length].GetExteriorAngleDegree(edges[j]));
if (angle < 80 || angle > 100)
{
isRectangle = false;
break;
}
}
#endregion
if (isRectangle)
boxList.Add(CvInvoke.MinAreaRect(approxContour));
}
}
}
}
}
watch.Stop();
msgBuilder.Append(String.Format("Triangles & Rectangles -
{0} ms; ", watch.ElapsedMilliseconds));
#endregion
this.Text = msgBuilder.ToString();
#region draw triangles and rectangles
Mat triangleRectangleImage = new Mat(img.Size, DepthType.Cv8U,
3);
triangleRectangleImage.SetTo(new MCvScalar(0));
foreach (Triangle2DF triangle in triangleList)
{
CvInvoke.Polylines(triangleRectangleImage,
Array.ConvertAll(triangle.GetVertices(), Point.Round), true,
new
Bgr(Color.DarkBlue).MCvScalar, 2);
}
int nombre; nombre = 0;
foreach (RotatedRect box in boxList)
{
CvInvoke.Polylines(triangleRectangleImage,
Array.ConvertAll(box.GetVertices(), Point.Round), true, new
Bgr(Color.DarkOrange).MCvScalar, 2);
nombre = nombre + 1;
Page | 50
MessageBox.Show(nombre.ToString());
// DImageBox2.Image = triangleRectangleImage;
#endregion
}
}
private void textBox1_TextChanged_1(object sender, EventArgs
e)
{
un = true; objet = 0;
if (FileName1.Text != String.Empty)
{
StringBuilder msgBuilder = new StringBuilder("Performance:
");
//Load the image from file and resize it for display
Image<Bgr, Byte> img =
new Image<Bgr, byte>(FileName1.Text)
.Resize(400, 400, Emgu.CV.CvEnum.Inter.Linear, true);
DImageBox1.Image = img;
DImageBox1.SizeMode = PictureBoxSizeMode.Zoom;
surfaceImage= img.Width * img.Height;
//Convert the image to grayscale and filter out the noise UMat
uimage = new UMat();
CvInvoke.CvtColor(img, uimage, ColorConversion.Bgr2Gray);
//use image pyr to remove noise UMat pyrDown = new UMat();
CvInvoke.PyrDown(uimage, pyrDown); CvInvoke.PyrUp(pyrDown,
uimage);
#region circle detection
Stopwatch watch = Stopwatch.StartNew();
double cannyThreshold = 180.0;
double circleAccumulatorThreshold = 120;
CircleF[] circles = CvInvoke.HoughCircles(uimage,
HoughType.Gradient,
2.0, 20.0, cannyThreshold, circleAccumulatorThreshold, 5);
watch.Stop();
msgBuilder.Append(String.Format("Hough circles - {0} ms; ",
watch.ElapsedMilliseconds));
#endregion
#region Canny and edge detection watch.Reset();
watch.Start();
double cannyThresholdLinking = 120.0;
UMat cannyEdges = new UMat();
CvInvoke.Canny(uimage, cannyEdges, cannyThreshold,
cannyThresholdLinking);
LineSegment2D[] lines = CvInvoke.HoughLinesP(
cannyEdges,
1, //Distance resolution in pixel-related units
Math.PI / 45.0, //Angle resolution measured in radians.
20, //threshold
30, //min Line width
10); //gap between lines
watch.Stop();
{
Page | 51
msgBuilder.Append(String.Format("Canny & Hough lines -
{0} ms; ", watch.ElapsedMilliseconds));
#endregion
#region Find triangles and rectangles
watch.Reset(); watch.Start();
List<Triangle2DF> triangleList = new
List<Triangle2DF>(); List<RotatedRect> boxList = new
List<RotatedRect>(); //a box is a rotated rectangle
using (VectorOfVectorOfPoint contours = new
VectorOfVectorOfPoint())
{
CvInvoke.FindContours(cannyEdges, contours, null,
RetrType.List,
ChainApproxMethod.ChainApproxSimple);
int count = contours.Size;
for (int i = 0; i < count; i++)
{
using (VectorOfPoint contour = contours[i])
using (VectorOfPoint approxContour = new VectorOfPoint())
{
CvInvoke.ApproxPolyDP(contour, approxContour,
CvInvoke.ArcLength(contour, true) * 0.05, true);
if (CvInvoke.ContourArea(approxContour, false) > 250)
//only consider contours with area greater than 250
{
if (approxContour.Size == 3) //The contour has 3
vertices, it is a triangle
{
Point[] pts = approxContour.ToArray();
triangleList.Add(new Triangle2DF(
pts[0],
pts[1],
pts[2]
));
}
else if (approxContour.Size == 4) //The contour has 4
vertices.
{
#region determine if all the angles in the contour
are within [80, 100] degree
bool isRectangle = true;
Point[] pts = approxContour.ToArray(); LineSegment2D[] edges
=
PointCollection.PolyLine(pts, true);
for (int j = 0; j < edges.Length; j++)
{
double angle = Math.Abs(
edges[(j + 1) %
edges.Length].GetExteriorAngleDegree(edges[j]));
if (angle < 80 || angle > 100)
{
isRectangle = false;
break;
}
}
#endregion
if (CvInvoke.MinAreaRect(approxContour).Size.Width
> 50)
Page | 52
if (isRectangle)
boxList.Add(CvInvoke.MinAreaRect(approxContour));
}}
}}
}}
watch.Stop();
msgBuilder.Append(String.Format("Triangles & Rectangles -
{0} ms; ", watch.ElapsedMilliseconds));
#endregion
this.Text = msgBuilder.ToString();
#region draw triangles and rectangles
Mat triangleRectangleImage = new Mat(img.Size, DepthType.Cv8U,
3);
triangleRectangleImage.SetTo(new MCvScalar(0));
foreach (Triangle2DF triangle in triangleList)
{
CvInvoke.Polylines(triangleRectangleImage,
Array.ConvertAll(triangle.GetVertices(), Point.Round), true,
new
Bgr(Color.DarkBlue).MCvScalar, 2);
}
foreach (RotatedRect box in boxList)
{
CvInvoke.Polylines(triangleRectangleImage,
Array.ConvertAll(box.GetVertices(), Point.Round), true, new
Bgr(Color.DarkOrange).MCvScalar, 2);
comptage1 = comptage1 + 1;
surfaceObjet[objet]= box.Size.Width * box.Size.Height;
objet++;
}
for (int i=0;i< objet;i++)
{
surfaceObjet1 = surfaceObjet1 + surfaceObjet[i];
}
Resultat(surfaceObjet1, surfaceImage, 1); for (int i = 0; i <
objet; i++)
{
surfaceObjet[i] = 0;
}
// ,comptage1,
label6.Text = (comptage1 / 2).ToString(); label20.Text =
surfaceObjet1.ToString();
label22.Text = surfaceImage.ToString();
// optimal1 = Resultat1(int.Parse(Nbre1.Text), comptage1 /
2);
surfaceObjet1 = 0;
#endregion
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
deux = true; objet = 0;
if (FileName2.Text != String.Empty)
{
StringBuilder msgBuilder = new StringBuilder("Performance:
");
//Load the image from file and resize it for display
Image<Bgr, Byte> img =
Page | 53
new Image<Bgr, byte>(FileName2.Text)
.Resize(400, 400, Emgu.CV.CvEnum.Inter.Linear, true);
DImageBox2.Image = img;
DImageBox2.SizeMode = PictureBoxSizeMode.Zoom;
surfaceImage = img.Width * img.Height;
//Convert the image to grayscale and filter out the noise UMat
uimage = new UMat();
CvInvoke.CvtColor(img, uimage, ColorConversion.Bgr2Gray);
//use image pyr to remove noise UMat pyrDown = new UMat();
CvInvoke.PyrDown(uimage, pyrDown); CvInvoke.PyrUp(pyrDown,
uimage);
#region circle detection
Stopwatch watch = Stopwatch.StartNew();
double cannyThreshold = 180.0;
double circleAccumulatorThreshold = 120;
CircleF[] circles = CvInvoke.HoughCircles(uimage,
HoughType.Gradient,
2.0, 20.0, cannyThreshold, circleAccumulatorThreshold, 5);
watch.Stop();
msgBuilder.Append(String.Format("Hough circles - {0} ms; ",
watch.ElapsedMilliseconds));
#endregion
#region Canny and edge detection watch.Reset();
watch.Start();
double cannyThresholdLinking = 120.0;
UMat cannyEdges = new UMat();
CvInvoke.Canny(uimage, cannyEdges, cannyThreshold,
cannyThresholdLinking);
LineSegment2D[] lines = CvInvoke.HoughLinesP(
cannyEdges,
1, //Distance resolution in pixel-related units
Math.PI / 45.0, //Angle resolution measured in radians.
20, //threshold
30, //min Line width
10); //gap between lines
watch.Stop();
msgBuilder.Append(String.Format("Canny & Hough lines - {0}
ms; ", watch.ElapsedMilliseconds));
#endregion
#region Find triangles and rectangles
watch.Reset(); watch.Start();
List<Triangle2DF> triangleList = new
List<Triangle2DF>(); List<RotatedRect> boxList = new
List<RotatedRect>(); //a box is a rotated rectangle
using (VectorOfVectorOfPoint contours = new
VectorOfVectorOfPoint())
{
CvInvoke.FindContours(cannyEdges, contours, null,
RetrType.List,
ChainApproxMethod.ChainApproxSimple);
int count = contours.Size;
for (int i = 0; i < count; i++)
{
using (VectorOfPoint contour = contours[i])
Page | 54
using (VectorOfPoint approxContour = new VectorOfPoint()) {
CvInvoke.ApproxPolyDP(contour, approxContour,
CvInvoke.ArcLength(contour, true) * 0.05, true);
if (CvInvoke.ContourArea(approxContour, false) > 250)
//only consider contours with area greater than 250
{
if (approxContour.Size == 3) //The contour has 3
vertices, it is a triangle
{
Point[] pts = approxContour.ToArray();
triangleList.Add(new Triangle2DF(
pts[0],
pts[1],
pts[2]
));
}
else if (approxContour.Size == 4) //The contour has 4
vertices.
{
#region determine if all the angles in the contour
are within [80, 100] degree
bool isRectangle = true;
Point[] pts = approxContour.ToArray(); LineSegment2D[] edges
=
PointCollection.PolyLine(pts, true);
for (int j = 0; j < edges.Length; j++)
{
double angle = Math.Abs(
edges[(j + 1) %
edges.Length].GetExteriorAngleDegree(edges[j]));
if (angle < 80 || angle > 100)
{
isRectangle = false;
break;
}
}
#endregion
if (CvInvoke.MinAreaRect(approxContour).Size.Width
> 50)
{
if (isRectangle)
boxList.Add(CvInvoke.MinAreaRect(approxContour));
}}
}}
}}
watch.Stop();
msgBuilder.Append(String.Format("Triangles & Rectangles -
{0} ms; ", watch.ElapsedMilliseconds));
#endregion
this.Text = msgBuilder.ToString();
#region draw triangles and rectangles
Mat triangleRectangleImage = new Mat(img.Size, DepthType.Cv8U,
3);
triangleRectangleImage.SetTo(new MCvScalar(0));
foreach (Triangle2DF triangle in triangleList)
{
}
Page | 55
CvInvoke.Polylines(triangleRectangleImage,
Array.ConvertAll(triangle.GetVertices(), Point.Round), true, new
Bgr(Color.DarkBlue).MCvScalar, 2);
}
foreach (RotatedRect box in boxList)
{
CvInvoke.Polylines(triangleRectangleImage,
Array.ConvertAll(box.GetVertices(), Point.Round), true, new
Bgr(Color.DarkOrange).MCvScalar, 2);
comptage2 = comptage2 + 1;
surfaceObjet[objet] = box.Size.Width * box.Size.Height;
objet++;
}
for (int i = 0; i < objet; i++)
{
surfaceObjet1 = surfaceObjet1 + surfaceObjet[i];
}
Resultat(surfaceObjet1, surfaceImage, 2); for (int i = 0; i <
objet; i++)
{
surfaceObjet[i] = 0;
}
label10.Text = (comptage2 / 2).ToString();
label24.Text = surfaceObjet1.ToString();
label26.Text = surfaceImage.ToString();
// optimal2 = Resultat1(int.Parse(Nbre2.Text), comptage2 /
2);
surfaceObjet1 = 0;
#endregion
}
}
private void SearchFile2_Click_1(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK || result == DialogResult.Yes)
{
FileName2.Text = openFileDialog1.FileName;
}
}
public void Resultat(double AireOccupe,double AireVoie,int
numeroImage)
{
double x, y;
if (numeroImage == 1)
{
x = (AireVoie - AireOccupe) / AireOccupe;
y = AireOccupe * x / AireVoie; optimal1 = x + y;
}
else {
x = (AireVoie - AireOccupe) / AireOccupe;
y = AireOccupe * x / AireVoie; optimal2 = x + y;
Page | 56
AfficherResultat();
}
public void AfficherResultat()
{
if (un == true && deux == true )
{
if ((optimal1 <= optimal2))
{
Image<Bgr, Byte> img =
new Image<Bgr, byte>(FileName1.Text)
.Resize(400, 400, Emgu.CV.CvEnum.Inter.Linear, true);
PimageBox.Image = img;
}
else {
}
}
label17.Text = "Image 1";
Image<Bgr, Byte> img =
new Image<Bgr, byte>(FileName2.Text)
.Resize(400, 400, Emgu.CV.CvEnum.Inter.Linear, true);
PimageBox.Image = img; label17.Text = "Image 2";
PimageBox.SizeMode = PictureBoxSizeMode.Zoom;
}
private void SearchFile1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK || result == DialogResult.Yes)
{
}
}}}
FileName1.Text = openFileDialog1.FileName;
Page | 57