Пример #1
0
        private void MainWindow_Load(object sender, EventArgs e)
        {
            _blobDetector = new CvBlobDetector();

            //try
            //{
            //    NameValueCollection appSettings = ConfigurationManager.AppSettings;
            //    lbconfig.Text = "ROI not set";

            //    if (appSettings.Count < 1) return;

            //    int X = 0, Y = 0, W = 0, H = 0;
            //    for (int i = 0; i < appSettings.Count; i++)
            //    {
            //        string key = appSettings.GetKey(i);
            //        if (key.CompareTo("ROIx") == 0)
            //            X = Convert.ToInt32(appSettings[i]);
            //        else if (key.CompareTo("ROIy") == 0 )
            //            Y = Convert.ToInt32(appSettings[i]);
            //        else if (key.CompareTo("ROIw") == 0 )
            //            W = Convert.ToInt32(appSettings[i]);
            //        else if (key.CompareTo("ROIh") == 0)
            //            H = Convert.ToInt32(appSettings[i]);
            //    }

            //    if (X * Y * W * H == 0) return;

            //    Global.g_rcROI = new Rectangle(X, Y, W, H);
            //    lbconfig.Text = "ROI load, (" + X + ", " + Y + ", " + W + ", " + H + ")";
            //}
            //catch (ConfigurationErrorsException){}

            //Location = new Point(Screen.PrimaryScreen.Bounds.Width - Width - 5, Screen.PrimaryScreen.Bounds.Height - Height - 25);
        }
        void Run()
        {
            try
            {
                var trt = CvInvoke.BuildInformation;

                _putenv_s("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp");
                // _putenv_s("OPENCV_FFMPEG_CAPTURE_OPTIONS", "");
                cameraCapture = new VideoCapture("rtsp://*****:*****@192.168.5.49:554/onvif1", VideoCapture.API.Ffmpeg);
                //_cameraCapture = new VideoCapture("http://192.168.1.90:81/stream?x.mjpeg", VideoCapture.API.Any);
                //_cameraCapture = new VideoCapture("rtsp://192.168.1.90:8554", VideoCapture.API.Ffmpeg);
                //_cameraCapture = new VideoCapture("http://192.168.1.90/?x.mjpeg", VideoCapture.API.Ffmpeg);
                //_cameraCapture = new VideoCapture("http://192.168.1.90", VideoCapture.API.Any);

                // Mat _frame = new Mat();
                // Mat _frameCopy = new Mat();
                // _cameraCapture.Read(_frame);
                //// _cameraCapture.Retrieve(_frame, 0);
                // _frame.CopyTo(_frameCopy);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return;
            }

            fgDetector   = new BackgroundSubtractorMOG2();
            blobDetector = new CvBlobDetector();
            tracker      = new CvTracks();

            Application.Idle += ProcessFrame;
        }
Пример #3
0
 public void mapear()
 {
     mDetector     = new Emgu.CV.VideoSurveillance.BackgroundSubtractorMOG2();
     mBlobDetector = new CvBlobDetector();
     _capture      = new Capture(mNomeDoArquivo);
     //_capture = new Capture();
     inicializarKalman();
     Application.Idle += ProcessFrame;
 }
Пример #4
0
 public BodyDetectionViewModel(ITrackedVideo headPoints = null, Dictionary <int, ISingleFrameResult> results = null, string file = "")
 {
     ThresholdValue = headPoints.ThresholdValue;
     Video          = ModelResolver.Resolve <IVideo>();
     VideoSettings  = ModelResolver.Resolve <IVideoSettings>();
     BlobDetector   = new CvBlobDetector();
     RBSK           = MouseService.GetStandardMouseRules();
     HeadPoints     = results;
     FileLocation   = file;
     OpenVideo();
 }
        private void MainWindow_Load(object sender, EventArgs e)
        {
            Global.g_MainWindow = this;

            _blobDetector = new CvBlobDetector();
            Mat mat = new Mat(".\\Res\\subImage.png", Emgu.CV.CvEnum.LoadImageType.Grayscale);

            mPrevScoreImage = mat.ToImage <Gray, Byte>();

            Location = new Point(Screen.PrimaryScreen.Bounds.Width - Width, Screen.PrimaryScreen.Bounds.Height - Height);

            _hookID = SetHook(_proc);
        }
Пример #6
0
        public void Load()
        {
            string proc = "th10e";

            _capture = new DXHook();
            _capture.AttachProcess(proc);
            _imgPower = new Image <Gray, byte>(Resources.Power);

            // Start visualization form
            if (DO_DRAWING)
            {
                _form = new THViz();
                Thread controlThread = new Thread(() =>
                {
                    Application.EnableVisualStyles();
                    Application.Run(_form);
                });
                controlThread.SetApartmentState(ApartmentState.STA);
                controlThread.Start();
            }

            new Thread(() => {
                Subscribe();
                Application.Run();
            }).Start();

            _process = _capture.Process;
            _hndl    = Win32.GetProcessHandle(_process);
            _hWnd    = _process.MainWindowHandle;
            Win32.SetForegroundWindow(_hWnd);

            _bDetect = new CvBlobDetector();

            new Thread(() =>
            {
                while (true)
                {
                    ProcessCapture();
                    if (DoMovement && !TEST_MODE)
                    {
                        DoPlayerMovement(_force);
                    }
                    Thread.Sleep(1);
                }
            }).Start();
        }
Пример #7
0
        protected override void inicializarVariaveis()
        {
            base.inicializarVariaveis();
            mDetector     = new Emgu.CV.VideoSurveillance.BackgroundSubtractorMOG2();
            mBlobDetector = new CvBlobDetector();

            Size vTamanhoDasImagens = mImagemColorida.Size;

            mImagemCinzaSemPlanoDeFundo   = new Mat();
            mImagemDoPlanoDeFundo         = new Mat(vTamanhoDasImagens.Width, vTamanhoDasImagens.Height, DepthType.Cv32F, 3);
            mImagemSemPlanoDeFundo        = null;// = cvCreateImage(gTamanhoDaImagem, IPL_DEPTH_32F, 3);;
            mCopiaImagemPlanoDeFundo      = null;
            mImagemBinariaSemPlanoDeFundo = new Mat();
            vHist = new Emgu.CV.UI.HistogramBox();
            vHist.Show();
            vHist.Visible       = true;
            mPrimeiraExecucao   = true;
            dicionarioMonitores = new Dictionary <int, MonitorDePessoa>();
            dicionarioBlobs     = new Dictionary <int, MCvBlob>();
        }
Пример #8
0
        public Form1()
        {
            InitializeComponent();

            minH = minS = minV = 0;
            maxH = maxS = maxV = 0;

            erosions = dilations = 0;


            try
            {
                camera = new Capture();
            }

            catch (TypeInitializationException exc)
            {
                MessageBox.Show(exc.Message);
            }



            CvBlobDetector blobDetector = new CvBlobDetector();
        }
Пример #9
0
        /// <summary>
        /// Finds blobs with the specified min and max area.
        /// </summary>
        /// <param name="src">Source image</param>
        /// <param name="minArea">The minimum allowable area</param>
        /// <param name="maxArea">The maximum allowable area</param>
        /// <returns>List containing the X and Y coordinates of the found blobs</returns>
        public IEnumerable <PointF> FindBlobs(Bitmap src, uint minArea, uint maxArea)
        {
            if (src == null)
            {
                throw new ArgumentNullException(nameof(src), @"Source image cannot be null");
            }

            using (var sourceImage = new Image <Gray, byte>(src))
                using (var blobDetector = new CvBlobDetector( ))
                    using (var blobs = new CvBlobs( ))
                    {
                        Image <Gray, byte> filteredSrc = null;

                        try
                        {
                            // Binarize and invert the image so that
                            // that blob detector can locate the dots.
                            filteredSrc = sourceImage.ThresholdBinaryInv(new Gray(90), new Gray(255));

                            // Finds all blobs in the input image and
                            // stores them in to the CvBlobs structure.
                            blobDetector.Detect(filteredSrc, blobs);

                            // Filter the blobs by area. The alignment dots
                            // have an average area of roughly 3500 pixels.
                            blobs.FilterByArea(( int )minArea, ( int )maxArea);

                            // Return the centroids of each blob.
                            return(blobs.Values.Select(b => new PointF(b.Centroid.X, b.Centroid.Y)));
                        }
                        finally
                        {
                            filteredSrc?.Dispose( );
                        }
                    }
        }
Пример #10
0
        public void BlobDetect()
        {
            BlobList.Clear();
            CvBlobs        blobs         = new CvBlobs();
            CvBlobDetector _blobDetector = new CvBlobDetector();

            _blobDetector.Detect(MatBinary.ToImage <Gray, byte>(), blobs);

            foreach (KeyValuePair <uint, CvBlob> item in blobs)
            {
                CvBlob   b        = item.Value;
                BlobInfo blobinfo = new BlobInfo
                {
                    Area   = b.Area,
                    Width  = b.BoundingBox.Right - b.BoundingBox.Left,
                    Height = b.BoundingBox.Bottom - b.BoundingBox.Top,
                };

                blobinfo.Rect.Location = new Point(ROI.Left + b.BoundingBox.Left, ROI.Top + b.BoundingBox.Top);
                blobinfo.Rect.Size     = new Size(blobinfo.Width, blobinfo.Height);

                BlobList.Add(blobinfo);
            }
        }
Пример #11
0
        private void ProcessFrame(object sender, EventArgs args)
        {
            //Get frame
            Mat frame = camera.QueryFrame();

            //Process frame
            Image <Bgr, Byte> img = frame.ToImage <Bgr, Byte>();

            img.ROI = new Rectangle(100, 100, 300, 300);
            Image <Hsv, Byte>  HSVimg  = img.Convert <Hsv, Byte>();
            Image <Gray, Byte> binary  = HSVimg.InRange(new Hsv(minH, minS, minV), new Hsv(maxH, maxS, maxV));
            Image <Gray, Byte> eroded  = binary.Erode(erosions);
            Image <Gray, Byte> dilated = eroded.Dilate(dilations);

            //Detect largest blob
            CvBlobDetector blobDetector = new CvBlobDetector();
            CvBlobs        blobs        = new CvBlobs();

            blobDetector.Detect(dilated, blobs);


            int    maxBlobArea = 0;
            CvBlob largestBlob = null;

            foreach (CvBlob blob in blobs.Values)
            {
                if (blob.Area > maxBlobArea)
                {
                    maxBlobArea = blob.Area;
                    largestBlob = blob;
                }
            }


            if (largestBlob != null && largestBlob.Area >= 10000)
            {
                handContour = largestBlob.GetContour();

                VectorOfInt   convexHullIndices = new VectorOfInt();
                VectorOfPoint convexHull        = new VectorOfPoint();

                CvInvoke.ConvexHull(new VectorOfPoint(handContour), convexHull);
                CvInvoke.ConvexHull(new VectorOfPoint(handContour), convexHullIndices);

                Mat defects = new Mat();


                //img.Draw(handContour, new Bgr(0, 0, 255),3);
                img.Draw(convexHull.ToArray(), new Bgr(255, 0, 0), 3);

                try
                {
                    CvInvoke.ConvexityDefects(new VectorOfPoint(handContour), convexHullIndices, defects);
                }

                catch (CvException exc)
                {
                    MessageBox.Show(exc.Message);
                }


                if (!defects.IsEmpty)
                {
                    Matrix <int> defectsInt = new Matrix <int>(defects.Rows, defects.Cols, defects.NumberOfChannels);

                    defects.CopyTo(defectsInt);


                    int countFingers = 0;

                    for (int i = 0; i < defectsInt.Rows; i++)
                    {
                        int   startIdx    = defectsInt.Data[i, 0];
                        int   endIdx      = defectsInt.Data[i, 1];
                        int   farthestIdx = defectsInt.Data[i, 2];
                        float distance    = defectsInt.Data[i, 3];


                        if (distance >= 15000)
                        {
                            //distances.Add(distance);

                            Point startPoint    = handContour[startIdx];
                            Point endPoint      = handContour[endIdx];
                            Point farthestPoint = handContour[farthestIdx];

                            img.Draw(new CircleF(startPoint, 2.0f), new Bgr(0, 255, 0), 2);
                            img.Draw(new CircleF(endPoint, 2.0f), new Bgr(255, 0, 0), 2);
                            img.Draw(new CircleF(farthestPoint, 2.0f), new Bgr(0, 0, 255), 2);


                            CvInvoke.Line(img, startPoint, farthestPoint, new MCvScalar(255, 255, 0));
                            countFingers++;
                        }
                    }


                    //Approssimo conteggio dita, e classifico : 1 dito = play, 5 dita = pausa

                    if (Math.Abs(countFingers - 1) < Math.Abs(countFingers - 5)
                        &&
                        Math.Abs(countFingers - 1) < Math.Abs(countFingers - 2))
                    {
                        label10.Text = "Play";
                        axWindowsMediaPlayer1.Ctlcontrols.play();
                    }

                    else if (Math.Abs(countFingers - 5) < Math.Abs(countFingers - 1)
                             &&
                             Math.Abs(countFingers - 5) < Math.Abs(countFingers - 2))
                    {
                        label10.Text = "Pause";
                        axWindowsMediaPlayer1.Ctlcontrols.pause();
                    }

                    else if (Math.Abs(countFingers - 2) < Math.Abs(countFingers - 1)
                             &&
                             Math.Abs(countFingers - 2) < Math.Abs(countFingers - 5))
                    {
                        label10.Text = "Volume Up";
                        axWindowsMediaPlayer1.Ctlcontrols.pause();

                        axWindowsMediaPlayer1.settings.volume++;
                    }
                }
            }

            pictureBox1.Image = binary.Bitmap;
        }
Пример #12
0
        private void ProcessFrame(object sender, EventArgs e)
        {
            DateTime methodStart = DateTime.Now;

            Image <Bgr, Byte>  redFiltered   = null;
            Image <Bgr, Byte>  ycbcrFiltered = null;
            Image <Gray, Byte> blobImage     = null;

            Image <Bgr, Byte> rawFrame = videoCapture.QueryFrame().ToImage <Bgr, Byte>();

            rawFrame = rawFrame.Resize(320, 240, Emgu.CV.CvEnum.Inter.Cubic);
            rawFrame._EqualizeHist();

            if (detectFire)
            {
                redFiltered   = redTreshhold(rawFrame);
                ycbcrFiltered = yCbCrThreshold(redFiltered);
                blobImage     = binaryTreshold(ycbcrFiltered);

                CvBlobs        blobs        = new CvBlobs();
                CvBlobDetector blobDetector = new CvBlobDetector();
                uint           blobCount    = blobDetector.Detect(blobImage, blobs);

                int minArea = (int)(rawFrame.Width * rawFrame.Height * 0.002);

                foreach (KeyValuePair <uint, CvBlob> blobPair in blobs)
                {
                    if (blobPair.Value.Area > minArea)
                    {
                        Rectangle rect = blobPair.Value.BoundingBox;
                        rawFrame.Draw(rect, new Bgr(0, 255, 0), 5);
                    }
                }
            }

            picPreview.Image = rawFrame.Bitmap;
            if (detectFire)
            {
                picRedFilter.Image = redFiltered.Bitmap;
                picFinal.Image     = blobImage.Bitmap;
            }
            else
            {
                picRedFilter.Image = null;
                picFinal.Image     = null;
            }

            if (frameSkip)
            {
                int timePassed   = (DateTime.Now - methodStart).Milliseconds;
                int framesToSkip = timePassed / frameInterval;
                for (int i = 0; i < framesToSkip; i++)
                {
                    videoCapture.QueryFrame();
                }
            }

            int currentFrame = (int)videoCapture.GetCaptureProperty(CapProp.PosFrames);
            int frameCount   = (int)videoCapture.GetCaptureProperty(CapProp.FrameCount);

            if (currentFrame != -1 && frameCount != -1)
            {
                trbSeek.Value = currentFrame;
                if (currentFrame == frameCount)
                {
                    CloseVideo();
                }
            }
        }
Пример #13
0
 public RBSKVideo()
 {
     BlobDetector = new CvBlobDetector();
 }
Пример #14
0
 public RBSKVideo2Image()
 {
     BlobDetector = new CvBlobDetector();
 }
Пример #15
0
        private void ProcessFrame(object sender, EventArgs e)
        {
            //String str = String.Format("withBall.jpg");

            //originalImg = CvInvoke.Imread(str)


            originalImg = capture.QueryFrame();
            Image <Bgr, Byte> outputImg = originalImg.ToImage <Bgr, Byte>();



            int imgWidth  = originalImg.Width;
            int imgHeight = originalImg.Height;


            UMat grayImg = new UMat();

            //Convert RBG to Gray
            CvInvoke.CvtColor(originalImg, grayImg, ColorConversion.Bgr2Gray);

            //use image pyr to remove noise
            UMat pyrDown = new UMat();

            CvInvoke.PyrDown(grayImg, pyrDown);
            CvInvoke.PyrUp(pyrDown, grayImg);


            UMat binaryImg = new UMat();

            //Find Potiential Plate Region
            CvInvoke.Threshold(grayImg, binaryImg, 200, 255, ThresholdType.BinaryInv);

            Image <Gray, Byte> binaryImgG = binaryImg.ToImage <Gray, Byte>();


            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

            int[,] hierachy = CvInvoke.FindContourTree(binaryImgG, contours, ChainApproxMethod.ChainApproxNone);

            int maxArea             = 0;
            int maxAreaContourIndex = 0;

            for (int idx = 0; idx < contours.Size; idx++)
            {
                //bool isChild = isChildContour(hierachy, idx);

                int numberOfChildren = GetNumberOfChildren(hierachy, idx);
                using (VectorOfPoint contour = contours[idx])
                {
                    if ((numberOfChildren > 3))
                    {
                        if (CvInvoke.ContourArea(contour) > maxArea)
                        {
                            maxAreaContourIndex = idx;
                        }
                    }
                }
            }


            Image <Gray, Byte> mask1 = new Image <Gray, Byte>(imgWidth, imgHeight);

            CvInvoke.DrawContours(mask1, contours, maxAreaContourIndex, new MCvScalar(255), -1);

            int openingFactor1           = 100;
            Image <Gray, Byte> plateMask = new Image <Gray, Byte>(imgWidth, imgHeight);

            plateMask = mask1.Erode(openingFactor1);
            plateMask = plateMask.Dilate(openingFactor1);

            CvBlobs        blobs         = new CvBlobs();
            CvBlobDetector blob_detector = new CvBlobDetector();

            //blobs.FilterByArea(10000, 1000000);
            blob_detector.Detect(plateMask, blobs);

            foreach (CvBlob blob in blobs.Values)
            {
                Rectangle r = blob.BoundingBox;

                outputImg.Draw(r, new Bgr(0, 255, 255), 4);
            }

            Image <Gray, Byte> invBinaryImgG = binaryImg.ToImage <Gray, Byte>();

            CvInvoke.BitwiseNot(invBinaryImgG, invBinaryImgG);


            Image <Gray, Byte> mask3 = plateMask.Clone();

            CvInvoke.BitwiseAnd(plateMask, invBinaryImgG, mask3);


            blob_detector.Detect(mask3, blobs);

            int patternSize = 20;
            int ballSize    = 60;
            int tolerance   = 10;

            int patternHigh = patternSize + tolerance;
            int patternLow  = patternSize - tolerance;

            int ballHigh = ballSize + tolerance * 2;
            int ballLow  = ballSize - tolerance * 2;

            blobs.FilterByArea(patternLow * patternLow, ballHigh * ballHigh);

            List <PointF> patternPoints             = new List <PointF>();
            PointF        ballPoint                 = new PointF();
            int           numberOfPatternPointFound = 0;

            foreach (CvBlob blob in blobs.Values)
            {
                Rectangle r = blob.BoundingBox;

                if ((r.Height > patternLow) && (r.Height < patternHigh) &&
                    (r.Width > patternLow) && (r.Width < patternHigh))
                {
                    outputImg.Draw(new CircleF(blob.Centroid, 2), new Bgr(0, 0, 255), 2);
                    patternPoints.Add(blob.Centroid);
                    numberOfPatternPointFound++;
                }

                if ((r.Height > ballLow) && (r.Height < ballHigh) &&
                    (r.Width > ballLow) && (r.Width < ballHigh))
                {
                    outputImg.Draw(new CircleF(blob.Centroid, 5), new Bgr(0, 0, 255), 5);
                    ballPoint = blob.Centroid;
                }
            }

            label14.Text = String.Format("{0}", numberOfPatternPointFound);
            List <PointF> sortedPatternPoints = new List <PointF>();
            // 1 for TopLeft - 2 for Top Right - 3 for Bottom Right - 4 for Bottom Left
            List <int> pointType = new List <int>();;

            PointF centerPoint = new PointF();

            foreach (PointF patternPoint in patternPoints)
            {
                centerPoint.X += patternPoint.X;
                centerPoint.Y += patternPoint.Y;
            }
            centerPoint.X /= numberOfPatternPointFound;
            centerPoint.Y /= numberOfPatternPointFound;

            x_position.Text = ballPoint.X.ToString();
            y_position.Text = ballPoint.Y.ToString();
            foreach (PointF patternPoint in patternPoints)
            {
                if ((patternPoint.X < centerPoint.X) && (patternPoint.Y < centerPoint.Y))
                {
                    sortedPatternPoints.Add(patternPoint);
                    pointType.Add(1);
                }
                else if ((patternPoint.X > centerPoint.X) && (patternPoint.Y < centerPoint.Y))
                {
                    sortedPatternPoints.Add(patternPoint);
                    pointType.Add(2);
                }
                else if ((patternPoint.X > centerPoint.X) && (patternPoint.Y > centerPoint.Y))
                {
                    sortedPatternPoints.Add(patternPoint);
                    pointType.Add(3);
                }
                else if ((patternPoint.X < centerPoint.X) && (patternPoint.Y > centerPoint.Y))
                {
                    sortedPatternPoints.Add(patternPoint);
                    pointType.Add(4);
                }
            }

            int id = 0;

            foreach (PointF patternPoint in sortedPatternPoints)
            {
                CvInvoke.PutText(outputImg,
                                 String.Format("{0}", pointType[id++]),
                                 new System.Drawing.Point((int)patternPoint.X, (int)patternPoint.Y),
                                 FontFace.HersheyComplex,
                                 1.0,
                                 new Bgr(0, 255, 0).MCvScalar);
            }



            imageBox1.Image = outputImg;
        }
Пример #16
0
        //##############################################################################################################################################################################################

        /// <summary>
        /// Extract all pieces from the source image.
        /// </summary>
        private void extract_pieces()
        {
            try
            {
                CurrentSolverState = PuzzleSolverState.INIT_PIECES;
                Piece.NextPieceID  = 0;
                CurrentSolverStepPercentageFinished = 0;
                _logHandle.Report(new LogEventInfo("Extracting Pieces"));
                NumberPuzzlePieces = 0;

                Pieces.Clear();
                InputImages.Clear();

                List <string> imageExtensions = new List <string>()
                {
                    ".jpg", ".png", ".bmp", ".tiff"
                };
                FileAttributes  attr           = File.GetAttributes(PuzzlePiecesFolderPath);
                List <FileInfo> imageFilesInfo = new List <FileInfo>();
                if (attr.HasFlag(FileAttributes.Directory))      //detect whether its a directory or file
                {
                    DirectoryInfo folderInfo = new DirectoryInfo(PuzzlePiecesFolderPath);
                    imageFilesInfo = folderInfo.GetFiles().ToList();
                }
                else
                {
                    FileInfo fileInfo = new FileInfo(PuzzlePiecesFolderPath);
                    imageFilesInfo.Add(fileInfo);
                }

                imageFilesInfo = imageFilesInfo.Where(f => imageExtensions.Contains(f.Extension)).ToList();

                int loopCount = 0;

                ParallelOptions parallelOptions = new ParallelOptions
                {
                    CancellationToken      = _cancelToken,
                    MaxDegreeOfParallelism = (PluginFactory.GetGeneralSettingsPlugin().UseParallelLoops ? Environment.ProcessorCount : 1)
                };
                //For each input image
                Parallel.For(0, imageFilesInfo.Count, parallelOptions, (i) =>
                {
                    using (Image <Rgba, byte> sourceImg = new Image <Rgba, byte>(imageFilesInfo[i].FullName)) //.LimitImageSize(1000, 1000))
                    {
                        CvInvoke.MedianBlur(sourceImg, sourceImg, 5);

                        // Get the (first) enabled Plugin for input image mask generation
                        PluginGroupInputImageMask pluginInputImageMask = PluginFactory.GetEnabledPluginsOfGroupType <PluginGroupInputImageMask>().FirstOrDefault();

                        using (Image <Gray, byte> mask = pluginInputImageMask.GetMask(sourceImg))
                        {
                            _logHandle.Report(new LogEventInfo("Extracting Pieces from source image " + i.ToString()));
                            if (PluginFactory.GetGeneralSettingsPlugin().SolverShowDebugResults)
                            {
                                _logHandle.Report(new LogEventImage("Source image " + i.ToString(), sourceImg.Bitmap));
                                _logHandle.Report(new LogEventImage("Mask " + i.ToString(), mask.Bitmap));
                            }

                            CvBlobDetector blobDetector = new CvBlobDetector();                 // Find all blobs in the mask image, extract them and add them to the list of pieces
                            CvBlobs blobs = new CvBlobs();
                            blobDetector.Detect(mask, blobs);

                            foreach (CvBlob blob in blobs.Values.Where(b => b.BoundingBox.Width >= PluginFactory.GetGeneralSettingsPlugin().PuzzleMinPieceSize&& b.BoundingBox.Height >= PluginFactory.GetGeneralSettingsPlugin().PuzzleMinPieceSize))
                            {
                                if (_cancelToken.IsCancellationRequested)
                                {
                                    _cancelToken.ThrowIfCancellationRequested();
                                }

                                Rectangle roi = blob.BoundingBox;

                                Image <Rgba, byte> pieceSourceImg;
                                Image <Gray, byte> pieceMask;

                                try
                                {
                                    if (sourceImg.Height > roi.Height + 4 && sourceImg.Width > roi.Width + 4)
                                    {
                                        roi.Inflate(2, 2);
                                    }
                                    pieceSourceImg = sourceImg.Copy(roi);
                                    pieceMask      = mask.Copy(roi);
                                }
                                catch (Exception)
                                {
                                    roi            = blob.BoundingBox;
                                    pieceSourceImg = sourceImg.Copy(roi);
                                    pieceMask      = mask.Copy(roi);
                                }

                                // Mask out background of piece
                                Image <Rgba, byte> pieceSourceImageForeground = new Image <Rgba, byte>(pieceSourceImg.Size);
                                CvInvoke.BitwiseOr(pieceSourceImg, pieceSourceImg, pieceSourceImageForeground, pieceMask);

                                Image <Gray, byte> pieceMaskInverted = pieceMask.Copy(pieceMask);
                                pieceMaskInverted._Not();
                                Image <Rgba, byte> background = new Image <Rgba, byte>(pieceSourceImg.Size);
                                background.SetValue(new Rgba(255, 255, 255, 0));
                                Image <Rgba, byte> pieceSourceImageBackground = new Image <Rgba, byte>(pieceSourceImg.Size);
                                CvInvoke.BitwiseOr(background, background, pieceSourceImageBackground, pieceMaskInverted);

                                Image <Rgba, byte> pieceSourceImgMasked = new Image <Rgba, byte>(pieceSourceImg.Size);
                                CvInvoke.BitwiseOr(pieceSourceImageForeground, pieceSourceImageBackground, pieceSourceImgMasked);

                                Piece p = new Piece(pieceSourceImgMasked, pieceMask, imageFilesInfo[i].FullName, roi.Location, _logHandle, _cancelToken);
                                lock (_piecesLock) { Pieces.Add(p); }

                                sourceImg.Draw(roi, new Rgba(255, 0, 0, 1), 2);
                                int baseLine  = 0;
                                Size textSize = CvInvoke.GetTextSize(p.PieceID.Replace("Piece", ""), FontFace.HersheyDuplex, 3, 2, ref baseLine);
                                CvInvoke.PutText(sourceImg, p.PieceID.Replace("Piece", ""), Point.Add(roi.Location, new Size(0, textSize.Height + 10)), FontFace.HersheyDuplex, 3, new MCvScalar(255, 0, 0), 2);

                                NumberPuzzlePieces++;

                                pieceSourceImg.Dispose();
                                pieceMask.Dispose();
                                pieceSourceImageForeground.Dispose();
                                pieceMaskInverted.Dispose();
                                background.Dispose();
                                pieceSourceImageBackground.Dispose();
                                pieceSourceImgMasked.Dispose();

                                GC.Collect();
                            }

                            Interlocked.Add(ref loopCount, 1);
                            CurrentSolverStepPercentageFinished = (loopCount / (double)imageFilesInfo.Count) * 100;

                            if (PluginFactory.GetGeneralSettingsPlugin().SolverShowDebugResults)
                            {
                                _logHandle.Report(new LogEventImage("Source Img " + i.ToString() + " Pieces", sourceImg.Bitmap));
                            }
                            InputImages.Add(new ImageDescribedLight(Path.GetFileName(imageFilesInfo[i].FullName), PuzzlePiecesFolderPath + @"\Results\InputImagesMarked\" + Path.GetFileName(imageFilesInfo[i].FullName), sourceImg.Bitmap)); //sourceImg.LimitImageSize(1000, 1000).Bitmap));
                            blobs.Dispose();
                            blobDetector.Dispose();
                            GC.Collect();
                        }
                    }

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                });

                Pieces.Sort(p => ((Piece)p).PieceIndex, null);
            }
            catch (OperationCanceledException)
            {
                _logHandle.Report(new LogEventWarning("The operation was canceled. Step: " + CurrentSolverState.ToString()));
                CurrentSolverState = PuzzleSolverState.UNSOLVED;
            }
            catch (Exception ex)
            {
                _logHandle.Report(new LogEventError("The following error occured in step " + CurrentSolverState.ToString() + ":\n" + ex.Message));
                CurrentSolverState = PuzzleSolverState.ERROR;
                CurrentSolverStepPercentageFinished = 100;
            }
        }
Пример #17
0
        List <Building> building_list = new List <Building>(); //blob된 장애물 정보 저장

        //영상에서 차량을 제외 한 후 blob을 검출하고 색상을 통해 장애물 판별
        public int detectBlob(Image <Bgr, Byte> blob_image, int[,] Map_obstacle, Rectangle[] tracking_rect)
        {
            int blob_count = 0;

            Image <Gray, Byte> graySoft = blob_image.Clone().Convert <Gray, Byte>().PyrDown().PyrUp();
            Image <Gray, Byte> gray     = graySoft.SmoothGaussian(3);

            //gray = gray.AddWeighted(graySoft, 1.3, -0.6, 0);
            //Image<Gray, Byte> bin = gray.ThresholdBinary(new Gray(70), new Gray(255));

            //gray = gray.AddWeighted(graySoft, 0.95, -0.1, 0);
            //Image<Gray, Byte> bin = gray.ThresholdBinary(new Gray(85), new Gray(255));

            gray = gray.AddWeighted(graySoft, 1.3, -0.6, 0);
            //Image<Gray, Byte> bin = gray.ThresholdBinary(new Gray(60), new Gray(255));
            Image <Gray, Byte> bin = gray.ThresholdBinary(new Gray(57), new Gray(255));

            Gray cannyThreshold              = new Gray(149);
            Gray cannyThresholdLinking       = new Gray(149);
            Image <Gray, Byte> greyThreshImg = bin.Canny(cannyThreshold.Intensity, cannyThresholdLinking.Intensity);

            CvBlobs        resultingImgBlobs = new CvBlobs();
            CvBlobDetector bDetect           = new CvBlobDetector();

            bDetect.Detect(greyThreshImg, resultingImgBlobs);

            Image <Bgr, Byte> temp_img = greyThreshImg.Convert <Bgr, Byte>();

            #region 차량 충돌 및 위기 검사

            //영상에서 차량 범위를 빼고 ROI만들기
            for (int i = 0; i < 4; i++)
            {
                if (tracking_rect[i].Width != 0 && tracking_rect[i].Height != 0)
                {
                    int pos_x = tracking_rect[i].X - globals.x_grid;
                    int pos_y = tracking_rect[i].Y - globals.y_grid;

                    //이미지가 범위를 벗어날경우 처리
                    if (pos_x < 0)
                    {
                        pos_x = 0;
                    }
                    if (pos_y < 0)
                    {
                        pos_y = 0;
                    }

                    if (pos_x + tracking_rect[i].Width > globals.rect_width)
                    {
                        pos_x = globals.rect_width - tracking_rect[i].Width;
                    }
                    if (pos_y + tracking_rect[i].Height > globals.rect_height)
                    {
                        pos_y = globals.rect_height - tracking_rect[i].Height;
                    }

                    for (int x = pos_x; x < pos_x + tracking_rect[i].Width + globals.x_grid; x++)
                    {
                        for (int y = pos_y; y < pos_y + tracking_rect[i].Height + globals.y_grid; y++)
                        {
                            temp_img[y, x] = new Bgr(0, 0, 0);
                            if (x % globals.x_grid == 0 && y % globals.y_grid == 0)
                            {
                                int t_x = x;
                                int t_y = y;

                                if (t_x != 0)
                                {
                                    t_x = x / globals.x_grid;
                                }

                                if (t_y != 0)
                                {
                                    t_y = y / globals.y_grid;
                                }

                                Map_obstacle[t_y, t_x] = i + 1; // 잡힌 차량은 Map에 2라고 표시
                            }
                        }
                    }

                    //차량 vs 차량 충돌 검사
                    for (int j = 0; j < 4; j++)
                    {
                        int leftA, leftB;
                        int rightA, rightB;
                        int topA, topB;
                        int bottomA, bottomB;

                        if (i != j)
                        {
                            if (!(tracking_rect[j].Width == 0 && tracking_rect[j].Height == 0))
                            {
                                //int add_size = 23;
                                int add_size = 15;

                                leftA   = tracking_rect[i].X - add_size;
                                rightA  = tracking_rect[i].X + tracking_rect[i].Width + add_size;
                                topA    = tracking_rect[i].Y - add_size;
                                bottomA = tracking_rect[i].Y + tracking_rect[i].Height + add_size;

                                leftB   = tracking_rect[j].X - add_size;
                                rightB  = tracking_rect[j].X + tracking_rect[j].Width + add_size;
                                topB    = tracking_rect[j].Y - add_size;
                                bottomB = tracking_rect[j].Y + tracking_rect[j].Height + add_size;

                                if (bottomA < topB)
                                {
                                    continue;                 //아래
                                }
                                if (topA > bottomB)
                                {
                                    continue;                 //위
                                }
                                if (rightA < leftB)
                                {
                                    continue;                 //오른쪽
                                }
                                if (leftA > rightB)
                                {
                                    continue;                 //왼쪽
                                }
                                int boarder_size = 15;

                                if (bottomA - topB <= boarder_size || bottomB - topA <= boarder_size || rightA - leftB <= boarder_size || rightB - leftA <= boarder_size)
                                {
                                    //Console.WriteLine("ObstacleDetection : " + i + " 차량과 " + j + " 차량이 충돌 위기");

                                    bool isEmpty = false;

                                    if (globals.evasionInfo.Count == 0)
                                    {
                                        globals.evasionInfo.Add(new KeyValuePair <int, int>(i, j));
                                        continue;
                                    }

                                    foreach (var evsionTempList in globals.evasionInfo)
                                    {
                                        if (evsionTempList.Key == i && evsionTempList.Value == j)
                                        {
                                            isEmpty = true;
                                        }
                                        else if (evsionTempList.Key == j && evsionTempList.Value == i)
                                        {
                                            isEmpty = true;
                                        }
                                    }

                                    if (!isEmpty)
                                    {
                                        globals.evasionInfo.Add(new KeyValuePair <int, int>(i, j));
                                    }
                                }

                                else
                                {
                                    bool isEmpty = false;

                                    if (globals.evasionInfo.Count == 0)
                                    {
                                        globals.evasionInfo.Add(new KeyValuePair <int, int>(i, j));
                                        continue;
                                    }

                                    foreach (var evsionTempList in globals.evasionInfo)
                                    {
                                        if (evsionTempList.Key == i && evsionTempList.Value == j)
                                        {
                                            isEmpty = true;
                                        }
                                        else if (evsionTempList.Key == j && evsionTempList.Value == i)
                                        {
                                            isEmpty = true;
                                        }
                                    }

                                    if (!isEmpty)
                                    {
                                        globals.evasionInfo.Add(new KeyValuePair <int, int>(i, j));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            #endregion 차량 충돌 및 위기 검사

            int[] temp_color_count = new int[4]; //[0]purple [1] black [2] yellow [3]
            temp_color_count = (int[])obstacle_color_count.Clone();
            int temp_blob_count = building_list.Count;

            List <Building> tmp_list = new List <Building>(); //몇개 있는지 확인후에 제거 하면서 검사 하기 위해
            for (int i = 0; i < building_list.Count; i++)
            {
                tmp_list.Add(new Building(building_list[i].Id, building_list[i].Width, building_list[i].Height, building_list[i].X, building_list[i].Y, building_list[i].BuildingColor, building_list[i].DisapperCheck));
            }

            //blob 검출
            foreach (CvBlob targetBlob in resultingImgBlobs.Values)
            {
                if (targetBlob.Area > 100 && targetBlob.Area < 700)
                {
                    string color_str;
                    int    color_index = -1;

                    /*
                     * // 장애물 범위 지정안했을때
                     * int temp_x, temp_y, temp_width, temp_height;
                     *
                     * temp_x = targetBlob.BoundingBox.X;
                     * temp_y = targetBlob.BoundingBox.Y;
                     *
                     * temp_width = targetBlob.BoundingBox.Width;
                     * temp_height = targetBlob.BoundingBox.Height;
                     */

                    /*
                     * //장애물의 충돌 검사를 위해 범위 설정
                     * int xx = 10, yy = 10;
                     * int temp_x, temp_y, temp_width, temp_height;
                     *
                     * temp_x = targetBlob.BoundingBox.X - xx;
                     * temp_width = targetBlob.BoundingBox.Width + xx * 2;
                     * if (temp_x < 0)
                     * {
                     *  temp_x = 0;
                     *  temp_width = targetBlob.BoundingBox.Width + xx;
                     * }
                     *
                     * temp_y = targetBlob.BoundingBox.Y - yy;
                     * temp_height = targetBlob.BoundingBox.Height + yy * 2;
                     * if (temp_y < 0)
                     * {
                     *  temp_y = 0;
                     *  temp_height = targetBlob.BoundingBox.Height + yy;
                     *
                     * }
                     * //여기 장애물이 화면 크기 벗어 났을때 에러 처리 안해놓은듯함!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                     */


                    //검출된 색이 장애물인지
                    if ((color_str = obstacle_colorCheck(blob_image, targetBlob.Area, targetBlob.BoundingBox.X, targetBlob.BoundingBox.Y, targetBlob.BoundingBox.Width, targetBlob.BoundingBox.Height)) == "null")
                    {
                        for (int x = targetBlob.BoundingBox.X; x < targetBlob.BoundingBox.X + targetBlob.BoundingBox.Width; x++)
                        {
                            for (int y = targetBlob.BoundingBox.Y; y < targetBlob.BoundingBox.Y + targetBlob.BoundingBox.Height; y++)
                            {
                                temp_img[y, x] = new Bgr(0, 0, 0);
                            }
                        }
                        continue; //장애물색상이 아니면 검정으로 색칠
                    }

                    //장애물 정보를 Map에만 +3씩해서 만듬
                    int overplus = 5;
                    int temp_x, temp_y, temp_width, temp_height;

                    temp_x = targetBlob.BoundingBox.X - overplus;
                    temp_y = targetBlob.BoundingBox.Y - overplus;

                    temp_width  = targetBlob.BoundingBox.Width + overplus * 2;
                    temp_height = targetBlob.BoundingBox.Height + overplus * 2;

                    if (temp_x < 0)
                    {
                        temp_x = 0;
                    }

                    if (temp_y < 0)
                    {
                        temp_y = 0;
                    }

                    if (temp_x + temp_width > globals.rect_width)
                    {
                        temp_width -= (temp_x + temp_width - globals.rect_width);
                    }

                    if (temp_y + temp_height > globals.rect_height)
                    {
                        temp_height -= (temp_y + temp_height - globals.rect_height);
                    }

                    for (int x = temp_x; x < temp_x + temp_width; x++)
                    {
                        for (int y = temp_y; y < temp_y + temp_height; y++)
                        {
                            temp_img[y, x] = new Bgr(255, 255, 255); //검출된 부분은 다 하얀색으로 색칠
                        }
                    }
                    if (color_str == "purple")
                    {
                        color_index = 0;
                    }
                    else if (color_str == "black")
                    {
                        color_index = 1;
                    }
                    //else if (color_str == "yellow")
                    //    color_index = 2;

                    //GUI상 장애물 그림을 줄이기 위해
                    int boundingBox_width, boundingBox_height, boundingBoxX, boundingBoxY;
                    int margin = 0;

                    boundingBoxX = targetBlob.BoundingBox.X + margin;
                    boundingBoxY = targetBlob.BoundingBox.Y + margin;

                    boundingBox_width  = targetBlob.BoundingBox.Width - margin * 2;
                    boundingBox_height = targetBlob.BoundingBox.Height - margin * 2;

                    if (temp_color_count[color_index] == 0) //검출된 색의 color_count가 0 일땐 list에 추가함
                    {
                        building_list.Add(new Building("B" + blob_indenti_count++, (double)boundingBox_width, (double)boundingBox_height, boundingBoxX, boundingBoxY, color_str, true));
                        obstacle_color_count[color_index]++; //obstacle_color_count 증가
                        //Console.WriteLine("blob_indenti_count = " + blob_indenti_count + " color_str = " + color_str + " x  = " + targetBlob.BoundingBox.X + " y = " + targetBlob.BoundingBox.Y);
                    }
                    else if (temp_color_count[color_index] != 0) //color_count가 0이 아니면 list에 있으니 정보 갱신만 함
                    {
                        for (int i = 0; i < tmp_list.Count; i++)
                        {
                            Building remov_tmp = tmp_list[i];

                            if (remov_tmp.BuildingColor == color_str)
                            {
                                tmp_list.Remove(remov_tmp);                                                     //tmp_list에서 하나 삭제
                                temp_color_count[color_index]--;                                                // temp_color_count 하나 감소

                                foreach (Building building in building_list)                                    //building_list의 정보 갱신
                                {
                                    if (building.BuildingColor == color_str && building.DisapperCheck == false) //building.DisapperCheck가 false인 경우 정보 갱신
                                    {
                                        building.X             = targetBlob.BoundingBox.X + margin;
                                        building.Y             = targetBlob.BoundingBox.Y + margin;
                                        building.Width         = targetBlob.BoundingBox.Width - margin * 2;
                                        building.Height        = targetBlob.BoundingBox.Height - margin * 2;
                                        building.DisapperCheck = true; //갱신했으면 building.DisapperChecf를 true로
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }

                    blob_count++;

                    //장애물 vs 차량 충돌 검사
                    for (int i = 0; i < 4; i++)
                    {
                        int leftA, leftB;
                        int rightA, rightB;
                        int topA, topB;
                        int bottomA, bottomB;

                        if (!(tracking_rect[i].Width == 0 && tracking_rect[i].Height == 0))
                        {
                            int add_size = -3;

                            leftA   = tracking_rect[i].X;
                            rightA  = tracking_rect[i].X + tracking_rect[i].Width;
                            topA    = tracking_rect[i].Y;
                            bottomA = tracking_rect[i].Y + tracking_rect[i].Height;

                            /*
                             * leftB = temp_x;
                             * rightB = temp_x + temp_width;
                             * topB = temp_y;
                             * bottomB = temp_y + temp_height;
                             */
                            leftB   = boundingBoxX - add_size;
                            rightB  = boundingBoxX + boundingBox_width + add_size;
                            topB    = boundingBoxY - add_size;
                            bottomB = boundingBoxY + boundingBox_height + add_size;

                            if (bottomA < topB)
                            {
                                continue;                 //아래
                            }
                            if (topA > bottomB)
                            {
                                continue;                 //위
                            }
                            if (rightA < leftB)
                            {
                                continue;                 //오른쪽
                            }
                            if (leftA > rightB)
                            {
                                continue;                 //왼쪽
                            }
                            int boarder_size = 0;

                            if (bottomA - topB <= boarder_size || bottomB - topA <= boarder_size || rightA - leftB <= boarder_size || rightB - leftA <= boarder_size)
                            {
                                Console.WriteLine(i + " 번쨰 차량이 장애물과 충돌함");

                                bool isEmpty = false;

                                if (globals.UGVandObstacleCollisionInofo.Count == 0)
                                {
                                    globals.UGVandObstacleCollisionInofo.Add(i);
                                }
                                else
                                {
                                    foreach (var list in globals.UGVandObstacleCollisionInofo)
                                    {
                                        if (list == i)
                                        {
                                            isEmpty = true;
                                            break;
                                        }
                                    }

                                    if (!isEmpty)
                                    {
                                        globals.UGVandObstacleCollisionInofo.Add(i);
                                    }
                                }
                            }
                        }
                    }
                }
                else //범위를 벗어난 크기는 검정으로 색칠
                {
                    for (int x = targetBlob.BoundingBox.X; x < targetBlob.BoundingBox.X + targetBlob.BoundingBox.Width; x++)
                    {
                        for (int y = targetBlob.BoundingBox.Y; y < targetBlob.BoundingBox.Y + targetBlob.BoundingBox.Height; y++)
                        {
                            temp_img[y, x] = new Bgr(0, 0, 0);
                        }
                    }
                }
            }

            drow_bloded_Grid(Map_obstacle, temp_img); //Map 배열에 장애물 표시

            return(blob_count);
        }
Пример #18
0
 public BodyDetection()
 {
     ThresholdValue = 20;
     BlobDetector   = new CvBlobDetector();
     RBSK           = MouseService.GetStandardMouseRules();
 }
Пример #19
0
        public Mat DoBinary()
        {
            if (!SetImageReady)
            {
                return(null);
            }

            if (!ManualBinary)
            {
                DoAutoBinary();
            }
            else
            {
                DoManualBinary();
            }

            if (FilterMask != 0)
            {
                Mat structElement = CvInvoke.GetStructuringElement(ElementShape.Ellipse,
                                                                   new Size(FilterMask, FilterMask), new Point(-1, -1));

                CvInvoke.MorphologyEx(MatBinary, MatBinary, MorphOp.Close, structElement, new Point(-1, -1), 1,
                                      BorderType.Default, new MCvScalar(0, 0, 0));

                CvInvoke.MorphologyEx(MatBinary, MatBinary, MorphOp.Open, structElement, new Point(-1, -1), 1,
                                      BorderType.Default, new MCvScalar(0, 0, 0));
            }

            CvBlobs        blobs         = new CvBlobs();
            CvBlobDetector _blobDetector = new CvBlobDetector();

            _blobDetector.Detect(MatBinary.ToImage <Gray, byte>(), blobs);

            int maxwidth = 0;
            int width    = 0;

            foreach (CvBlob item in blobs.Values)
            {
                width = item.BoundingBox.Right - item.BoundingBox.Left;
                if (width > maxwidth)
                {
                    maxwidth = width;
                }
            }

            Image <Gray, byte> img1 = MatBinary.ToImage <Gray, byte>();

            img1 = MatBinary.ToImage <Gray, byte>();

            if (!FillWafer)
            {
                //雜訊太多就不補了
                if (blobs.Values.Count < 50)
                {
                    int[] TopSide = new int[3840];

                    for (int x = 0; x < ROI.Width; x++)
                    {
                        for (int y = 0; y < ROI.Height; y++)
                        {
                            if (img1.Data[y, x, 0] == 0xFF)
                            {
                                y = TopSide[x];
                                break;
                            }
                        }
                    }
                    foreach (CvBlob item in blobs.Values)
                    {
                        //Blob過小的時候視為雜訊
                        if (item.BoundingBox.Right - item.BoundingBox.Left < maxwidth / 3)
                        {
                            for (int y = item.BoundingBox.Top; y < item.BoundingBox.Bottom; y++)
                            {
                                for (int x = item.BoundingBox.Left; x < item.BoundingBox.Right; x++)
                                {
                                    if (y > TopSide[x])
                                    {
                                        img1.Data[y, x, 0] = 0x00;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            MatBinary = img1.Mat;

            if (FillWafer)
            {
                Image <Gray, byte> img = MatBinary.ToImage <Gray, byte>();

                for (int x = 0; x < ROI.Width; x++)
                {
                    for (int y = 0; y < ROI.Height; y++)
                    {
                        if (0xFF == img.Data[y, x, 0])
                        {
                            for (int k = y; k < ROI.Height; k++)
                            {
                                img.Data[k, x, 0] = 0xFF;
                            }

                            break;
                        }
                    }
                }

                MatBinary = img.Mat;
            }

            return(MatBinary);
        }
Пример #20
0
        private void b2t_ConversionButton_Click(object sender, EventArgs e)
        {
            b2t_resultBox.Clear();
            if (b2t_openImageBox.Image != null)
            {
                Image <Gray, Byte> gray = openImg.Convert <Gray, Byte>();   //원본을 그레이 이미지로
                CvInvoke.Threshold(gray, gray, 0, 255, ThresholdType.Otsu); //이진화

                CvInvoke.BitwiseNot(gray, gray);                            //흑백 전환


                CvBlobDetector blobDetector = new CvBlobDetector();
                CvBlobs        blobs        = new CvBlobs();
                blobDetector.Detect(gray, blobs);
                List <CvBlob> blobList = new List <CvBlob>();

                for (int i = 0; i < blobs.Count; i++)
                {
                    CvBlob blob = blobs.Values.ElementAt(i);
                    int    j;
                    for (j = 0; j < blobs.Count; j++)
                    {
                        CvBlob otherBlob = blobs.Values.ElementAt(j);
                        if (blob.Label == otherBlob.Label)
                        {
                            continue;
                        }
                        if (otherBlob.BoundingBox.Contains(blob.BoundingBox))
                        {
                            break;
                        }
                    }
                    if (j == blobs.Count)
                    {
                        blobList.Add(blob);
                    }
                }

                int width  = openImg.Width;
                int height = openImg.Height;

                Image <Gray, Byte> hor = new Image <Gray, byte>(new Size(width, height));
                Image <Gray, Byte> ver = new Image <Gray, byte>(new Size(height, height));
                CvInvoke.Blur(gray, gray, new Size(9, 9), new Point(5, 5));

                int ysize = gray.Rows;
                int xsize = gray.Cols;

                byte white = 255;

                for (int x = 0; x < xsize; x++)
                {
                    int z = 0;
                    for (int y = 0; y < ysize; y++)
                    {
                        if (gray.Data[y, x, 0] > 100)
                        {
                            if (z < ysize)
                            {
                                hor.Data[z++, x, 0] = white;
                            }
                        }
                    }
                }

                for (int y = 0; y < ysize; y++)
                {
                    int z = 0;
                    for (int x = 0; x < xsize; x++)
                    {
                        if (gray.Data[y, x, 0] > 200)
                        {
                            if (z < ver.Width)
                            {
                                ver.Data[y, z++, 0] = white;
                            }
                        }
                    }
                }

                Image <Gray, Byte> black = new Image <Gray, byte>(new Size(gray.Width, gray.Height));

                //------------Drawing areas---------------
                //--Vertical
                for (int y = 0; y < ysize; y++)
                {
                    if (ver.Data[y, 0, 0] != white)
                    {
                        for (int i = 0; i < openImg.Cols; i++)
                        {
                            black.Data[y, i, 0] = 255;
                        }
                    }
                }


                //--Horizontal
                for (int x = 0; x < xsize; x++)
                {
                    if (hor.Data[0, x, 0] != white)
                    {
                        for (int i = 0; i < openImg.Rows; i++)
                        {
                            black.Data[i, x, 0] = 255;
                        }
                    }
                }

                CvInvoke.BitwiseNot(black, black);//흑백 전환

                CvBlobDetector blackBlobDetector = new CvBlobDetector();
                CvBlobs        blackBlobs        = new CvBlobs();
                blackBlobDetector.Detect(black, blackBlobs);
                List <CvBlob> blackBlobList = new List <CvBlob>();
                for (int i = 0; i < blackBlobs.Count; i++)
                {
                    CvBlob blackBlob = blackBlobs.Values.ElementAt(i);
                    int    j;
                    for (j = 0; j < blackBlobs.Count; j++)
                    {
                        CvBlob blackOtherBlob = blackBlobs.Values.ElementAt(j);
                        if (blackBlob.Label == blackOtherBlob.Label)
                        {
                            continue;
                        }
                        if (blackOtherBlob.BoundingBox.Contains(blackBlob.BoundingBox))
                        {
                            break;
                        }
                    }
                    if (j == blackBlobs.Count)
                    {
                        blackBlobList.Add(blackBlob);
                    }
                }

                b2t_openImageBox.Image = openImg;

                //각각의 축의 개수를 추출하기 위한 변수
                int brailleBase_Count_X = 1;
                int brailleBase_Count_Y = 1;

                //첫번째 blob을 기준으로 각각의 축의 개수를 추출
                int brailleBase_X = blackBlobList.ElementAt(0).BoundingBox.X;
                int brailleBase_Y = blackBlobList.ElementAt(0).BoundingBox.Y;

                for (int i = 1; i < blackBlobs.Count; i++)
                //x축의 점자 베이스 개수, y축의 점자 베이스 개수를 추출하는 루프
                {
                    int blackX = blackBlobList.ElementAt(i).BoundingBox.X;
                    int blackY = blackBlobList.ElementAt(i).BoundingBox.Y;

                    if (blackX == brailleBase_X)
                    {
                        brailleBase_Count_X++;
                    }
                    if (blackY == brailleBase_Y)
                    {
                        brailleBase_Count_Y++;
                    }
                }


                int[] arr = new int[brailleBase_Count_Y - 1];
                int[] brr = new int[brailleBase_Count_Y - 1];

                for (int i = 0; i < brailleBase_Count_Y - 1; i++)
                {
                    arr[i] = blackBlobList.ElementAt(i + 1).BoundingBox.X - blackBlobList.ElementAt(i).BoundingBox.X;
                    brr[i] = blackBlobList.ElementAt(i + 1).BoundingBox.X - blackBlobList.ElementAt(i).BoundingBox.X;
                }

                for (int i = 0; i < arr.Length; i++)
                {
                    for (int j = 0; j < arr.Length - 1; j++)
                    {
                        if (arr[j + 1] < arr[j])
                        {
                            int temp = arr[j + 1];
                            arr[j + 1] = arr[j];
                            arr[j]     = temp;
                        }
                    }
                }

                int A = arr[1];
                int B = 0; int b;
                int max = arr[arr.Length - 1];
                for (b = 0; b < arr.Length; b++)
                {
                    if (arr[b] > A + 4 && arr[b] < max - 4)
                    {
                        B = arr[b];
                        break;
                    }
                }
                A += 1;
                B += 1;

                Image <Gray, Byte> Cblack = new Image <Gray, byte>(new Size(gray.Width, gray.Height));

                int px = blackBlobList.ElementAt(0).BoundingBox.X;
                int py = blackBlobList.ElementAt(0).BoundingBox.Y;
                int c  = 0;
                while (c < brr.Length && px < Cblack.Width)
                {
                    Cblack.Draw(new Rectangle(new Point(px, py), blackBlobList.ElementAt(0).BoundingBox.Size), new Gray(255), 1);
                    Cblack.Draw(new Rectangle(new Point(px, py + A), blackBlobList.ElementAt(0).BoundingBox.Size), new Gray(255), 1);
                    Cblack.Draw(new Rectangle(new Point(px, py + A * 2), blackBlobList.ElementAt(0).BoundingBox.Size), new Gray(255), 1);

                    px += A;
                    Cblack.Draw(new Rectangle(new Point(px, py), blackBlobList.ElementAt(0).BoundingBox.Size), new Gray(255), 1);
                    Cblack.Draw(new Rectangle(new Point(px, py + A), blackBlobList.ElementAt(0).BoundingBox.Size), new Gray(255), 1);
                    Cblack.Draw(new Rectangle(new Point(px, py + A * 2), blackBlobList.ElementAt(0).BoundingBox.Size), new Gray(255), 1);

                    c++;

                    if (c < brr.Length && brr[c] > B - 4 && brr[c] < B + 4)
                    {
                        px += B;
                        c++;
                    }

                    else if (c < brr.Length && brr[c] > (int)(A * 2.5) - 4 && brr[c] < (int)(A * 2.5) + 4)
                    {
                        px += B;
                    }

                    else if (c < brr.Length && brr[c] > (int)(A * 4.2) - 4 && brr[c] < (int)(A * 4.2) + 4)
                    {
                        px += (int)(A * 3.0);
                        px += (int)(A / 2);
                    }

                    else
                    {
                        px += (int)(A * 3.3);
                        c++;
                    }
                }

                CvBlobDetector CblackBlobDetector = new CvBlobDetector();
                CvBlobs        CblackBlobs        = new CvBlobs();
                CblackBlobDetector.Detect(Cblack, CblackBlobs);
                List <CvBlob> CblackBlobList = new List <CvBlob>();
                for (int i = 0; i < CblackBlobs.Count; i++)
                {
                    CvBlob CblackBlob = CblackBlobs.Values.ElementAt(i);
                    int    j;
                    for (j = 0; j < CblackBlobs.Count; j++)
                    {
                        CvBlob CblackOtherBlob = CblackBlobs.Values.ElementAt(j);
                        if (CblackBlob.Label == CblackOtherBlob.Label)
                        {
                            continue;
                        }
                        if (CblackOtherBlob.BoundingBox.Contains(CblackBlob.BoundingBox))
                        {
                            break;
                        }
                    }
                    if (j == CblackBlobs.Count)
                    {
                        CblackBlobList.Add(CblackBlob);
                    }
                }

                /////////////////////////////////////////////////////////////////
                //각각의 축의 개수를 추출하기 위한 변수
                int CbrailleBase_Count_X = 1;
                int CbrailleBase_Count_Y = 1;

                //첫번째 blob을 기준으로 각각의 축의 개수를 추출
                int CbrailleBase_X = CblackBlobList.ElementAt(0).BoundingBox.X;
                int CbrailleBase_Y = CblackBlobList.ElementAt(0).BoundingBox.Y;

                for (int i = 1; i < CblackBlobList.Count; i++)//x축의 점자 베이스 개수, y축의 점자 베이스 개수를 추출하는 루프
                {
                    int CblackX = CblackBlobList.ElementAt(i).BoundingBox.X;
                    int CblackY = CblackBlobList.ElementAt(i).BoundingBox.Y;

                    if (CblackX == CbrailleBase_X)
                    {
                        CbrailleBase_Count_X++;
                    }
                    if (CblackY == CbrailleBase_Y)
                    {
                        CbrailleBase_Count_Y++;
                    }
                }

                int[,] CbrailleArray = new int[CbrailleBase_Count_Y, CbrailleBase_Count_X]; //점자를 위한 배열

                for (int j = 0; j < CbrailleBase_Count_X; j++)                              //배열 초기화
                {
                    for (int i = 0; i < CbrailleBase_Count_Y; i++)
                    {
                        CbrailleArray[i, j] = 0;//초기값은 0으로
                    }
                }


                for (int j = 0; j < CbrailleBase_Count_X; j++)
                {
                    for (int i = 0; i < CbrailleBase_Count_Y; i++)
                    {
                        int cnt = (CbrailleBase_Count_Y * j) + i;                                                                   //점자 베이스의 위치를 위한 변수

                        int bx    = CblackBlobList.ElementAt(cnt).BoundingBox.X;                                                    //점자 베이스 시작 x위치
                        int by    = CblackBlobList.ElementAt(cnt).BoundingBox.Y;                                                    //점자 베이스 시작 y위치
                        int bendX = CblackBlobList.ElementAt(cnt).BoundingBox.X + CblackBlobList.ElementAt(cnt).BoundingBox.Width;  //점자 베이스 끝 x위치
                        int bendY = CblackBlobList.ElementAt(cnt).BoundingBox.Y + CblackBlobList.ElementAt(cnt).BoundingBox.Height; //점자 베이스 끝 Y위치

                        for (int k = 0; k < blobList.Count; k++)
                        {
                            int x    = blobList.ElementAt(k).BoundingBox.X;                                            //점자 시작 x위치
                            int y    = blobList.ElementAt(k).BoundingBox.Y;                                            //점자 시작 y위치
                            int endX = blobList.ElementAt(k).BoundingBox.X + blobList.ElementAt(k).BoundingBox.Width;  //점자 끝 x위치
                            int endY = blobList.ElementAt(k).BoundingBox.Y + blobList.ElementAt(k).BoundingBox.Height; //점자 끝 Y위치


                            if (x < bendX && endX > bx && y < bendY && endY > by) //사각형이 겹치는지 체크
                            {
                                CbrailleArray[i, j] = 1;                          //겹치면 점자가 있는 것으로 간주하고 1을 넣는다.
                                break;
                            }
                        }
                    }
                }

                //---------------------------------------------------------
                double code = 0;
                int    cnti = 1;
                int    zed  = 1;
                for (int j = 0; j < CbrailleBase_Count_Y; j++)
                {
                    for (int i = 0; i < 3 * zed && i < CbrailleBase_Count_X; i++)
                    {
                        if (cnti > 6)
                        {
                            exceptionCode((int)code);
                            code = 0;
                            cnti = 1;
                        }
                        if (CbrailleArray[j, i] == 1)
                        {
                            code += Math.Pow(2, cnti);
                        }
                        cnti++;
                    }
                }

                exceptionCode((int)code);

                ArrayList cho  = new ArrayList(new char[] { 'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' });
                ArrayList jung = new ArrayList(new char[] { 'ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ' });
                ArrayList jong = new ArrayList(new char[] { ' ', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' });

                int c1 = -1;
                int c2 = -1;
                int c3 = -1;

                string hanString = b2t_resultBox.Text;
                char[] hanChar   = hanString.ToCharArray();

                b2t_resultBox.Clear();

                int c4 = -1;

                for (int i = 0; i < hanChar.Length; i++)
                {
                    if (cho.Contains(hanChar[i]))                                    //자음일때 초성 종성
                    {
                        if (i + 1 < hanChar.Length && jung.Contains(hanChar[i + 1])) //자음 다음이 모음일때   초성
                        {
                            if (i < hanChar.Length && jung.Contains(hanChar[i + 1]) && c1 != -1)
                            {
                                c3 = 0;
                                c4 = 44032 + c1 + c2 + c3;
                                b2t_resultBox.AppendText(((char)c4).ToString());

                                c1 = -1;
                                c2 = -1;
                                c3 = -1;
                            }

                            //초성+중성
                            c1  = cho.IndexOf(hanChar[i]);
                            c2  = jung.IndexOf(hanChar[i + 1]);
                            c1 *= 588;
                            c2 *= 28;

                            //중성(모음)을 추가했으니 다음 배열 인덱스로~
                            i++;
                            if (i + 2 >= hanChar.Length)
                            {
                                if (cho.Contains(hanChar[hanChar.Length - 1]))  //끝에 자음일 경우 종성이기에
                                {
                                    c3 = jong.IndexOf(hanChar[hanChar.Length - 1]);
                                }
                                else//아니면 종성없음
                                {
                                    c3 = 0;
                                }
                                c4 = 44032 + c1 + c2 + c3;
                                b2t_resultBox.AppendText(((char)c4).ToString());
                            }
                        }
                        else if (i + 1 < hanChar.Length && cho.Contains(hanChar[i + 1]))                                //자음 다음 자음일 때   종성
                        {
                            if (i + 3 < hanChar.Length && cho.Contains(hanChar[i + 2]) && cho.Contains(hanChar[i + 3])) //종성뒤에 자음이 있을경우 두 자음 모두 받침
                            {
                                /////없음
                            }
                            else//없을 때 그냥
                            {
                                c3 = jong.IndexOf(hanChar[i]);
                                c4 = 44032 + c1 + c2 + c3;
                                b2t_resultBox.AppendText(((char)c4).ToString());

                                c1 = -1;
                                c2 = -1;
                                c3 = -1;
                            }
                        }
                    }
                    else if (i < hanChar.Length && jung.Contains(hanChar[i]))     //모음일때  중성
                    {
                        //앞에 ㅇ을 붙인다.
                        c1  = cho.IndexOf('ㅇ');
                        c2  = jung.IndexOf(hanChar[i]);
                        c1 *= 588;
                        c2 *= 28;
                        if (i + 1 == hanChar.Length)
                        {
                            c3 = 0;
                            c4 = 44032 + c1 + c2 + c3;
                            b2t_resultBox.AppendText(((char)c4).ToString());

                            c1 = -1;
                            c2 = -1;
                            c3 = -1;
                        }
                    }
                }
            }
        }