コード例 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            int i, j;

            int[,] picmat = new int[330, 250];
            int sum;
            int Ws = 0, We = 0, Hs = 0, He = 0;

            picOrigin.Image = OriginPic.Bitmap;
            Transferpic     = OriginPic.Copy();
            Licensepic      = OriginPic.Copy();


            for (i = 0; i < 320; i++)
            {
                for (j = 0; j < 240; j++)
                {
                    Color pix = Transferpic.Bitmap.GetPixel(i, j);
                    if (pix.R > 50 || pix.B < 60)
                    {
                        Transferpic.Bitmap.SetPixel(i, j, Color.White);
                        picmat[i, j] = 0;
                    }
                    else
                    {
                        Transferpic.Bitmap.SetPixel(i, j, Color.Black);
                        picmat[i, j] = 1;
                    }
                }
            }
            picRec.Image = Transferpic.Bitmap;

            for (i = 50; i < 320; i++)
            {
                sum = 0;
                for (j = 80; j < 240; j++)
                {
                    sum = sum + picmat[i, j];
                }

                if (sum > 20)
                {
                    Ws = i;
                    break;
                }
                else
                {
                    continue;
                }
            }

            for (i = Ws; i < 320; i++)
            {
                sum = 0;
                for (j = 80; j < 240; j++)
                {
                    sum = sum + picmat[i, j];
                }

                if (sum < 2)
                {
                    We = i;
                    break;
                }
                else
                {
                    continue;
                }
            }

            for (j = 80; j < 240; j++)
            {
                sum = 0;
                for (i = 50; i < 320; i++)
                {
                    sum = sum + picmat[i, j];
                }

                if (sum > 50)
                {
                    Hs = j;
                    break;
                }
                else
                {
                    continue;
                }
            }

            for (j = Hs; j < 240; j++)
            {
                sum = 0;
                for (i = 50; i < 320; i++)
                {
                    sum = sum + picmat[i, j];
                }

                if (sum < 5)
                {
                    He = j;
                    break;
                }
                else
                {
                    continue;
                }
            }

            if (He - Hs == 0)
            {
                He = He + 1;
                MessageBox.Show("System Error");
            }

            if (We - Ws == 0)
            {
                We = We + 1;
                MessageBox.Show("System Error");
            }

            OriginPic.Draw(new Rectangle(Ws, Hs, We - Ws, He - Hs), new Bgr(0, 0, 255), 2);
            SetRoiRed(Licensepic, new Rectangle(Ws, Hs, We - Ws, He - Hs));
            picOrigin.Image  = OriginPic.Bitmap;
            picLicense.Image = Licensepic.Bitmap;



            chapic1 = Licensepic.Copy();
            chapic2 = Licensepic.Copy();
            chapic3 = Licensepic.Copy();
            chapic4 = Licensepic.Copy();
            chapic5 = Licensepic.Copy();
            chapic6 = Licensepic.Copy();
            chapic7 = Licensepic.Copy();

            SetRoiRed(chapic1, new Rectangle(0, 0, (We - Ws) / 7, He - Hs));
            piccha1.Image = chapic1.Bitmap;

            SetRoiRed(chapic2, new Rectangle((We - Ws) / 7, 0, (We - Ws) / 7, He - Hs));
            piccha2.Image = chapic2.Bitmap;

            SetRoiRed(chapic3, new Rectangle((We - Ws) * 2 / 7 + (We - Ws) / 20, 0, (We - Ws) / 7, He - Hs));
            piccha3.Image = chapic3.Bitmap;

            SetRoiRed(chapic4, new Rectangle((We - Ws) * 3 / 7 + (We - Ws) / 30, 0, (We - Ws) / 7, He - Hs));
            piccha4.Image = chapic4.Bitmap;

            SetRoiRed(chapic5, new Rectangle((We - Ws) * 4 / 7 + (We - Ws) / 30, 0, (We - Ws) / 7, He - Hs));
            piccha5.Image = chapic5.Bitmap;

            SetRoiRed(chapic6, new Rectangle((We - Ws) * 5 / 7, 0, (We - Ws) / 7, He - Hs));
            piccha6.Image = chapic6.Bitmap;

            SetRoiRed(chapic7, new Rectangle((We - Ws) * 6 / 7, 0, (We - Ws) / 7, He - Hs));
            piccha7.Image = chapic7.Bitmap;

            //切割


            var grayImage = Transferpic.Convert <Gray, Byte>();
            Image <Gray, byte>    Cannypic     = new Image <Gray, byte>(Transferpic.Width, Transferpic.Height);
            Image <Gray, byte>    Thresholdpic = new Image <Gray, byte>(Transferpic.Width, Transferpic.Height);
            Image <Gray, byte>    Contourspic  = new Image <Gray, byte>(Transferpic.Width, Transferpic.Height);
            Image <Bgr, byte>     Drawpic      = new Image <Bgr, byte>(Transferpic.Width, Transferpic.Height);
            VectorOfVectorOfPoint con1         = new VectorOfVectorOfPoint();
            IntPtr Dynstorage = CvInvoke.cvCreateMemStorage(0);

            CvInvoke.cvThreshold(grayImage, Thresholdpic, 0.0001, 255.0, THRESH.CV_THRESH_OTSU);
            CvInvoke.cvCanny(grayImage, Cannypic, 60, 100, 3);

            IntPtr     Dyncontour = new IntPtr();
            MCvContour con        = new MCvContour();

            CvInvoke.cvFindContours(Thresholdpic, Dynstorage, ref Dyncontour, Marshal.SizeOf(con), RETR_TYPE.CV_RETR_TREE, CHAIN_APPROX_METHOD.CV_LINK_RUNS, new Point(0, 0));
            CvInvoke.cvDrawContours(Contourspic, Dyncontour, new MCvScalar(255, 255, 255), new MCvScalar(255, 255, 0), 2, 1, LINE_TYPE.CV_AA, new Point(0, 0));

            //待施工

            //CvInvoke.cvMinAreaRect2(Dynstorage, Contourspic);//???

            picLaststep.Image = Contourspic.Bitmap;

            houghpic = Contourspic.Copy();
            IntPtr houghline = CvInvoke.cvCreateMemStorage(0);

            ////int[,] linemat = new int[5, 1];
            CvInvoke.cvHoughLines2(houghpic, houghline, HOUGH_TYPE.CV_HOUGH_STANDARD, 1.0, 0.01745, 1, 50, 0);

            //picLaststep.Image = houghpic.Bitmap;


            grayImage.Dispose();
            OriginPic.Dispose();
            Transferpic.Dispose();
            Cannypic.Dispose();
            Contourspic.Dispose();
            Drawpic.Dispose();
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: rayee-github/VS_2010
        private void button3_Click(object sender, EventArgs e)
        {
            if (label1.Text != "")
            {
                Bitmap             imgOutput        = new Bitmap(Image.FromFile(label1.Text), 144, 144);                    //圖片位址與尺寸
                Image <Bgr, Byte>  m_BgrImage       = new Image <Bgr, byte>(imgOutput);                                     //彩色圖片
                Image <Gray, Byte> m_SourceImage    = new Image <Gray, byte>(m_BgrImage.ToBitmap());                        //灰階圖片
                Image <Gray, Byte> m_ThresholdImage = new Image <Gray, Byte>(m_SourceImage.Size);                           //二值化圖片
                Image <Gray, Byte> m_CannyImage     = new Image <Gray, Byte>(m_SourceImage.Size);                           //繪出線條
                Image <Gray, Byte> m_ContoursImage  = new Image <Gray, Byte>(m_SourceImage.Size);                           //繪出邊緣
                Image <Gray, Byte> m_ErodeImage     = new Image <Gray, byte>(m_BgrImage.ToBitmap());                        //擴張像素
                Image <Bgr, Byte>  m_FrameImage     = new Image <Bgr, Byte>(imgOutput);                                     //加邊框圖

                CvInvoke.cvThreshold(m_SourceImage, m_ThresholdImage, 0.0001, 255.0, Emgu.CV.CvEnum.THRESH.CV_THRESH_OTSU); //二值化圖片
                CvInvoke.cvCanny(m_SourceImage, m_CannyImage, 0, 255, 3);                                                   //繪出線條

                /////繪出邊緣/////
                IntPtr     Dynstorage = CvInvoke.cvCreateMemStorage(0); //圖片暫存
                IntPtr     Dyncontour = new IntPtr();                   //存放检测到的图像块的首地址
                MCvContour con        = new MCvContour();
                CvInvoke.cvFindContours(m_CannyImage, Dynstorage, ref Dyncontour, Marshal.SizeOf(con), Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_LINK_RUNS, new Point(0, 0));
                CvInvoke.cvDrawContours(m_ContoursImage, Dyncontour, new MCvScalar(255, 255, 255), new MCvScalar(255, 255, 255), 2, 1, Emgu.CV.CvEnum.LINE_TYPE.CV_AA, new Point(0, 0));

                CvInvoke.cvThreshold(m_CannyImage, m_ErodeImage, 0.0001, 255.0, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY_INV);
                m_ErodeImage = m_ErodeImage.Erode(1);//擴張處理(擴張x像素)

                /////畫外框/////
                Rectangle BoundingBox = CvInvoke.cvBoundingRect(m_CannyImage, true);
                CvInvoke.cvRectangleR(m_ContoursImage, BoundingBox, new MCvScalar(255, 0, 255, 255), 3, LINE_TYPE.CV_AA, 0);

                /////去背/////
                Image <Gray, Byte> foregroundMask = m_BgrImage.GrabCut(BoundingBox, 5);
                for (int w = 0; w < foregroundMask.Width; w++)
                {
                    for (int h = 0; h < foregroundMask.Height; h++)
                    {
                        if (foregroundMask[h, w].Intensity == 1)
                        {
                            foregroundMask[h, w] = new Gray(50);
                        }
                        else if (foregroundMask[h, w].Intensity == 2)
                        {
                            foregroundMask[h, w] = new Gray(100);
                        }
                        else if (foregroundMask[h, w].Intensity == 3)
                        {
                            foregroundMask[h, w] = new Gray(150);
                        }
                    }
                }
                //去背2
                Image <Gray, Byte> foregroundMask2 = foregroundMask;
                foregroundMask2 = foregroundMask.And(new Gray(2));
                Image <Bgr, Byte> result = m_BgrImage.Copy(foregroundMask2);

                //兩圖相加

                /*Image<Bgr, byte> m_addImage = new Image<Bgr, byte>(144, 144);
                 * Image<Gray, byte> m_addImage2 = new Image<Gray, byte>(144, 144,new Gray(255));
                 * CvInvoke.cvAdd(new Image<Bgr, byte>(new Bitmap(Image.FromFile("人.jpg"), 144, 144)), new Image<Bgr, byte>(new Bitmap(Image.FromFile("b.png"), 144, 144)), m_addImage, m_addImage2);*/

                //pictureBox_Image
                pictureBox1.Image = m_BgrImage.ToBitmap();
                pictureBox2.Image = result.ToBitmap();
                pictureBox3.Image = m_SourceImage.ToBitmap();
                pictureBox4.Image = m_ThresholdImage.ToBitmap();
                pictureBox5.Image = m_CannyImage.ToBitmap();
                pictureBox6.Image = m_ContoursImage.ToBitmap();
                pictureBox7.Image = m_ErodeImage.ToBitmap();
                pictureBox8.Image = foregroundMask.ToBitmap();
            }
        }