Пример #1
0
        private void skeletonizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Emgu.CV.Image <Emgu.CV.Structure.Gray, byte> img1 = new Image <Emgu.CV.Structure.Gray, byte>(img.ToBitmap()).Resize(500, 500, true);
            Emgu.CV.Image <Gray, byte> gray   = img1.Convert <Gray, byte>();                       //convert to grayscale
            Emgu.CV.Image <Gray, byte> binary = gray.ThresholdBinary(new Gray(75), new Gray(255)); //perform binarization
            IntPtr dsti = Emgu.CV.CvInvoke.cvCreateImage(Emgu.CV.CvInvoke.cvGetSize(binary), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_32F, 1);

            Emgu.CV.CvInvoke.cvDistTransform(binary, dsti, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_L2, 5, null, IntPtr.Zero);
            CvInvoke.cvLaplace(dsti, dsti, 7);
            CvInvoke.cvThreshold(dsti, dsti, 3, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
            Image <Gray, byte> fu = new Image <Gray, byte>(binary.Width, binary.Height);

            CvInvoke.cvConvertScaleAbs(binary.Ptr, fu.Ptr, 2, 1.5);
            CvInvoke.cvNot(fu.Ptr, fu.Ptr);
            fu._Dilate(1);
            fu._Erode(1);
            showimage(fu);
        }
        public int apply(string fileName, string output)
        {
            int counter = 0;

            Emgu.CV.Image<Bgr, Byte> imgS = new Emgu.CV.Image<Bgr, Byte>(fileName);

            Emgu.CV.Image<Gray, Byte> img = new Emgu.CV.Image<Gray, Byte>(fileName);

            //Emgu.CV.Image<Gray, Byte> imgGray = new Image<Gray, byte>(img.Width, img.Height);
            //CvInvoke.cvCvtColor(img, imgGray, COLOR_CONVERSION.BGR2GRAY);

            int thresh = 1;
            int max_thresh = 255;
            img = img.ThresholdBinary(new Gray(thresh), new Gray(max_thresh));

            img.Save(output.Replace(".", "_binary."));

            Contour<Point> contur = img.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP);
            Emgu.CV.CvInvoke.cvDrawContours(imgS, contur, new MCvScalar(0, 0, 255), new MCvScalar(0, 0, 255), 1, 1, LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));

            contur = img.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP);

            while (contur != null && contur.HNext != null)
            {
                if (counter == 0) { counter++; }

                contur = contur.HNext;
                counter++;
            }

            MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SIMPLEX, 0.8f, 0.8f);
            MCvScalar color = new MCvScalar(255, 255, 255);

            CvInvoke.cvPutText(imgS, "counter:" + counter, new Point(10, 20), ref font, color);

            imgS.Save(output);

            return counter;
        }