示例#1
0
        public void ApplyChangeDetection()
        {
            UnsafeBitmap uBit1 = new UnsafeBitmap(inputImage1);
            uBit1.MakeGreyUnsafeFaster();
            inputImage1 = new Bitmap(uBit1.Bitmap);

            UnsafeBitmap uBit2 = new UnsafeBitmap(inputImage2);
            uBit2.MakeGreyUnsafeFaster();
            inputImage2 = new Bitmap(uBit2.Bitmap);

            picSize = new Point(inputImage1.Width, inputImage1.Height);

            resultImage = new Bitmap(inputImage1);
            BitmapData resultImageData = resultImage.LockBits(new Rectangle(0, 0, picSize.X, picSize.Y), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            byte* pResultImage = (byte*)resultImageData.Scan0.ToPointer();

            bmData1 = inputImage1.LockBits(new Rectangle(0, 0, picSize.X, picSize.Y), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            bmData2 = inputImage2.LockBits(new Rectangle(0, 0, picSize.X, picSize.Y), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            pBase1 = (byte*)bmData1.Scan0.ToPointer();
            pBase2 = (byte*)bmData2.Scan0.ToPointer();

            nOffset = bmData1.Stride - inputImage1.Width * 3;

            for (int y = 0; y < picSize.Y; y++)
            {
                for (int x = 0; x < picSize.X; x++)
                {
                    // subtracting two pixels at the same position to find the difference between them
                    int tempInt = Math.Abs(pBase1[0] - pBase2[0]);

                    // if the difference is bigger than the threshold then mark that pixel
                    if (tempInt > 50) // set threshold here pls ^^
                    {
                        pResultImage[0] = 255;
                        pResultImage[1] = 255;
                        pResultImage[2] = 255;
                    }
                    else // if the difference smaller than the threshold then treat the current pixel as background
                    {
                        pResultImage[0] = 0;
                        pResultImage[1] = 0;
                        pResultImage[2] = 0;
                    }

                    pBase1 += 3;
                    pBase2 += 3;
                    pResultImage += 3;
                }
                pBase1 += nOffset;
                pBase2 += nOffset;
                pResultImage += nOffset;
            }

            inputImage1.UnlockBits(bmData1);
            inputImage2.UnlockBits(bmData2);
            resultImage.UnlockBits(resultImageData);
        }
示例#2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            //image = wc.GetCurrentImage();
            //picOriginalImage.Image = image;
            //Bitmap analyzingImage = new Bitmap(image);
            Bitmap img2 = new Bitmap(picOriginalImage.Image);
            if (rdEdgeDetection.Checked)
            {
                #region Sobel edge detection
                //UnsafeBitmap uBitmap = new UnsafeBitmap((Bitmap)wc.GetCurrentImage());
                //ConvMatrix cmx = new ConvMatrix();
                //ConvMatrix cmy = new ConvMatrix();
                //cmx.TopLeft = -1;
                //cmx.TopMid = 0;
                //cmx.TopRight = 1;

                //cmx.MidLeft = -2;
                //cmx.Pixel = 0;
                //cmx.MidRight = 2;

                //cmx.BottomLeft = -1;
                //cmx.BottomMid = 0;
                //cmx.BottomRight = 1;

                //cmy.TopLeft = 1;
                //cmy.TopMid = 2;
                //cmy.TopRight = 1;

                //cmy.MidLeft = 0;
                //cmy.Pixel = 0;
                //cmy.MidRight = 0;

                //cmy.BottomLeft = -1;
                //cmy.BottomMid = -2;
                //cmy.BottomRight = -1;

                //uBitmap.SobelEdgeDetector(cmx, cmy);

                //image = uBitmap.Bitmap;
                #endregion

                #region Canny Edge Detection
                //timer1.Stop();
                Bitmap temBM = new Bitmap(img2);
                UnsafeBitmap uBitmap = new UnsafeBitmap(temBM);
                uBitmap.MakeGreyUnsafeFaster();
                cannyEdge.ApplyCannyEdgeDetection(uBitmap.Bitmap);
                //analyzingImage = cannyEdge.Image;
                img2 = cannyEdge.Image;
                //timer1.Start();
                #endregion
            }
            else if (rdChangeDetection.Checked)
            {
                if (previousImage != null)
                {
                    Bitmap temBM = null;

                    temBM = new Bitmap(img2);
                    ChangeDetection cd = new ChangeDetection(previousImage, temBM);
                    previousImage = new Bitmap(temBM);
                    cd.ApplyChangeDetection();
                    img2 = cd.resultImage;
                }
                else
                {
                    Bitmap temBM = null;

                    temBM = new Bitmap(img2);
                    previousImage = temBM;
                }
            }
            else
            {
                // do nothing here
            }
            picAnalyzedImage.Image = img2; //wc.GetCurrentImage();
        }
示例#3
0
        private void ProcessImage(Bitmap img2)
        {
            //image = wc.GetCurrentImage();
            image = new Bitmap(img2);
            //picOriginalImage.Image = image;
            //Bitmap analyzingImage = new Bitmap(image);
            //Bitmap img2 = new Bitmap(picOriginalImage.Image);
            if (rdEdgeDetection.Checked)
            {
                #region Sobel edge detection
                //UnsafeBitmap uBitmap = new UnsafeBitmap((Bitmap)wc.GetCurrentImage());
                //ConvMatrix cmx = new ConvMatrix();
                //ConvMatrix cmy = new ConvMatrix();
                //cmx.TopLeft = -1;
                //cmx.TopMid = 0;
                //cmx.TopRight = 1;

                //cmx.MidLeft = -2;
                //cmx.Pixel = 0;
                //cmx.MidRight = 2;

                //cmx.BottomLeft = -1;
                //cmx.BottomMid = 0;
                //cmx.BottomRight = 1;

                //cmy.TopLeft = 1;
                //cmy.TopMid = 2;
                //cmy.TopRight = 1;

                //cmy.MidLeft = 0;
                //cmy.Pixel = 0;
                //cmy.MidRight = 0;

                //cmy.BottomLeft = -1;
                //cmy.BottomMid = -2;
                //cmy.BottomRight = -1;

                //uBitmap.SobelEdgeDetector(cmx, cmy);

                //image = uBitmap.Bitmap;
                #endregion

                #region Canny Edge Detection
                //timer1.Stop();
                Bitmap temBM = new Bitmap(image);
                UnsafeBitmap uBitmap = new UnsafeBitmap(temBM);
                uBitmap.MakeGreyUnsafeFaster();
                cannyEdge.ApplyCannyEdgeDetection(uBitmap.Bitmap);
                //analyzingImage = cannyEdge.Image;
                image = cannyEdge.Image;
                //timer1.Start();
                #endregion
            }
            else if (rdChangeDetection.Checked)
            {
                if (previousImage != null)
                {
                    Bitmap temBM = null;

                    temBM = new Bitmap(image);
                    ChangeDetection cd = new ChangeDetection(previousImage, temBM);
                    previousImage = new Bitmap(temBM);
                    cd.ApplyChangeDetection();
                    image = cd.resultImage;
                }
                else
                {
                    Bitmap temBM = null;

                    temBM = new Bitmap(img2);
                    previousImage = temBM;
                }
            }

            else if (rdFollowLaserDot.Checked)
            {
                UnsafeBitmap _ubit = new UnsafeBitmap(image);
                Point laserLocation = _ubit.LaserDotsRecognizer();

                Graphics g = Graphics.FromImage(image);

                using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 3))
                {
                    g.DrawLine(pen, new Point(laserLocation.X, 0), new Point(laserLocation.X, 240));
                    g.DrawLine(pen, new Point(0, laserLocation.Y), new Point(320, laserLocation.Y));
                }
                g.Dispose();

                if (laserLocation.X > 180)
                {
                    _frmPanTilt.IncreaseTrackBarValueBy("tbMotor2", -1);
                }
                else if (laserLocation.X < 140)
                {
                    _frmPanTilt.IncreaseTrackBarValueBy("tbMotor2", 1);
                }

                if (laserLocation.Y > 140)
                {
                    _frmPanTilt.IncreaseTrackBarValueBy("tbMotor1", -1);
                }
                else if (laserLocation.Y < 100)
                {
                    _frmPanTilt.IncreaseTrackBarValueBy("tbMotor1", 1);
                }
            }
            else if (rdLineFollowing.Checked)
            {
                FollowLine fl = new FollowLine(image);
                string direction = fl.ChooseDirection();

                // draw 2 lines that cross at line we are following
                Graphics g = Graphics.FromImage(image);
                using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 1))
                {
                    g.DrawLine(pen, new Point(fl.CenterX, 0), new Point(fl.CenterX, 240)); // vertical
                    g.DrawLine(pen, new Point(fl.LeftBorder, 0), new Point(fl.LeftBorder, 240)); // vertical
                    g.DrawLine(pen, new Point(fl.RightBorder, 0), new Point(fl.RightBorder, 240)); // vertical
                    //g.DrawLine(pen, new Point(0, laserLocation.Y), new Point(320, laserLocation.Y)); // horizontal
                }
                g.Dispose();
            }
            else
            {
                // do nothing here
            }
            picAnalyzedImage.Image = image; //wc.GetCurrentImage();
        }