private void ellipseDetectionToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // load image imMat = FxMatrixF.Load(ofd.FileName); ieEllipseImage = new ImageElement(imMat, new ColorMap(ColorMapDefaults.Jet)); ieEllipseImage.lockMoving = true; canvas_ellipse.AddElement(ieEllipseImage); // add plot element gpeEllipseImage = new GeometryPlotElement(); canvas_ellipse.AddElement(gpeEllipseImage); var contours = new FxContour(imMat<0.2f); WriteLine("Num Contours : " + contours.NumChains); int i = 0; float pe_pos_y = ieEllipseImage._Position.y; foreach (var cont in contours.ChainList) { // draw the rectanges in main image FxVector2f start = cont.RectStart ; FxVector2f end = start + cont.RectSize; FxMaths.Geometry.Rectangle r = new FxMaths.Geometry.Rectangle(start, end); gpeEllipseImage.AddGeometry(r, false); // draw the centroids FxVector2f cen = cont.GetCentroid(); var l = new FxMaths.Geometry.Line(cen - new FxVector2f(0, 2), cen + new FxVector2f(0, 2)); l.LineWidth = 0.5f; gpeEllipseImage.AddGeometry(l, false); l = new FxMaths.Geometry.Line(cen - new FxVector2f(2, 0), cen + new FxVector2f(2, 0)); l.LineWidth = 0.5f; gpeEllipseImage.AddGeometry(l, false); // add the numbering of the contours var t = new TextElement(i.ToString()); t._Position = cen; t.FontColor = SharpDX.Color.Green; t._TextFormat.fontSize = 16.0f; canvas_ellipse.AddElement(t); // show the chain vector in plot { FxVectorF vec_i = new FxVectorF(cont.Count); FxVectorF vec_r = new FxVectorF(cont.Count); vec_i[0] = cont[0].i; vec_r[0] = cont[0].r; for (int j = 1; j < cont.Count; j++) { vec_i[j] = vec_i[j - 1] + cont[j].i; vec_r[j] = vec_r[j - 1] + cont[j].r; } // show the plot of this vector var pe = new PloterElement(vec_i, PlotType.Lines, System.Drawing.Color.Blue); pe._Position.x = ieEllipseImage._Position.x + ieEllipseImage.Size.x; pe._Position.y = pe_pos_y; pe.AddPlot(vec_r, PlotType.Lines, System.Drawing.Color.Red); pe.CenterYOrigin(); pe.FitPlots(); canvas_ellipse.AddElement(pe); // update the y of pe for the next one pe_pos_y += pe.Size.y; // debug the ellipse for (int j = 0; j < cont.Count; j++) { imMat[(int)(vec_r[j] + cont.StartPoint.x), (int)(vec_i[j] + cont.StartPoint.y)] = 0.8f/contours.NumChains + 0.1f; } ieEllipseImage.UpdateInternalImage(imMat, new ColorMap(ColorMapDefaults.Jet)); } i++; } canvas_ellipse.ReDraw(); } }
private void toolStripButton_ellipse_extract_Click(object sender, EventArgs e) { // extract the rectangle that contain if ((imMat as object != null) && (rect != null)) { int numLabels; // get the sub matrix var subMat = imMat.GetSubMatrix(rect.StartPoint, rect.EndPoint) as FxMatrixF; // make binary the image based on the start and end point of rectangle float t = subMat[0, 0]; if (t > subMat[subMat.Width - 1, subMat.Height - 1]) t = subMat[subMat.Width - 1, subMat.Height - 1]; var binMat = subMat < t - 0.02f; // find the labels of the image var labels = binMat.Labeling(out numLabels); var imSub = new ImageElement(binMat.ToFxMatrixF(), new ColorMap(ColorMapDefaults.Jet)); imSub._Position.x = ieEllipseImage.Size.x + ieEllipseImage.Position.x; imSub.lockMoving = true; canvas_ellipse.AddElement(imSub, false, false); var imSub2 = new ImageElement(labels, new ColorMap(ColorMapDefaults.Jet)); imSub2._Position.x = ieEllipseImage.Size.x + ieEllipseImage.Position.x; imSub2._Position.y = imSub.Size.y + imSub.Position.y; imSub2.lockMoving = true; canvas_ellipse.AddElement(imSub2, false, false); WriteLine("Num Labels : " + numLabels.ToString()); var contours = new FxContour(binMat, 10, 300); WriteLine("Num Contours : " + contours.NumChains); results = new FxMatrixF(3, contours.NumChains); int i=0; foreach (var x in contours.ChainList) { float delta = 1; // draw the rectanges in the sub image FxVector2f start = x.RectStart + imSub2._Position + new FxVector2f(1, 1); FxVector2f end = start + x.RectSize; FxMaths.Geometry.Rectangle r = new FxMaths.Geometry.Rectangle(start, end); gpeEllipseImage.AddGeometry(r, false); // draw the rectanges in main image start = x.RectStart + rect.StartPoint + new FxVector2f(-delta, -delta); end = start + x.RectSize + new FxVector2f(2 * delta, 2 * delta); r = new FxMaths.Geometry.Rectangle(start, end); gpeEllipseImage.AddGeometry(r, false); // draw the centroids FxVector2f cen = x.GetCentroid() + rect.StartPoint; var l = new FxMaths.Geometry.Line(cen - new FxVector2f(0, 2), cen + new FxVector2f(0, 2)); l.LineWidth = 0.5f; gpeEllipseImage.AddGeometry(l, false); l = new FxMaths.Geometry.Line(cen - new FxVector2f(2, 0), cen + new FxVector2f(2, 0)); l.LineWidth = 0.5f; gpeEllipseImage.AddGeometry(l, false); // calculate the depth float[] depth = new float[4]; FxVector2f pos = x.RectStart + rect.StartPoint; depth[0] = imMat[pos.x - delta, pos.y - delta]; depth[1] = imMat[pos.x + x.RectSize.x + delta, pos.y - delta]; depth[2] = imMat[pos.x - delta, pos.y + x.RectSize.y + delta]; depth[3] = imMat[pos.x + x.RectSize.x + delta, pos.y + x.RectSize.y + delta]; results[2, i] = (depth[0] + depth[1] + depth[2] + depth[3]) / 4.0f; // save centroid results[0, i] = cen.x; results[1, i] = cen.y; // print the centroid WriteLine("[" + i.ToString() + "] Depth:" + results[2, i].ToString() + " Pixels:" + x.Count + " Centroid: " + cen.ToString()); i++; // show the vector of one blob if (i == 1) { FxVectorF vec_i = new FxVectorF(x.Count); FxVectorF vec_r = new FxVectorF(x.Count); vec_i[0] = x[0].i; vec_r[0] = x[0].r; for (int j = 1; i < x.Count; j++) { vec_i[j] = vec_i[j - 1] + x[j].i; vec_r[j] = vec_r[j - 1] + x[j].r; } } } canvas_ellipse.ReDraw(); results.SaveCsv("ellipseResults.csv"); } }