Exemplo n.º 1
0
        void Compute()
        {
            propositions = new Dictionary <string, Proposition>();

            Dictionary <int, List <List <int> > > operations = OperationsGenerator(nbrButtons, nbrButtons, valUnitaireMax);

            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                                       new Action(() =>
            {
                lv.Items.Clear();
                foreach (List <List <int> > item in operations.Values)
                {
                    foreach (List <int> entiers in item)
                    {
                        lv.Items.Add(Proposition.SetSignature(entiers));
                    }
                }
            }));

            List <MCvScalar> couleurs = new List <MCvScalar>()
            {
                new MCvScalar(couleur.B, couleur.G, couleur.R, couleur.A)
            };

            Compute(operations, diametre, epaisseur,
                    new MCvScalar(couleurBackground.B, couleurBackground.G, couleurBackground.R, couleurBackground.A),
                    couleurs);
        }
Exemplo n.º 2
0
 internal void del(Proposition proposition)
 {
     LV.Items.Remove(proposition.pUC);
     lv.Items.Remove(proposition.signature);
 }
Exemplo n.º 3
0
        void Compute(Dictionary <int, List <List <int> > > operations,
                     int diametre, int epaisseur,
                     MCvScalar couleurFond, List <MCvScalar> couleurs)
        {
            int couleurindex = -1;

            //draw operations
            foreach (int x in operations.Keys)
            {
                couleurindex++;
                if (couleurindex == couleurs.Count)
                {
                    couleurindex = 0;
                }
                MCvScalar couleur = couleurs[couleurindex];

                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                                           new Action(() =>
                {
                    TextBlock tb = new TextBlock()
                    {
                        Text       = x.ToString(),
                        Foreground = new SolidColorBrush(Color.FromArgb((byte)couleur.V3,
                                                                        (byte)couleur.V2,
                                                                        (byte)couleur.V1,
                                                                        (byte)couleur.V0))
                    };
                    Viewbox vb = new Viewbox()
                    {
                        Child = tb, Height = diametre, Width = diametre
                    };
                    LV.Items.Add(vb);
                }));

                foreach (List <int> entiers in operations[x])
                {
                    string        titre   = Proposition.SetSignature(entiers);
                    List <Bouton> boutons = new List <Bouton>();

                    Emgu.CV.Image <Rgba, byte> image = DrawRings(diametre,
                                                                 epaisseur,
                                                                 couleur,
                                                                 couleurFond,
                                                                 entiers,
                                                                 angleDegres_OrigineParRapportAX,
                                                                 ref boutons
                                                                 );

                    Image <Gray, byte> gray    = new Image <Gray, byte>(image.Width, image.Height);
                    Image <Gray, byte> binaire = new Image <Gray, byte>(image.Width, image.Height);

                    //Alpha to WHITE
                    SETAlphaPixelToColorPixel(image, new MCvScalar(255, 255, 255, 255));

                    CvInvoke.CvtColor(image.Mat, gray, ColorConversion.Bgra2Gray);     //color to grayscale
                    CvInvoke.Threshold(gray, binaire, 100, 255, ThresholdType.Binary); //Binary
                    CvInvoke.BitwiseNot(binaire, binaire);                             //Logical NOT

                    Emgu.CV.Util.VectorOfVectorOfPoint contours = new Emgu.CV.Util.VectorOfVectorOfPoint();
                    Mat hierarchy = new Mat();

                    CvInvoke.FindContours(binaire, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxSimple);
                    //CvInvoke.DrawContours(image.Mat, contours, -1, new MCvScalar(0, 255, 0));

                    //WHITE to Alpha
                    SETColorPixelToAlphaPixel(image, new MCvScalar(255, 255, 255, 255));

                    for (int b = 0; b < boutons.Count; b++)
                    {
                        Bouton bouton = boutons[b];

                        CvInvoke.Circle(image.Mat, bouton.barycentre, 2, new MCvScalar(0, 255, 0, 255), -1);

                        //Quel contour est concerné ?
                        int contour_index = 0;
                        for (contour_index = 0; contour_index < contours.Size; contour_index++)
                        {
                            double result = CvInvoke.PointPolygonTest(contours[contour_index], bouton.barycentre, false);
                            if (result > 0) // >0 : inside
                            {
                                break;
                            }
                        }

                        Mat btn = new Mat(image.Size, DepthType.Cv8U, 1);
                        //colorie tout (-1) l'intérieur en vert (0,255,0,255) du contour n°1
                        CvInvoke.DrawContours(btn, contours, contour_index, new MCvScalar(100), -1);

                        //optimisation sur 2 paramètres :
                        //- p position sur l'axe centre du cercle, barycentre (position comprise entre rayon max et rayon min)
                        //- t taille de l'icone
                        //La meilleure solution p aura t maximum

                        // first values
                        // p0 = barycentre (parfait ou trop prêt du centre)
                        // t0 = 1
                        // test : est ce qu'aucun pixels du carré n'est vide (on a une image vide avec "seulement" le bouton de dessiné)
                        // ==> on augmente t
                        // quand tmax atteint pour p donné, on change de p, avec p compris entre p0 et p sur axe avec D = rayon_ext - t

                        // puis dichtomie pour p en maximisant t à chaque essai.



                        int cote_px = 10;

                        List <int>               icon_cote_px_test = new List <int>();
                        System.Drawing.Size      icon_size;
                        System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
                        System.Drawing.Point     icon_center;
                        ////int cote_px_retenu = 100;
                        //while (!icon_cote_px_test.Contains(cote_px))
                        //{
                        //    icon_cote_px_test.Add(cote_px);

                        //    icon_size = new System.Drawing.Size(cote_px, cote_px);
                        //    icon_center = new System.Drawing.Point(btn.barycentre.X - cote_px / 2, btn.barycentre.Y - cote_px / 2);
                        //    rect = new System.Drawing.Rectangle(icon_center, icon_size);

                        //    int area_theo = cote_px * cote_px;
                        //    Mat ROI = new Mat(image.Mat, rect);
                        //    int area_real = CvInvoke.CountNonZero(ROI);
                        //    int pixelvide = area_theo - area_real;
                        //    Console.WriteLine(cote_px + " ==> " + pixelvide);
                        //    if (pixelvide > 0)
                        //        cote_px--;
                        //    if (pixelvide <= 0)
                        //        cote_px++;
                        //}

                        //cote_px = icon_cote_px_test[icon_cote_px_test.Count - 2];
                        //icon_size = new System.Drawing.Size(cote_px, cote_px);
                        //icon_center = new System.Drawing.Point(btn.barycentre.X - cote_px / 2, btn.barycentre.Y - cote_px / 2);
                        //rect = new System.Drawing.Rectangle(icon_center, icon_size);

                        //CvInvoke.Rectangle(image.Mat,
                        //    rect,
                        //    new MCvScalar(0, 0, 0, 255));
                    }



                    //for (int i = 0; i < contours.Size; i++)
                    //{
                    //    Mat btn = new Mat(image.Size, DepthType.Cv8U, 1);
                    //    //colorie tout (-1) l'intérieur en vert (0,255,0,255) du contour n°1
                    //    CvInvoke.DrawContours(btn, contours, i, new MCvScalar(100), -1);



                    //    //CHANGER : au lieu du centre de masse, prendre l'axe "parfait" = axe entre 2 séparations
                    //    //pourquoi pas commencer par le point sur cet axe le plus prêt du barycentre
                    //    //puis se balader sur l'axe



                    //    //barycentre (centre de masse)
                    //    MCvMoments mom = CvInvoke.Moments(contours[i]);
                    //    System.Drawing.Point center_of_mass = new System.Drawing.Point((int)((float)mom.M10 / mom.M00), (int)((float)mom.M01 / mom.M00));
                    //    CvInvoke.Circle(image.Mat, center_of_mass, 2, new MCvScalar(0, 255, 0, 255), -1);

                    //    int cote_px = 10;

                    //    List<int> icon_cote_px_test = new List<int>();
                    //    System.Drawing.Size icon_size;
                    //    System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
                    //    System.Drawing.Point icon_center;
                    //    //int cote_px_retenu = 100;
                    //    while (!icon_cote_px_test.Contains(cote_px))
                    //    {
                    //        icon_cote_px_test.Add(cote_px);

                    //        icon_size = new System.Drawing.Size(cote_px, cote_px);
                    //        icon_center = new System.Drawing.Point(center_of_mass.X - cote_px / 2, center_of_mass.Y - cote_px / 2);
                    //        rect = new System.Drawing.Rectangle(icon_center, icon_size);

                    //        int area_theo = cote_px * cote_px;
                    //        Mat ROI = new Mat(btn, rect);
                    //        int area_real = CvInvoke.CountNonZero(ROI);
                    //        int pixelvide = area_theo - area_real;
                    //        Console.WriteLine(cote_px + " ==> " + pixelvide);
                    //        if (pixelvide > 0)
                    //            cote_px--;
                    //        if (pixelvide <= 0)
                    //            cote_px++;
                    //    }

                    //    cote_px = icon_cote_px_test[icon_cote_px_test.Count - 2];
                    //    icon_size = new System.Drawing.Size(cote_px, cote_px);
                    //    icon_center = new System.Drawing.Point(center_of_mass.X - cote_px / 2, center_of_mass.Y - cote_px / 2);
                    //    rect = new System.Drawing.Rectangle(icon_center, icon_size);

                    //    CvInvoke.Rectangle(image.Mat,
                    //        rect,
                    //        new MCvScalar(0, 0, 0, 255));



                    //    //int nbr = 100000;
                    //    //while (nbr > 50)
                    //    //{
                    //    //    CvInvoke.Erode(btn_prec, btn, element, new System.Drawing.Point(-1, -1), 1, BorderType.Reflect, default(MCvScalar));
                    //    //    nbr = CvInvoke.CountNonZero(btn);
                    //    //    if (nbr > 50)
                    //    //        btn_prec = btn;
                    //    //    //Console.WriteLine(nbr);
                    //    //    //Mat btn_cpy = new Mat();
                    //    //    //btn.CopyTo(btn_cpy);
                    //    //    //Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                    //    //    //    new Action(() =>
                    //    //    //    {
                    //    //    //        Proposition p = new Proposition(entiers, btn_cpy, titre, this);
                    //    //    //        LV.Items.Add(p.pUC);
                    //    //    //    }));
                    //    //}
                    //    //btn = btn_prec;

                    //    //Emgu.CV.Util.VectorOfVectorOfPoint contourBouton = new Emgu.CV.Util.VectorOfVectorOfPoint();
                    //    //Mat hierarchyBouton = new Mat();

                    //    //CvInvoke.FindContours(btn, contourBouton, hierarchyBouton, RetrType.External, ChainApproxMethod.ChainApproxSimple);
                    //    //System.Drawing.Rectangle rect = CvInvoke.BoundingRectangle(contourBouton[0]);


                    //    //System.Drawing.Point centre = new System.Drawing.Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
                    //    //CvInvoke.Circle(image.Mat, geometric_center, 2, new MCvScalar(0, 0, 255, 255), -1);


                    //}



                    #region BoundingBox
                    //Dictionary<int, double> dict = new Dictionary<int, double>();
                    //for (int i = 0; i < contours.Size; i++)
                    //{
                    //    double aera = CvInvoke.ContourArea(contours[i]);
                    //    System.Drawing.Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);

                    //    dict.Add(i, aera);
                    //}

                    //foreach (var it in dict)//item)
                    //{
                    //    int key = int.Parse(it.Key.ToString());
                    //    System.Drawing.Rectangle rect = CvInvoke.BoundingRectangle(contours[key]);
                    //    CvInvoke.Rectangle(image.Mat, rect, new MCvScalar(0, 0, 255), 1);
                    //}
                    #endregion

                    //ajout de l'image dans l'interface
                    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                                               new Action(() =>
                    {
                        //Proposition_UC pUC = AddImageToIHM();
                        Proposition p = new Proposition(entiers, image.Mat, titre, this, boutons);
                        propositions.Add(titre, p);
                        LV.Items.Add(p.pUC);
                    }));
                }
            }
        }
 public void LINK(Proposition prop)
 {
     p = prop;
 }