Profil en fonction d'une ligne

sebbur Messages postés 39 Date d'inscription lundi 9 mai 2016 Statut Membre Dernière intervention 27 juillet 2016 - Modifié par sebbur le 27/07/2016 à 11:17
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 - 31 août 2016 à 22:09
Bonjour à tous,

Je souhaiterai afficher le profil d'intensité d'une image que je capture avec une caméra, en fonction d'une ligne tracée par l'utilisateur. Donc je capture une image, ensuite je peux tracer une ligne sur cette image, et après ça le profil s'afficherait en fonction de la ligne tracée.

Voici le code :


        private Image monImage = null;
        private void MainForm_Load(object sender, EventArgs e)
        {
            monImage = pictureBox.Image;
            bmpAxis = new Bitmap(pictureBox.Width, pictureBox.Height);
            rectImage = new Rectangle(0, 0, monImage.Width, monImage.Height);

            pictureBox.Invalidate();


            m_EchX = (Convert.ToDouble(pictureBox.ClientRectangle.Height) / Convert.ToDouble(pictureBox.Height));
            double l_DX = Convert.ToDouble(pictureBox.Width) * m_EchX;
            Int32 l_X = Convert.ToInt32(l_DX);

            double l_DY = Convert.ToDouble(pictureBox.Height) * m_EchX;
            Int32 l_Y = Convert.ToInt32(l_DY);

            pictureBox.Image = new Bitmap(pictureBox.Image, new Size(l_X, l_Y));

            //image pour l'affichage du diagrame
            Bitmap xx = new Bitmap(l_X, pictureBox2.ClientRectangle.Height);
            Graphics grph = Graphics.FromImage(xx);
            grph.Clear(Color.LightCoral);

            m_XminIMG = (pictureBox.Width - xx.Width) / 2;
            m_XmaxIMG = m_XminIMG + l_X;
            pictureBox2.Location = new Point(pictureBox.Location.X + m_XminIMG, pictureBox.Location.Y + pictureBox.Height + 6);
            pictureBox2.Size = new Size(xx.Width, xx.Height);
            pictureBox2.Image = xx;

            /*
             * PICTBcouleurR.Image = new Bitmap(PICTBcouleurR.ClientRectangle.Width, PICTBcouleurR.ClientRectangle.Height);
            Graphics l_Grph = Graphics.FromImage(PICTBcouleurR.Image);
            l_Grph.Clear(Color.FromArgb(125, 0, 0));

            PICTBcouleurG.Image = new Bitmap(PICTBcouleurG.ClientRectangle.Width, PICTBcouleurG.ClientRectangle.Height);
            l_Grph = Graphics.FromImage(PICTBcouleurG.Image);
            l_Grph.Clear(Color.FromArgb(0, 125, 0));

            PICTBcouleurB.Image = new Bitmap(PICTBcouleurB.ClientRectangle.Width, PICTBcouleurB.ClientRectangle.Height);
            l_Grph = Graphics.FromImage(PICTBcouleurB.Image);
            l_Grph.Clear(Color.FromArgb(0, 0, 125));
            */

        }

        #endregion INITIALISATION FERMETURE


        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            if (checkBox2.Checked)
            {
                // GRAPHE HORIZONTAL VERTICAL

                PictureBox ctrl = (PictureBox)sender;
                Image img = pictureBox.Image;
                Graphics g = e.Graphics;
                Pen p = new Pen(Color.Red, 2);

                g.Clear(Color.White);
                g.DrawImage(img, 0, 0, img.Width, img.Height);

                ctrl.Width = img.Width;
                ctrl.Height = img.Height;
                ctrl.BorderStyle = BorderStyle.FixedSingle;

                p.Color = Color.White; // une autre couleur pour différencier les tracés
                for (int i = 0; i < _rowIntensities.Count - 1; i++)
                    // on dessine un segment entre chaque point, sans inverser : les hautes intensités soient à droite
                    g.DrawLine(p, _rowIntensities[i], i, _rowIntensities[i + 1], i + 1);

                for (int i = 0; i < _colIntensities.Count - 1; i++)
                    // on dessine un segment entre chaque point, en inversant les ordonnées pour que les hautes intensités soient en haut
                    g.DrawLine(p, i, img.Height - _colIntensities[i], i + 1, img.Height - _colIntensities[i + 1]);
            }
            if (checkBox3.Checked)
            {
                Graphics I_Grph = pictureBox.CreateGraphics();
                I_Grph.DrawLine(new Pen(Color.Red), pictureBox.Width / 2, 2048, pictureBox.Width / 2, 0);
                I_Grph.DrawLine(new Pen(Color.Red), 2048, pictureBox.Height / 2, 0, pictureBox.Height / 2);
            }

            Pen l_Stylo = new Pen(Color.LightCyan, 3.0F);
            foreach (Segment l_Seg in m_Polyligne.LstSegment)
            {
                e.Graphics.DrawLine(l_Stylo, l_Seg.Pt1, l_Seg.Pt2);
            }

            l_Stylo = new Pen(Color.Red, 3.0F);
            foreach (Segment l_Seg in m_PolyligneDyn.LstSegment)
            {
                e.Graphics.DrawLine(l_Stylo, l_Seg.Pt1, l_Seg.Pt2);
            }

        }

        // coordonnées du curseur de la pictureBox + tracé de ligne dans la pictureBox
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            // coordonnées du curseur dans la pictureBox 
            textBox1.Text = "X : " + e.X.ToString();
            textBox2.Text = "Y : " + e.Y.ToString();

            // Profil en fonction de la ligne 

            // LBLcoords.Text = string.Format("Position {0:D3},{1:D3}   Pixel {2:D3},{1:D3}", new object[] { e.Location.X, e.Location.Y, e.Location.X - m_XminIMG });
            switch (m_Action)
            {
                case EAction.Aucune:
                    break;
                case EAction.SaisiePt1:
                    break;
                case EAction.SaisiePT2:
                    m_PolyligneDyn.LstSegment.Clear();
                    m_PT2 = new PointF(e.X, e.Y);
                    m_PolyligneDyn.ajouterSegment(m_PT1, m_PT2);
                    pictureBox.Refresh();
                    break;
            }

        }


        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            PointF ptx = new PointF(e.X, e.Y);

            //Mettre en commentaire la définition de _TEST au début du fichier pour la mise au point du programme
#if _OK
            switch (m_Action)
            {
                case EAction.Aucune:
                    if (m_Polyligne.LstSegment.Count > 0)
                    {
                        try
                        {
                            Segment l_Seg = m_Polyligne.getSegmentAtX(e.X);
                            Int32 l_Y = l_Seg.getOrdonneeAtX(e.X, m_XminIMG);
                            Bitmap l_Image = (Bitmap)pictureBox.Image;
                            Color l_Couleur = l_Image.GetPixel(e.X - m_XminIMG, l_Y);

                            /*Graphics l_Grph = Graphics.FromImage(PICTBcouleurR.Image);
                            l_Grph.Clear(Color.FromArgb(l_Couleur.R, 0, 0));
                            PICTBcouleurR.Refresh();
                            TBOXr.Text = string.Format("R = {0:D}", l_Couleur.R);

                            l_Grph = Graphics.FromImage(PICTBcouleurG.Image);
                            l_Grph.Clear(Color.FromArgb(0, l_Couleur.G, 0));
                            PICTBcouleurG.Refresh();
                            TBOXg.Text = string.Format("G = {0:D}", l_Couleur.G);

                            l_Grph = Graphics.FromImage(PICTBcouleurB.Image);
                            l_Grph.Clear(Color.FromArgb(0, 0, l_Couleur.B));
                            PICTBcouleurB.Refresh();
                            TBOXb.Text = string.Format("B = {0:D}", l_Couleur.B);

                            TBOXposition.Text = string.Format("Pixel {0:D} , {1:D}", new object[] { e.X - m_XminIMG, l_Y });
                            */
                        }
                        catch
                        {

                        }
                    }
                    break;
                default:
                    break;
            }
#endif

        }

        private void pictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            m_Curseur = new PointF(e.X, e.Y);
            m_PtPixel = new PointF(e.X - m_XminIMG, e.Y);
            switch (e.Button)
            {
                case MouseButtons.Left:
                    switch (m_Action)
                    {
                        case EAction.SaisiePt1:
#if _COMPLETE_DEBUTFIN_H
                            if ( (m_XminIMG <= e.X) && (e.X <= m_XmaxIMG))  
                            {
                                m_PT1 = new PointF(m_XminIMG, e.Y);
                                m_Action = EAction.SaisiePT2;
                                if ((e.X - m_XminIMG) > 5)
                                {
                                    m_Polyligne.ajouterSegment(m_PT1, new PointF(e.X, e.Y));
                                    m_PT1 = new PointF(e.X, e.Y);
                                    PICTBimage.Refresh();
                                }
                            }
#else
                            m_PT1 = new PointF(e.X, e.Y);
                            m_Action = EAction.SaisiePT2;
#endif
                            break;
                        case EAction.SaisiePT2:
                            if (e.X > m_PT1.X)
                            {
                                m_Polyligne.ajouterSegment(m_PT1, new PointF(e.X, e.Y));
                                m_PT1 = new PointF(e.X, e.Y);
                                pictureBox.Refresh();
                            }
                            break;
                    }

                    break;
                case MouseButtons.Right:
                    ContextMenu l_Ctxtmnu = new ContextMenu();
                    switch (m_Commande)
                    {
                        case ECommande.Polyligne:
                            l_Ctxtmnu.MenuItems.Add(new MenuItem("Arrêter", new EventHandler(MNUarreter)));
                            break;
                        default:
                            l_Ctxtmnu.MenuItems.Add(new MenuItem("Dessiner", new EventHandler(MNUdessiner)));
                            break;
                    }

                    l_Ctxtmnu.Show(pictureBox, e.Location);
                    break;
            }
        }


        private void MNUarreter(object sender, EventArgs e)
        {
            m_PolyligneDyn.LstSegment.Clear();

#if _COMPLETE_DEBUTFIN_H
            m_Polyligne.ajouterSegment(m_PT1, new PointF(m_XminIMG + PICTBdiagram.Width, m_PT1.Y));
#endif

            m_Commande = ECommande.Aucune;
            m_Action = EAction.Aucune;
            pictureBox.Refresh();
            afficherIntensite();
        }

        private void MNUdessiner(object sender, EventArgs e)
        {
            m_Polyligne.LstSegment.Clear();
            m_PolyligneDyn.LstSegment.Clear();
            m_Action = EAction.SaisiePt1;
            m_Commande = ECommande.Polyligne;
        }


        #region fonctions

        void activerCtrl(bool p_Activer)
        {
            //MNUprincipal.Enabled = p_Activer;
            //BARRstatut.Enabled = p_Activer;
            pictureBox2.Enabled = p_Activer;
        }

        void afficherIntensite()
        {
            Bitmap l_Image = (Bitmap)pictureBox.Image;
            Graphics grPh = Graphics.FromImage(pictureBox2.Image);
            grPh.Clear(Color.LightCoral);
            Bitmap xx = (Bitmap)pictureBox2.Image;
            foreach (Segment l_Seg in m_Polyligne.LstSegment)
            {
                Int32 l_XD = Convert.ToInt32(l_Seg.Pt1.X) - m_XminIMG;
                Int32 l_XF = Convert.ToInt32(l_Seg.Pt2.X) - m_XminIMG;

                for (int l_X = l_XD; l_X < l_XF; l_X++)
                {
                    //X et Y de l'image originale
                    Int32 l_Y = l_Seg.getOrdonneeAtX(Convert.ToDouble(l_X), m_XminIMG);
                    Color l_Coul = l_Image.GetPixel(l_X, l_Y);
                    double l_Intensite = Convert.ToDouble(l_Coul.R + l_Coul.G + l_Coul.B) / (3.0 * 255.0);

                    //calcul de Y sur l'image Diagram
                    l_Y = Convert.ToInt32(l_Intensite * Convert.ToDouble(xx.Height));
                    for (int ky = 0; ky < l_Y; ky++)
                    {
                        xx.SetPixel(l_X, 79 - ky, Color.DarkBlue); //79 - ky ==> à cause de l'axe Y orienté vers le bas
                    }
                }
            }
        }

        #endregion FONCTIONS


        private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
        {
            //Mettre en commentaire la définition de _TEST au début du fichier
#if _TEST
            PointF ptx = new PointF(e.X, e.Y);
            switch (m_Action)
            {
                case EAction.Aucune:
                    if (m_Polyligne.LstSegment.Count > 0)
                    {
                        try
                        {
                            Segment l_Seg = m_Polyligne.getSegmentAtX(e.X);
                            Int32 l_Y = l_Seg.getOrdonneeAtX(e.X, m_XminIMG);
                            Bitmap l_Image = (Bitmap)PICTBimage.Image;
                            Color l_Couleur = l_Image.GetPixel(e.X - m_XminIMG, l_Y);

                            Graphics l_Grph = Graphics.FromImage(PICTBcouleurR.Image);
                            l_Grph.Clear(Color.FromArgb(l_Couleur.R, 0, 0));
                            PICTBcouleurR.Refresh();
                            TBOXr.Text = string.Format("R = {0:D}", l_Couleur.R);

                            l_Grph = Graphics.FromImage(PICTBcouleurG.Image);
                            l_Grph.Clear(Color.FromArgb(0, l_Couleur.G, 0));
                            PICTBcouleurG.Refresh();
                            TBOXg.Text = string.Format("G = {0:D}", l_Couleur.G);

                            l_Grph = Graphics.FromImage(PICTBcouleurB.Image);
                            l_Grph.Clear(Color.FromArgb(0, 0, l_Couleur.B));
                            PICTBcouleurB.Refresh();
                            TBOXb.Text = string.Format("B = {0:D}", l_Couleur.B);

                            TBOXposition.Text = string.Format("Pixel {0:D} , {1:D}", new object[] { e.X - m_XminIMG, l_Y });

                        }
                        catch
                        {

                        }
                    }
                    break;
                default:
                    break;
            }
#endif
        }

        private void pictureBox_MouseEnter(object sender, EventArgs e)
        {
            activerCtrl(false);
        }

        private void pictureBox_MouseLeave(object sender, EventArgs e)
        {
            activerCtrl(true);
        }


Quand je trace ma ligne dans ma pictureBox, le graphe ne se trace pas et l'exception suivante survient :
" Une exception non gérée du type 'System.ArgumentNullException' s'est produite dans PylonLiveView.exe
Informations supplémentaires : La valeur ne peut pas être null. "

Quelqu'un aurait une idée à mon problème ?

Merci d'avance,

Sebbur

1 réponse

cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
Modifié par cs_ShayW le 31/08/2016 à 22:13
Bonjour


Utilise le debogger
Mets un point d'arret dans la sub qui doit tracer ta ligne et continue pas à pas.
0
Rejoignez-nous