当前位置:编程学习 > C#/ASP.NET >>

各位大大您好,新手請問關於C#寫影像顏色抓取

 private void WebCamCapture_ImageCaptured(object source,WebCam_Capture.WebcamEventArgs e)
請問有大大看過這個寫好的DLL黨的解釋嗎?
因為小弟一直找不到這個內容,還有可以請問大大image.LockBits功用嗎?


全文如下










        private void WebCamCapture_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e)
        {
            this.pictureBox2.Image = e.WebCamImage;//255.153.0,是webcam自己拍到影像之後做的事情,就像之前講的timer,我們設定時間,時間一到就會做的事情。
            Bitmap image = new Bitmap(e.WebCamImage);//new Rectangle(0, 0, e.WebCamImage.Width, e.WebCamImage.Height), e.WebCamImage.PixelFormat);
            Bitmap mask = ((Bitmap)image).Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format24bppRgb);
            ColorFiltering filter = new ColorFiltering();
            // set color ranges to keep
            filter.Red = new IntRange(threshold_Red-10, threshold_Red+10);
            filter.Green = new IntRange(threshold_Green-10, threshold_Green+10);
            filter.Blue = new IntRange(threshold_Blue-10, threshold_Blue+10);
            // apply the filter
            filter.ApplyInPlace(image);
            BitmapData imageData = image.LockBits(new Rectangle(0,0,image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite,image.PixelFormat);
            BitmapData maskData = mask.LockBits(new Rectangle(0, 0, mask.Width, mask.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, mask.PixelFormat);
            unsafe
            {
                byte* pI = (byte*)(imageData.Scan0);
                byte* pM = (byte*)(maskData.Scan0);
                for (int y = 0; y < imageData.Height; y++)
                {
                    for (int x = 0; x < imageData.Width; x++)
                    {
                        if (pI[0] != 0 && pI[1] != 0 && pI[2] != 0 && pI[3] != 0)
                        {
                            pM[0] = (byte)1;
                            pM[1] = (byte)1;
                            pM[2] = (byte)1;
                            //pM[3] = (byte)1;
                        }
                        else
                        {
                            pM[0] = (byte)0;
                            pM[1] = (byte)0;
                            pM[2] = (byte)0;
                            //pM[3] = (byte)0;
                        }
                        pI += 4;
                        pM += 3;
                    }
                }
                image.UnlockBits(imageData);
                mask.UnlockBits(maskData);
            }

            Closing filterC = new Closing();
            // apply the filter
            mask = filterC.Apply(mask);
            BlobCounterBase bc = new BlobCounter();
            bc.FilterBlobs = false;// true;
            bc.MinWidth = 50;
            bc.MinHeight = 50;
            bc.ObjectsOrder = ObjectsOrder.Size;
            bc.ProcessImage(mask);
            Graphics g = Graphics.FromImage(image);
            Blob[] blobs = bc.GetObjectsInformation();
            if (blobs.Length > 0)
            {
                g.DrawRectangle(Pens.Red, blobs[0].Rectangle);
                /*if (blobs[0].Rectangle.X - lastLocate.X > blobs[0].Rectangle.Width)
                    label9.Text = "Left";
                else if (blobs[0].Rectangle.X - lastLocate.X < blobs[0].Rectangle.Width)
                    label9.Text = "Right";
                else
                    label9.Text = "None";*/
                label9.Text = ((blobs[0].Rectangle.X + blobs[0].Rectangle.Width / 2) - image.Width / 2).ToString();
                //label10
            }
            pictureBox1.Image = image;
        }

 



        private void Capturing_Tick(object sender, EventArgs e)
        {
            try
            {
                Bitmap image = new Bitmap(pictureBox2.Image);//(BitmappictureBox2.Image.Clone();//new Rectangle(0, 0, e.WebCamImage.Width, e.WebCamImage.Height), e.WebCamImage.PixelFormat);
                Bitmap mask = ((Bitmap)image).Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format24bppRgb);
                ColorFiltering filter = new ColorFiltering();
                // set color ranges to keep
                filter.Red = new IntRange(threshold_Red - 50, threshold_Red + 50);
                filter.Green = new IntRange(threshold_Green - 50, threshold_Green + 50);
                filter.Blue = new IntRange(threshold_Blue - 50, threshold_Blue + 50);
                // apply the filter
                filter.ApplyInPlace(image);
                BitmapData imageData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, image.PixelFormat);
                BitmapData maskData = mask.LockBits(new Rectangle(0, 0, mask.Width, mask.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, mask.PixelFormat);
                unsafe
                {
                    byte* pI = (byte*)(imageData.Scan0);
                    byte* pM = (byte*)(maskData.Scan0);
                    for (int y = 0; y < imageData.Height; y++)
                    {
                        for (int x = 0; x < imageData.Width; x++)
                        {
                            if (pI[0] != 0 && pI[1] != 0 && pI[2] != 0 && pI[3] != 0)
                            {
                                pM[0] = (byte)1;
                                pM[1] = (byte)1;
                                pM[2] = (byte)1;
                                //pM[3] = (byte)1;
                            }
                            else
                            {
                                pM[0] = (byte)0;
                                pM[1] = (byte)0;
                                pM[2] = (byte)0;
                                //pM[3] = (byte)0;
                            }
                            pI += 4;
                            pM += 3;
                        }
                    }
                    image.UnlockBits(imageData);
                    mask.UnlockBits(maskData);
                }

                Closing filterC = new Closing();
                // apply the filter
                mask = filterC.Apply(mask);
                BlobCounterBase bc = new BlobCounter();
                bc.FilterBlobs = false;// true;
                bc.MinWidth = 50;
                bc.MinHeight = 50;
                bc.ObjectsOrder = ObjectsOrder.Size;
                bc.ProcessImage(mask);
                Graphics g = Graphics.FromImage(image);
                Blob[] blobs = bc.GetObjectsInformation();
                if (blobs.Length > 0)
                {
                    g.DrawRectangle(Pens.Red, blobs[0].Rectangle);
                    /*if (blobs[0].Rectangle.X - lastLocate.X > blobs[0].Rectangle.Width)
                        label9.Text = "Left";
                    else if (blobs[0].Rectangle.X - lastLocate.X < blobs[0].Rectangle.Width)
                        label9.Text = "Right";
                    else
                        label9.Text = "None";*/
                    int move = (blobs[0].Rectangle.X + blobs[0].Rectangle.Width / 2) - image.Width / 2;
                    int moveY = (blobs[0].Rectangle.Y + blobs[0].Rectangle.Height / 2) - image.Height / 2;
                    label9.Text = move.ToString();

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,