示例#1
0
        //**********************************************************************************************************************************************************************************************

        /// <summary>
        /// Find corners, extract the edges and classify the piece
        /// </summary>
        private void process()
        {
            Bitmap bwImg = PieceImgBw.Bmp;
            Bitmap colorImg = PieceImgColor.Bmp;

            corners.Clear();
            PluginGroupFindPieceCorners pluginFindPieceCorners = PluginFactory.GetEnabledPluginsOfGroupType<PluginGroupFindPieceCorners>().FirstOrDefault();
            corners.Push(pluginFindPieceCorners?.FindCorners(PieceID, bwImg, colorImg).ToArray());

            bwImg.Dispose();
            colorImg.Dispose();

            extract_edges();
            classify();
        }
示例#2
0
        private void button2_Click(object sender, EventArgs e)
        {
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            try
            {
                if (imgInput == null)
                {
                    MessageBox.Show("Please select an image");
                    return;
                }

                var img = imgInput;

                imgOut = new Image <Gray, byte>(imgInput.Width, imgInput.Height, new Gray(0));

                var gray  = imgInput.Convert <Gray, byte>().ThresholdBinaryInv(new Gray(140), new Gray(255));
                var gray2 = imgInput.Convert <Gray, byte>().ThresholdBinaryInv(new Gray(140), new Gray(255));
                //var gray = imgCanny.Convert<Gray, byte>().ThresholdBinaryInv(new Gray(125), new Gray(255));

                //Contours
                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat h = new Mat();

                CvInvoke.FindContours(gray, contours, h, Emgu.CV.CvEnum.RetrType.External,
                                      Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);

                VectorOfPoint approx = new VectorOfPoint();

                Dictionary <int, double> shapes  = new Dictionary <int, double>();
                Dictionary <int, double> shapes2 = new Dictionary <int, double>();
                Dictionary <int, double> shapes3 = new Dictionary <int, double>();
                Dictionary <int, double> shapes4 = new Dictionary <int, double>();

                for (int i = 0; i < contours.Size; i++)
                {
                    approx.Clear();
                    double perimeter = CvInvoke.ArcLength(contours[i], true);
                    KomaPerimeter = Math.Round(perimeter, 2); // membatasi angka

                    CvInvoke.ApproxPolyDP(contours[i], approx, 0.04 * perimeter, true);

                    double area = CvInvoke.ContourArea(contours[i]);

                    if (approx.Size > 0 && area < 220000 && area > 60000 && perimeter < 2100)
                    {
                        shapes.Add(i, area);
                        shapes2.Add(i, KomaPerimeter);
                        shapes3.Add(i, KomaPerimeter);
                    }
                }

                if (shapes.Count > 0)
                {
                    var sortedShapes = (from item in shapes
                                        orderby item.Value ascending
                                        select item).ToList();

                    for (int i = 0; i < sortedShapes.Count; i++)
                    {
                        CvInvoke.DrawContours(img, contours, sortedShapes[i].Key, new MCvScalar(255, 0, 0), 2);

                        var moments = CvInvoke.Moments(contours[sortedShapes[i].Key]);

                        int x = (int)(moments.M10 / moments.M00);
                        int y = (int)(moments.M01 / moments.M00);

                        textBox6.Text = sortedShapes[i].Value.ToString();
                        fitur_area    = sortedShapes[i].Value.ToString();
                    }

                    var sortedShapes2 = (from item in shapes2
                                         orderby item.Value ascending
                                         select item).ToList();

                    for (int i = 0; i < sortedShapes2.Count; i++)
                    {
                        CvInvoke.DrawContours(img, contours, sortedShapes2[i].Key, new MCvScalar(255, 255, 0), 1);
                        var moments = CvInvoke.Moments(contours[sortedShapes2[i].Key]);
                        int x       = (int)(moments.M10 / moments.M00);
                        int y       = (int)(moments.M01 / moments.M00);

                        textBox5.Text   = sortedShapes2[i].Value.ToString();
                        fitur_perimeter = sortedShapes2[i].Value.ToString();
                    }

                    var sortedShapes3 = (from item in shapes3
                                         orderby item.Value descending
                                         select item).ToList();

                    for (int i = 0; i < sortedShapes3.Count; i++)
                    {
                        var moments = CvInvoke.Moments(contours[sortedShapes3[i].Key]);
                        int x       = (int)(moments.M10 / moments.M00);
                        int y       = (int)(moments.M01 / moments.M00);

                        img.Draw(new CircleF(new Point(x, y), 3), new Bgr(Color.Yellow), 3);
                        //Draw titik tengah
                        textBox4.Text      = x.ToString() + ", " + y.ToString();
                        fitur_titik_tengah = x.ToString() + ", " + y.ToString();
                    }
                }

                pictureBox2.Image = img.Bitmap;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#3
0
        private void findHolesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (pictureBox1.Image == null)
                {
                    return;
                }

                var img = new Bitmap(pictureBox1.Image).ToImage <Bgr, byte>();

                var gray = img.Convert <Gray, byte>()
                           .ThresholdBinaryInv(new Gray(240), new Gray(255));

                // contours
                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                Mat h = new Mat();

                CvInvoke.FindContours(gray, contours, h, Emgu.CV.CvEnum.RetrType.External
                                      , Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);


                VectorOfPoint approx = new VectorOfPoint();

                Dictionary <int, double> shapes = new Dictionary <int, double>();

                for (int i = 0; i < contours.Size; i++)
                {
                    approx.Clear();
                    double perimeter = CvInvoke.ArcLength(contours[i], true);
                    CvInvoke.ApproxPolyDP(contours[i], approx, 0.04 * perimeter, true);
                    double area = CvInvoke.ContourArea(contours[i]);

                    if (approx.Size > 6)
                    {
                        shapes.Add(i, area);
                    }
                }


                if (shapes.Count > 0)
                {
                    var sortedShapes = (from item in shapes
                                        orderby item.Value ascending
                                        select item).ToList();

                    for (int i = 0; i < sortedShapes.Count; i++)
                    {
                        CvInvoke.DrawContours(img, contours, sortedShapes[i].Key, new MCvScalar(0, 0, 255), 2);
                        var moments = CvInvoke.Moments(contours[sortedShapes[i].Key]);
                        int x       = (int)(moments.M10 / moments.M00);
                        int y       = (int)(moments.M01 / moments.M00);

                        /*Initiale mask for flood filling - tut useless
                         * int height = img.Height;
                         * int width = img.Width;
                         * Mat mask = new Mat(width, height+4, DepthType.Cv8U, 1);
                         * CvInvoke.FloodFill(img, mask, new Point(x, y), new MCvScalar(255, 255, 255), out rect,
                         * new MCvScalar(155,155,155), new MCvScalar(255,255,255));
                         */

                        CvInvoke.PutText(img, (i + 1).ToString(), new Point(x, y), Emgu.CV.CvEnum.FontFace.HersheyTriplex, 1.0,
                                         new MCvScalar(0, 0, 255), 2);
                        CvInvoke.PutText(img, sortedShapes[i].Value.ToString(), new Point(x, y - 30), Emgu.CV.CvEnum.FontFace.HersheyTriplex, 1.0,
                                         new MCvScalar(0, 0, 255), 2);

                        Console.WriteLine("X: " + x + ", Y: " + y);

                        // vzames x in y -> sepravi kjer je contour postavljen, gres cez zanke in pristevas dokler je piksel črn... recimo bo delal
                        int pixel = 0;

                        // za dimenzije
                        int x_desno_counter   = 0;
                        int x_levo_counter    = 0;
                        int x_sum             = 0;
                        int y_spodnji_counter = 0;
                        int y_zgornji_counter = 0;
                        int y_sum             = 0;
                        int tmp_x             = x;
                        int tmp_y             = y;
                        int area = 0;

                        /*ZA DOBIT X-E*/
                        while (true) // neskoncna pa jo pol breakam
                        {
                            if (img.Data[tmp_y, tmp_x, 0] < 255)
                            {
                                x_desno_counter++;
                            }
                            else
                            {
                                break;
                            }

                            //se pomaknemo desno -> povečamo x
                            tmp_x++;
                        }

                        tmp_x = x;   //reseteram

                        while (true) // neskoncna pa jo pol breakam
                        {
                            if (img.Data[tmp_y, tmp_x, 0] < 255)
                            {
                                x_levo_counter++;
                            }
                            else
                            {
                                break;
                            }

                            //se pomaknemo levo -> zmanjsamo x
                            tmp_x--;
                        }

                        tmp_x = x;

                        /*ZA DOBIT Y-E*/
                        while (true) // neskoncna pa jo pol breakam
                        {
                            if (img.Data[tmp_y, tmp_x, 0] < 255)
                            {
                                y_spodnji_counter++;
                            }
                            else
                            {
                                break;
                            }

                            //se pomaknemo dol -> zmanjsamo y
                            tmp_y--;
                        }

                        tmp_y = y;   //reseteram

                        while (true) // neskoncna pa jo pol breakam
                        {
                            if (img.Data[tmp_y, tmp_x, 0] < 255)
                            {
                                y_zgornji_counter++;
                            }
                            else
                            {
                                break;
                            }

                            //se pomaknemo gor -> povečamo y
                            tmp_y++;
                        }

                        //dobimo vsoto levih in desnih x pikslov
                        x_sum = x_desno_counter + x_levo_counter;
                        //dobimo vsoto zgornjih in spodnjih y pikslov
                        y_sum = y_zgornji_counter + y_spodnji_counter;

                        //dobimo začetke
                        int x_start = x - x_levo_counter;
                        int y_start = y + y_spodnji_counter;

                        area = x_sum + y_sum;
                        Console.WriteLine("AREA SUM BEFORE: " + area);

                        //dvojna zanka -> gremo čez kvadrat in štejemo samo črne piksle
                        for (int j = y_start; j < y_start + y_sum; j++)
                        {
                            for (int k = x_start; k < x_start + x_sum; k++)
                            {
                                if (img.Data[j, k, 0] < 255) // je črn piksel
                                {
                                    area++;                  // povečamo ploščino
                                }
                            }
                        }

                        Console.WriteLine("X DESNI COUNTER: " + x_desno_counter);
                        Console.WriteLine("X LEVI COUNTER: " + x_levo_counter);
                        Console.WriteLine("Y ZGORNJI COUNTER: " + y_zgornji_counter);
                        Console.WriteLine("Y SPODNJI  COUNTER: " + y_spodnji_counter);
                        Console.WriteLine("X SUM: " + x_sum);
                        Console.WriteLine("Y SUM: " + y_sum);
                        Console.WriteLine("X START: " + x_start);
                        Console.WriteLine("Y START: " + y_start);
                        Console.WriteLine("AREA: " + area);
                        Console.WriteLine("---------------------------------------------------------------");

                        //se zapisem v podatkovno bazo
                        Luknja luknja = new Luknja(x_sum, y_sum, area);
                        //connect to database
                        using (IDbConnection connection = new SqlConnection(Utility.ConnVal("ec38")))
                        {
                            //insert into table DelovniNalogi
                            connection.Query("INSERT INTO Holes(hole_height, hole_width, hole_area) VALUES(@height, @width, @area);",
                                             new { height = luknja.Height, width = luknja.Width, area = luknja.Area });
                        }
                    }

                    /*BOLJ PRIBLIŽNE VREDNOSTI SO...*/

                    /* BIGGEST CONTOUR - USELESS
                     * double largest = sortedShapes[sortedShapes.Count - 1].Value;
                     * Console.WriteLine("largest contour: " + largest);
                     */
                }

                pictureBox1.Image = img.ToBitmap();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Try different threshold\n" + ex.Message);
            }
        }