Esempio n. 1
0
        private void find_max_area(Mat src_gray, ref OpenCvSharp.Point center, ref Mat dst_color)//カラーで返す
        {
            src_gray.Threshold(0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);
            Mat[] contours;
            Mat   hie = new Mat();

            Cv2.FindContours(src_gray, out contours, hie, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
            if (contours.Length > 0)
            {
                double max_size  = 0;
                int    max_index = 0;

                for (int i = 0; i < contours.Length; i++)
                {
                    double size = Cv2.ContourArea(contours[i]);
                    if (max_size < size)
                    {
                        max_size  = size;
                        max_index = i;
                    }
                }
                Cv2.DrawContours(dst_color, contours, max_index, new Scalar(255, 255, 255), -1);
                RotatedRect box = Cv2.MinAreaRect(contours[max_index]);
                center = box.Center;
            }

            contours = null;
            hie.Dispose();
            src_gray.Dispose();
        }
        private void btnCentro_Click(object sender, EventArgs e)
        {
            Emgu.CV.Util.VectorOfVectorOfPoint contours = new Emgu.CV.Util.VectorOfVectorOfPoint();
            Mat mat = new Mat();

            CvInvoke.FindContours(imgout, contours, mat, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);

            for (int i = 0; i < contours.Size; i++)
            {
                var area = CvInvoke.ContourArea(contours[i]);
                if (area > (int)numericSenCount.Value)
                {
                    Rectangle   rect     = CvInvoke.BoundingRectangle(contours[i]);
                    RotatedRect box      = CvInvoke.MinAreaRect(contours[i]);
                    PointF[]    Vertices = box.GetVertices();
                    PointF      point    = box.Center;
                    papel       = pictureBox1.CreateGraphics();
                    pluma.Width = 5;
                    pluma.Color = Color.DarkBlue;
                    papel.DrawRectangle(pluma, point.X, point.Y, 5, 5);
                }
            }

            /*PointF edge1 = new PointF(Vertices[1].X - Vertices[0].X,
             * Vertices[1].Y - Vertices[0].Y);
             * PointF edge2 = new PointF(Vertices[2].X - Vertices[1].X, Vertices[2].Y - Vertices[1].Y);
             * double edge1Magnitude = Math.Sqrt(Math.Pow(edge1.X, 2) + Math.Pow(edge1.Y, 2));
             * double edge2Magnitude = Math.Sqrt(Math.Pow(edge2.X, 2) + Math.Pow(edge2.Y, 2));
             * PointF primaryEdge = edge1Magnitude > edge2Magnitude ? edge1 : edge2;
             * PointF reference = new PointF(Vertices[1].X, Vertices[0].Y);
             * double thetaRads = Math.Acos(Math.Sqrt(Math.Pow((primaryEdge.X * reference.X),2) + Math.Pow((primaryEdge.Y * reference.Y),2))/ (Math.Sqrt(Math.Pow(primaryEdge.X,2)+Math.Pow(reference.X,2))* Math.Sqrt(Math.Pow(primaryEdge.Y, 2) + Math.Pow(reference.Y, 2))));
             * double thetaDeg = thetaRads * 180 / Math.PI;
             * MessageBox.Show(thetaDeg.ToString());*/
        }
Esempio n. 3
0
        /// <summary>
        /// 矩形顶点编号
        /// </summary>
        /// <param name="pointfs"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        public static PointF[] RectCode(RotatedRect rect)
        {
            PointF[] p       = rect.GetVertices();
            PointF[] pointfs = new PointF[4];
            pointfs[0] = p[0];
            pointfs[1] = p[0];
            pointfs[2] = p[0];
            pointfs[3] = p[0];
            //逆时针编号
            for (int i = 1; i < 4; i++)
            {
                if (p[i].X < p[i - 1].X)
                {
                    pointfs[0] = p[i];
                }
                if (p[i].Y > p[i - 1].Y)
                {
                    pointfs[1] = p[i];
                }
                if (p[i].X > p[i - 1].X)
                {
                    pointfs[2] = p[i];
                }
                if (p[i].Y < p[i - 1].Y)
                {
                    pointfs[3] = p[i];
                }
            }

            return(pointfs);
        }
        /// <summary>
        /// 角度纠正
        /// </summary>
        /// <param name="Image原图"></param>
        /// <returns></returns>
        public Mat The_Angle_correct(Mat Image原图)
        {
            Mat Image旋转纠正后原图 = new Mat();

            using (Mat Image灰度 = new Mat())
                using (Mat Image自适应阈值化 = new Mat())
                //using (Mat Image旋转纠正后原图 = new Mat())
                {
                    Cv2.CvtColor(Image原图, Image灰度, ColorConversionCodes.BGR2GRAY);
                    Cv2.AdaptiveThreshold(~Image灰度, Image自适应阈值化, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 15, -2);
                    //查找轮廓
                    OpenCvSharp.Point[][] contours查找的轮廓 = Find_the_outline(Image自适应阈值化);

                    for (int i = 0; i < contours查找的轮廓.Length; i++)
                    {
                        RotatedRect rotaterect倾斜 = Cv2.MinAreaRect(contours查找的轮廓[i]);
                        if (rotaterect倾斜.Size.Height * rotaterect倾斜.Size.Width > 1000000)
                        {
                            Rect  rect轮廓 = Cv2.BoundingRect(contours查找的轮廓[i]);
                            float angle;
                            Image旋转纠正后原图.SetTo(255);
                            Point2f center = rotaterect倾斜.Center; //中心点
                            if (rotaterect倾斜.Size.Height > rotaterect倾斜.Size.Width)
                            {
                                angle = rotaterect倾斜.Angle;
                            }
                            else
                            {
                                angle = 90.0f + rotaterect倾斜.Angle;

                                //Point2f center =(Point2f)90.0- rotaterect倾斜.Center;
                            }
                            using (Mat M2 = Cv2.GetRotationMatrix2D(center, angle, 1))
                            {
                                //Cv2.WarpAffine(Image直接阈值化, Image旋转后灰度图, M2, Image直接阈值化.Size(), InterpolationFlags.Linear, 0, new Scalar(0));//仿射变换
                                Cv2.WarpAffine(Image原图, Image旋转纠正后原图, M2, Image自适应阈值化.Size(), InterpolationFlags.Linear, 0, new Scalar(0));
                                //仿射变换
                                //Image原图.Rectangle(rect轮廓, Scalar.Red);
                                //Image旋转后原图.Rectangle(rect轮廓, Scalar.Gray);
                                //var window旋转后灰度图 = new Window("旋转后灰度图");
                                //window旋转后灰度图.Image = Image旋转后灰度图;
                                //var window旋转后原图 = new Window("旋转后原图");
                                //window旋转后原图.Image = Image旋转后原图;
                            }
                        }
                    }

                    return(Image旋转纠正后原图);
                    // Bottom-Right


                    //using (new Window("Image原图", WindowMode.AutoSize, Image原图))
                    //using (new Window("Image灰度", WindowMode.AutoSize, Image灰度))
                    //using (new Window("Image自适应阈值化", WindowMode.AutoSize, Image自适应阈值化))
                    //using (new Window("Image旋转后原图", WindowMode.AutoSize, Image旋转后原图))
                    //{
                    //    Window.WaitKey(0);
                    //}
                }
        }
Esempio n. 5
0
 private void GetOriginImage()
 {
     try
     {
         using (Mat img = CvInvoke.Imread(@"img\origin.bmp", Emgu.CV.CvEnum.ImreadModes.Grayscale))
         {
             Image <Gray, byte> _img = img.ToImage <Gray, byte>();
             _img = ComputerVison.RoiImage(_img, Config.Parameter.ROI);
             VectorOfPoint cnt = new VectorOfPoint();
             cnt = ComputerVison.FindContours(_img, Config.Parameter.THRESHOLD_VALUE);
             RotatedRect a = CvInvoke.MinAreaRect(cnt);
             ORGRec         = a.MinAreaRect();
             ORGRec.X      += Config.Parameter.ROI.X - 10;
             ORGRec.Y      += Config.Parameter.ROI.Y - 10;
             ORGRec.Height += 20;
             ORGRec.Width  += 20;
             using (Image <Bgr, byte> iBgr2 = _img.Convert <Bgr, byte>())
             {
                 CvInvoke.Rectangle(iBgr2, ORGRec, new MCvScalar(0, 255, 0), 3);
                 CvInvoke.Imwrite("img\\originRoi.bmp", iBgr2);
             }
             Point[] p = ComputerVison.Search2Tip(cnt);
             ORG.SetPointA(p[0]);
             ORG.SetPointB(p[1]);
             cnt.Dispose();
             _img.Dispose();
         }
     }
     catch (Exception er)
     {
         MessageBox.Show(er.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Esempio n. 6
0
        private void getOriginImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            p_imShow.Image = null;
            UpdateParameter();
            bool response = OpenCamera();

            if (response == true)
            {
                using (Image <Bgr, byte> iBgr = GetImage())
                {
                    p_imShow.Image = iBgr.ToBitmap();
                    DialogResult kq = MessageBox.Show("You Sure create new origin image?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (kq == DialogResult.Yes)
                    {
                        CvInvoke.Imwrite(@"img\origin.bmp", iBgr);
                        Image <Gray, byte> iGray = ComputerVison.RoiImage(iBgr.Convert <Gray, byte>(), Config.Parameter.ROI);

                        using (VectorOfPoint cnt = ComputerVison.FindContours(iGray, Config.Parameter.THRESHOLD_VALUE))
                        {
                            RotatedRect a = CvInvoke.MinAreaRect(cnt);
                            ORGRec = a.MinAreaRect();
                            using (Image <Bgr, byte> iBgr2 = iGray.Convert <Bgr, byte>())
                            {
                                CvInvoke.Rectangle(iBgr2, ORGRec, new MCvScalar(0, 255, 0), 3);
                                CvInvoke.Imwrite("img\\originRoi.bmp", iBgr2);
                            }
                        }
                        iGray.Dispose();
                    }
                }
                Camera.CloseDevice();
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Draw All the Contours On An Image
        /// </summary>
        internal void FireImageEvent(object sender, Guid cardId, Guid imageId, ImageType imageType, double angle, float X, float Y,
                                     VectorOfVectorOfPoint contours, CannyParam cannyParameter = null, Func <Size, Rectangle, bool> contourFilter = null)
        {
            if (ImageEvent != null)
            {
                using (Mat rotatedImage = GetRotatedImage(angle))
                {
                    for (int i = 0; i < contours.Size; i++)
                    {
                        RotatedRect rotatedRect = CvInvoke.MinAreaRect(contours[i]);
                        Rectangle   box         = rotatedRect.MinAreaRect();

                        if (contourFilter == null || (contourFilter != null && contourFilter(rotatedImage.Size, box)))
                        {
                            CvInvoke.DrawContours(rotatedImage, contours, i, yellow);
                            CvInvoke.Rectangle(rotatedImage, box, green, thickness: 2);

                            //if (tagged)
                            //{
                            //    CvInvoke.PutText(rotatedImage, string.Format("HR: {0:0.00} AR: {0:0.00}", heightRatio, aspectRatio),
                            //        new Point((int)box.X + (box.Width / 2), (int)box.Y + (box.Height / 2)),
                            //        FontFace.HersheyPlain, fontScale: 1, color: black, thickness: 2);
                            //}
                        }
                    }

                    Image.FireImageEvent(null, cardId, imageId, imageType, rotatedImage, angle, X, Y, cannyParameter: cannyParameter);
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Draws the bound rotated rectangle.
        /// </summary>
        /// <param name="rect">The rotated rectangle.</param>
        /// <param name="annotatedImage">The image with bound rectangles.</param>
        /// <param name="data">The raw data for rectangles.</param>
        private void SetRect(RotatedRect rect, ref Image <Bgr, byte> annotatedImage, ref List <object> data)
        {
            var vertices = rect.GetVertices().Select(Point.Round).ToArray();

            annotatedImage.DrawPolyline(vertices, true, new Bgr(_annoColor.Color()), _lineThick);
            data.Add(new RotatedBox(rect));
        }
Esempio n. 9
0
        public static Mat vector_RotatedRect_to_Mat(List <RotatedRect> rs)
        {
            Mat res;
            int count = (rs != null) ? rs.Count : 0;

            if (count > 0)
            {
                res = new Mat(count, 1, CvType.CV_32FC(5));
                float[] buff = new float[5 * count];
                for (int i = 0; i < count; i++)
                {
                    RotatedRect r = rs[i];
                    buff[5 * i]     = (float)r.center.x;
                    buff[5 * i + 1] = (float)r.center.y;
                    buff[5 * i + 2] = (float)r.size.width;
                    buff[5 * i + 3] = (float)r.size.height;
                    buff[5 * i + 4] = (float)r.angle;
                }
                res.put(0, 0, buff);
            }
            else
            {
                res = new Mat();
            }
            return(res);
        }
Esempio n. 10
0
        private void rotateRectRotation(RotatedRect rr, ref Image <Gray, Byte> img)
        {
            Mat rotateM = new Mat();

            CvInvoke.GetRotationMatrix2D(rr.Center, 90 + rr.Angle, 1, rotateM);
            CvInvoke.WarpAffine(img, img, rotateM, img.Size);
        }
Esempio n. 11
0
        private (byte[] ProcessedPixels, System.Drawing.PointF[] centroids, System.Drawing.Point[]) ProceessImage(Image <Gray, byte> FilteredImage)
        {
            UMat FrameCannyImage = new UMat();

            CvInvoke.Canny(FilteredImage, FrameCannyImage, 100, 200);
            VectorOfVectorOfPoint FrameImageContours = new VectorOfVectorOfPoint();

            CvInvoke.FindContours(FrameCannyImage, FrameImageContours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);
            VectorOfPoint      FrameAppContour = new VectorOfPoint(2);
            Image <Gray, byte> image           = new Image <Gray, byte>(512, 424);

            for (int k = 0; k < FrameImageContours.Size; k++)
            {
                VectorOfPoint contour = FrameImageContours[k];


                if (CvInvoke.ContourArea(contour) > CvInvoke.ContourArea(FrameAppContour))
                {
                    FrameAppContour = contour;
                }
            }

            RotatedRect FrameRotatedRect = CvInvoke.MinAreaRect(FrameAppContour);

            System.Drawing.PointF[] FrameInitCenters = FrameRotatedRect.GetVertices();

            System.Drawing.Point[] contours = FrameAppContour.ToArray();
            byte[] NonZeroPixels            = FilteredImage.Bytes;


            return(NonZeroPixels, FrameInitCenters, FrameAppContour.ToArray());
        }
Esempio n. 12
0
    private VectorOfVectorOfPoint FilterContours(Mat _webcamFrame)
    {
        VectorOfVectorOfPoint vectListMarkers = new VectorOfVectorOfPoint();

        for (int i = 0; i < contours.Size; i++)
        {
            double contourArea           = CvInvoke.ContourArea(contours[i], false);
            double contourAreaProportion = contourArea / (_webcamFrame.Height * _webcamFrame.Width);

            if (contourAreaProportion < _minContourAreaProportion || contourAreaProportion > _maxContourAreaProportion)
            {
                continue;
            }

            RotatedRect rect = CvInvoke.MinAreaRect(contours[i]);
            double      squareShapeComparison = rect.Size.Width / rect.Size.Height;
            if (squareShapeComparison < (1.0 - _squareShapeTolerance) || squareShapeComparison > (1.0 + _squareShapeTolerance))
            {
                continue;
            }

            double areaComparison = contourArea / (rect.Size.Width * rect.Size.Height);
            if (areaComparison < (1.0 - _areaRectTolerance) || areaComparison > (1.0 + _areaRectTolerance))
            {
                continue;
            }


            vectListMarkers.Push(contours[i]);
        }

        return(vectListMarkers);
    }
Esempio n. 13
0
        private Rectangle GetRectangleFromContour(VectorOfPoint approxContour, LineSegment2D[] edgesList)
        {
            if (approxContour.Size > 2)
            {
                bool isRectangle = true;

                for (int j = 0; j < edgesList.Length; j++)
                {
                    double angle = Math.Abs(edgesList[(j + 1) % edgesList.Length]
                                            .GetExteriorAngleDegree(edgesList[j]));

                    if (angle < 85 || angle > 95)
                    {
                        isRectangle = false;
                        break;
                    }
                }

                if (isRectangle)
                {
                    RotatedRect currentRectangle = CvInvoke.MinAreaRect(approxContour);
                    Rectangle   minRectangle     = currentRectangle.MinAreaRect();
                    return(minRectangle);
                }
            }

            return(default(Rectangle));
        }
Esempio n. 14
0
    void Update()
    {
        Mat imgBGRMat;

        imgBGRMat = webcam.QueryFrame();

        if (imgBGRMat == null) // if frame is not ready
        {
            return;
        }

        CvInvoke.Flip(imgBGRMat, imgBGRMat, FlipType.Horizontal);
        CvInvoke.Resize(imgBGRMat, imgBGRMat, new Size(webcam_Capture_WIDTH, webcam_Capture_HEIGHT));

        Mat thresoldOUTFilter = new Mat();
        Mat imgOUTMat         = new Mat();

        CvInvoke.CvtColor(imgBGRMat, imgOUTMat, ColorConversion.Bgr2Hsv);
        Image <Hsv, byte> imgOUTBin = imgOUTMat.ToImage <Hsv, byte>();

        thresoldOUTFilter = imgOUTBin.InRange(new Hsv(minValueH, minValueS, minValueV), new Hsv(maxValueH, maxValueS, maxValueV)).Mat;

        int operationSize = 1;

        Mat structuringElement = CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(2 * operationSize + 1, 2 * operationSize + 1), new Point(operationSize, operationSize));

        CvInvoke.Erode(thresoldOUTFilter, thresoldOUTFilter, structuringElement, new Point(-1, -1), 1, BorderType.Constant, new MCvScalar(0)); // Erode -> Dilate <=> Opening
        CvInvoke.Dilate(thresoldOUTFilter, thresoldOUTFilter, structuringElement, new Point(-1, -1), 1, BorderType.Constant, new MCvScalar(0));

        VectorOfVectorOfPoint contours       = new VectorOfVectorOfPoint();
        VectorOfPoint         biggestContour = new VectorOfPoint();
        int    biggestContourIndex           = -1;
        double biggestContourArea            = 0;

        Mat hierarchy = new Mat();

        CvInvoke.FindContours(thresoldOUTFilter, contours, hierarchy, RetrType.List, ChainApproxMethod.ChainApproxNone); // Find Contour using Binary filter
        for (int i = 0; i < contours.Size; i++)
        {
            double a = CvInvoke.ContourArea(contours[i], false);
            if (a > biggestContourArea)
            {
                biggestContourArea  = a;
                biggestContourIndex = i;
                biggestContour      = contours[i];
            }
        }

        if (biggestContourIndex != -1 && biggestContour.Size > 0)
        {
            // Determine Bounding Rectangle and setting its related values
            RotatedRect boundRec       = CvInvoke.MinAreaRect(biggestContour);
            PointF[]    boundRecPoints = boundRec.GetVertices();

            // Draw Bounding Rectangle
            DrawPointsFRectangle(boundRecPoints, imgBGRMat);
        }

        texture = Utils.ConvertMatToTex2D(imgBGRMat, texture, webcam_Capture_WIDTH, webcam_Capture_HEIGHT);
    }
Esempio n. 15
0
        public static RotatedRect[] detectBarcodes(Bitmap inputImage)
        {
            RotatedRect[]      toReturn  = new RotatedRect[2];
            Image <Gray, Byte> grayImage = new Image <Bgr, byte>(inputImage).Convert <Gray, Byte>();

            CvInvoke.Threshold(grayImage, grayImage, 120, 1000, Emgu.CV.CvEnum.ThresholdType.BinaryInv);


            CvInvoke.Blur(grayImage, grayImage, new Size(27, 9), new Point(-1, -1));
            Mat kernel = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(21, 7), new Point(-1, -1));

            CvInvoke.MorphologyEx(grayImage, grayImage, Emgu.CV.CvEnum.MorphOp.Close, kernel, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Default, new MCvScalar(1));
            CvInvoke.Erode(grayImage, grayImage, null, new Point(-1, -1), 40, Emgu.CV.CvEnum.BorderType.Default, new MCvScalar(1));
            CvInvoke.Dilate(grayImage, grayImage, null, new Point(-1, -1), 40, Emgu.CV.CvEnum.BorderType.Default, new MCvScalar(1));
            grayImage.ToBitmap().Save("backscatter.png");
            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

            CvInvoke.FindContours(grayImage, contours, null, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
            Graphics boxGraphics = Graphics.FromImage(inputImage);

            toReturn[0] = CvInvoke.MinAreaRect(contours[0]);
            toReturn[1] = CvInvoke.MinAreaRect(contours[1]);

            Debug.WriteLine("Barcode Size: " + contours.Size);
            return(toReturn);
        }
Esempio n. 16
0
        public List <RotatedRect> GetBarcode(Mat src)
        {
            Mat imgthresh = BarcodeRegion(src);

            Point[][]        contours;
            HierarchyIndex[] hierarchy;
            Cv2.FindContours(imgthresh, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone, new Point(0, 0));

            //Cv2.ImShow("X", imgthresh);
            //Cv2.WaitKey();

            List <RotatedRect> barcode = new List <RotatedRect>();

            for (int i = 0; i < contours.Length; i++)
            {
                double      area  = Cv2.ContourArea(contours[i]);
                RotatedRect rect  = Cv2.MinAreaRect(contours[i]);
                double      ratio = area / (rect.Size.Width * rect.Size.Height);
                if (1 - ratio < 0.2 && area > 200)
                {
                    Cv2.DrawContours(src, contours, i, 255, -1);
                    barcode.Add(rect);
                }
            }

            //RotatedRect p;
            barcode = barcode.OrderBy(p => p.Center.X).ThenBy(p => p.Center.Y).ToList();

            Cv2.ImShow("X", src);
            Cv2.WaitKey();
            return(barcode);
        }
Esempio n. 17
0
    public void update()
    {
        // convert the local point to world coordinates
        pointsOnWorld = new List <Vector3> ();
        foreach (var pt in pointsOnLocal)
        {
            pointsOnWorld.Add(targetBehaviour.transform.TransformPoint(pt));
        }

        // project the world coordinates to screen coords (pixels)
        pointsOnScreen = new List <Vector3> ();
        foreach (var pt in pointsOnWorld)
        {
            pointsOnScreen.Add(Camera.main.WorldToScreenPoint(pt));
        }

        // 皿の上面のスクリーン座標を計算. y座標が逆向き.
        pointsOnCvScreen = new List <Point> ();
        foreach (var pt in pointsOnScreen)
        {
            pointsOnCvScreen.Add(new Point(pt.x, this.screenHeight - pt.y));
        }

        ellipseRotatedRect = Imgproc.fitEllipse(new MatOfPoint2f(pointsOnCvScreen.ToArray()));
    }
Esempio n. 18
0
        public Form1()
        {
            InitializeComponent();
            //Y


            trackBar4.Maximum = 255;
            trackBar4.Minimum = 0;
            trackBar1.Maximum = 255;
            trackBar1.Minimum = 0;

            //Cb
            trackBar2.Maximum = 255;
            trackBar2.Minimum = 0;
            trackBar5.Maximum = 255;
            trackBar5.Minimum = 0;
            //Cr
            trackBar3.Maximum = 255;
            trackBar3.Minimum = 0;
            trackBar6.Maximum = 255;
            trackBar6.Minimum = 0;


            //detector = new AdaptiveSkinDetector(1, AdaptiveSkinDetector.MorphingMethod.NONE);
            hsv_min   = new Hsv(0, 45, 0);
            hsv_max   = new Hsv(20, 255, 255);
            YCrCb_min = new Ycc(80, 133, 80);
            YCrCb_max = new Ycc(255, 173, 158);
            box       = new RotatedRect();

            DrawBox.MouseClick += new MouseEventHandler(Form1_MouseClick);
        }
Esempio n. 19
0
    private void RunCamShift()
    {
        //Debug.Log("qui");
        //Imgproc.rectangle(rgbMat, roiRect.tl(), roiRect.br(), new Scalar(255, 255, 255, 255), 2);
        using (Mat backProj = new Mat())
        {
            Imgproc.calcBackProject(new List <Mat>(new Mat[] { hsvMat }), new MatOfInt(1), roiHistMat, backProj, new MatOfFloat(100, 255), 1.0);


            SaveMatToFile("backProjBefore" + ss, backProj);



            Mat kernelD = new Mat(10, 10, CvType.CV_8UC1, new Scalar(255, 255, 255));
            Imgproc.dilate(backProj, backProj, kernelD);
            Mat new_back_proj = new Mat(frame.rows(), frame.cols(), CvType.CV_8UC1, new Scalar(255, 255, 255)) - backProj;
            //hs();


            RotatedRect r = Video.CamShift(backProj, roiRect, termination);
            SaveMatToFile("backProjAfter" + ss, new_back_proj);
            //SaveMatToFile("hsvMat" + ss, hsvMat);
        }
        Imgproc.rectangle(rgbMat, roiRect.tl(), roiRect.br(), new Scalar(0, 255, 0, 255), 2);
    }
Esempio n. 20
0
        public void RotatedRectangleIntersectionVector()
        {
            var rr1 = new RotatedRect(new Point2f(100, 100), new Size2f(100, 100), 45);
            var rr2 = new RotatedRect(new Point2f(130, 100), new Size2f(100, 100), 0);

            Cv2.RotatedRectangleIntersection(rr1, rr2, out var intersectingRegion);

            if (Debugger.IsAttached)
            {
                Point[] ToPoints(IEnumerable <Point2f> enumerable)
                {
                    return(enumerable.Select(p => new Point(p.X, p.Y)).ToArray());
                }

                using (var img = new Mat(200, 200, MatType.CV_8UC3, 0))
                {
                    img.Polylines(new[] { ToPoints(rr1.Points()) }, true, Scalar.Red);
                    img.Polylines(new[] { ToPoints(rr2.Points()) }, true, Scalar.Green);
                    img.Polylines(new[] { ToPoints(intersectingRegion) }, true, Scalar.White);

                    Window.ShowImages(img);
                }
            }

            intersectingRegion.ToString();
        }
Esempio n. 21
0
        private static Rectangle _FindLargestRectFromContours(VectorOfVectorOfPoint contours)
        {
            //IF CONTOURS WERE FOUND ON THE IMAGE,
            //OBTAIN THE BIGGEST CONTOUR
            double largestArea      = 0;
            int    largestAreaIndex = 0;

            for (int i = 0; i < contours.Size; i++)
            {
                double A = CvInvoke.ContourArea(contours[i]);
                if (A > largestArea)
                {
                    largestArea      = A;
                    largestAreaIndex = i;
                }
            }
            try {
                RotatedRect r_rect    = CvInvoke.MinAreaRect(contours[largestAreaIndex]);
                PointF[]    vertixles = r_rect.GetVertices();
                int         x         = (int)vertixles[1].X;
                int         y         = (int)vertixles[2].Y;
                //OBTAIN RECTANGLE THAT SURROUNDS THE DETECTED CONTOUR
                int width  = (int)((vertixles[3].X) - (vertixles[1].X));
                int height = (int)((vertixles[0].Y) - (vertixles[2].Y));

                Rectangle rectangle = new Rectangle(x, y, width, height);
                return(rectangle);
            }
            catch {
                return(new Rectangle(0, 0, 1, 1));
            }
        }
Esempio n. 22
0
 public ClusterElement(int id, int cn, RotatedRect rect, int[] profile)
 {
     Id        = id;
     Element   = rect;
     ClusterNo = cn;
     Profile   = profile;
 }
Esempio n. 23
0
        bool verifySize(RotatedRect mr)
        {
            float error  = m_error;
            float aspect = m_aspect;
            int   min    = 34 * 8 * m_verifyMin; // minimum area
            int   max    = 34 * 8 * m_verifyMax; // maximum area
            float rmin   = aspect - aspect * error;
            float rmax   = aspect + aspect * error;
            float area   = mr.Size.Height * mr.Size.Width;
            float r      = mr.Size.Width / mr.Size.Height;

            if (r < 1)
            {
                r = mr.Size.Height / mr.Size.Width;
            }

            if ((area < min || area > max) || (r < rmin || r > rmax))
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
 static void GetRegionRectangle(ref RotatedRect region, out Rect rect)
 {
     rect.Width  = (int)region.Size.Width;
     rect.Height = (int)region.Size.Height;
     rect.X      = (int)region.Center.X - rect.Width / 2;
     rect.Y      = (int)region.Center.Y - rect.Height / 2;
 }
Esempio n. 25
0
        public Rectangle  camshift(Image <Bgr, byte> frame, Rectangle trackerbox, Mat HIST)
        {
            Mat HSV = new Mat();

            CvInvoke.CvtColor(frame, HSV, ColorConversion.Bgr2Hsv);

            float[] range    = { 0, 180 };
            int[]   histsize = { 24 };
            int[]   channels = { 0, 0 };
            int[]   Chn      = { 0 };

            Mat dst = new Mat();

            float[] Range = { 0, 180 };

            var vhue = new VectorOfMat(HSV);

            CvInvoke.CalcBackProject(vhue, Chn, HIST, dst, Range, 1);
            Mat element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(5, 5), new Point(1, 1));//

            CvInvoke.Erode(dst, dst, element, new Point(1, 1), 1, BorderType.Default, new MCvScalar(0, 0, 0));


            Mat DST = new Mat();

            MCvTermCriteria termCrit = new MCvTermCriteria(10, 1);

            try
            {
                RotatedRect result = CvInvoke.CamShift(dst, ref trackerbox, termCrit);
            }
            catch (Exception e) { MessageBox.Show(e.ToString()); }
            tbox = trackerbox;
            return(trackerbox);
        }
Esempio n. 26
0
        public Mat GetBoxImages(double threshold1, double threshold2)
        {
            Mat       template = GetGrayImage();
            MCvScalar color    = new MCvScalar(255, 255, 255);

            using (VectorOfVectorOfPoint contours = GetContours(threshold1, threshold2))
            {
                for (int i = 0; i < contours.Size; i++)
                {
                    RotatedRect box  = CvInvoke.MinAreaRect(contours[i]);
                    Rectangle   rect = box.MinAreaRect();
                    int         area = rect.Size.Height * rect.Size.Width;

                    if (area < 200000)
                    {
                        continue;
                    }

                    if (rect.Size.Width > 0)
                    {
                        Debug.WriteLine("{0}:{1} / {2}/{3} : {4} : {5}", rect.Top, rect.Left, rect.Bottom, rect.Right, area, (double)rect.Size.Height / (double)rect.Size.Width);
                    }

                    CvInvoke.Rectangle(template, box.MinAreaRect(), color);
                }
            }

            return(template);
        }
Esempio n. 27
0
        public WRectangle(Contour contour)
        {
            var points = new VectorOfPoint(contour.Data);

            this.Rectangle = CvInvoke.MinAreaRect(points);
            this.Data      = this.Rectangle.GetVertices().Select(v => Point.Round(v)).ToArray();
        }
        public double getResult(out OpenCvSharp.RotatedRect location)
        {
            VP_MatchTemplate m = (VP_MatchTemplate)testcase;

            location = new RotatedRect(new Point2f(m.Loc.X + temp.Width / 2, m.Loc.Y + temp.Height / 2), new Size2f(temp.Width, temp.Height), -(float)m.Angle);
            return(m.Val);
        }
Esempio n. 29
0
        public void Draw(Robot robot, PointF offset, SizeF size)
        {
            PointF center   = robot.Center.Add(offset);
            float  armAngle = 0;
            // Base
            RotatedRect rect = new RotatedRect(ToPixel(center), ToPixel(size), 0);

            CvInvoke.Ellipse(Drawing, rect, new Bgr(Color.Black).MCvScalar, 1);

            rect = new RotatedRect(ToPixel(center), ToPixel(new SizeF(size.Width / 2, size.Height / 2)), 0);
            CvInvoke.Ellipse(Drawing, rect, new Bgr(Color.DarkOrange).MCvScalar, 4);

            // Arm
            PointF effectorCenter = new PointF(
                center.X - 0.8f * size.Width * (float)Math.Sin(armAngle),
                center.Y - 0.8f * size.Width * (float)Math.Cos(armAngle)
                );

            CvInvoke.Line(Drawing, ToPixel(center), ToPixel(effectorCenter), new Bgr(Color.Black).MCvScalar, 2);
            // Effector
            //float effectorHalfLength = size.Width * 0.07f;
            //float fingerHalfLength = size.Width * 0.04f;

            //PointF leftFingerMiddle = new PointF(
            //    effectorCenter.X - effectorHalfLength * (float)Math.Cos(effectorAngle),
            //    effectorCenter.Y + effectorHalfLength * (float)Math.Sin(effectorAngle)
            //);
            //PointF rightFingerMiddle = new PointF(
            //    effectorCenter.X + effectorHalfLength * (float)Math.Cos(effectorAngle),
            //    effectorCenter.Y - effectorHalfLength * (float)Math.Sin(effectorAngle)
            //);

            //CvInvoke.Line(Drawing, ToPixel(leftFingerMiddle), ToPixel(rightFingerMiddle), new Bgr(Color.Green).MCvScalar, 2);
        }
        void Canvas_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.PageUp)
            {
                ImageScale += ScaleIncrement;
            }
            if (e.KeyCode == Keys.PageDown)
            {
                ImageScale -= ScaleIncrement;
            }
            if (e.Control && e.KeyCode == Keys.Z)
            {
                commandExecutor.Undo();
            }
            if (e.Control && e.KeyCode == Keys.Y)
            {
                commandExecutor.Redo();
            }
            if (e.Control && e.KeyCode == Keys.V)
            {
                var roiText = (string)Clipboard.GetData(DataFormats.Text);
                try
                {
                    var mousePosition = PointToClient(MousePosition);
                    var offset        = NormalizedLocation(mousePosition.X, mousePosition.Y);
                    var roiData       = (float[])ArrayConvert.ToArray(roiText, 1, typeof(float));
                    var center        = new Point2f(offset.X, offset.Y);
                    var size          = new Size2f(roiData[0], roiData[1]);
                    var roi           = new RotatedRect(center, size, 0);

                    var selection = selectedRoi;
                    commandExecutor.Execute(
                        () => AddRegion(roi),
                        () => { regions.Remove(roi); SelectedRegion = selection; });
                }
                catch (ArgumentException) { }
                catch (InvalidCastException) { }
                catch (FormatException) { }
            }

            if (selectedRoi.HasValue)
            {
                if (e.Control && e.KeyCode == Keys.C)
                {
                    var roi     = regions[selectedRoi.Value];
                    var roiData = new[] { roi.Size.Width, roi.Size.Height };
                    Clipboard.SetData(DataFormats.Text, ArrayConvert.ToString(roiData));
                }

                if (e.KeyCode == Keys.Delete)
                {
                    var selection = selectedRoi.Value;
                    var region    = regions[selection];
                    commandExecutor.Execute(
                        () => { regions.RemoveAt(selection); SelectedRegion = null; },
                        () => { regions.Insert(selection, region); SelectedRegion = selection; });
                }
            }
        }
Esempio n. 31
0
 /// <summary>
 /// Implements CAMSHIFT object tracking algorithm ([Bradski98]). First, it finds an object center using cvMeanShift and, after that, calculates the object size and orientation. 
 /// </summary>
 /// <param name="probImage">Back projection of object histogram </param>
 /// <param name="window">Initial search window</param>
 /// <param name="criteria">Criteria applied to determine when the window search should be finished</param>
 /// <returns>Circumscribed box for the object, contains object size and orientation</returns>
 public static RotatedRect CamShift(
    IInputArray probImage,
    ref Rectangle window,
    MCvTermCriteria criteria)
 {
    RotatedRect box = new RotatedRect();
    using (InputArray iaProbImage = probImage.GetInputArray())
    {
       cveCamShift(iaProbImage, ref window, ref criteria, ref box);
    }
    return box;
 }
Esempio n. 32
0
        public static void DrawEllipse(IInputOutputArray image,
            RotatedRect box,
            MCvScalar color,
            int thickness = 1,
            LineType lineType = LineType.EightConnected,
            int shift = 0)
        {
            int width = (int)Math.Round(box.Size.Height * 0.5F);
            int height = (int)Math.Round(box.Size.Width * 0.5F);
            Size axesSize = new Size(width, height);
            Point center = Point.Round(box.Center);

            DrawEllipse(image, center, axesSize, box.Angle, 0.0D, 360.0D, color, thickness, lineType, shift);
        }
Esempio n. 33
0
        public static PointF[] BoxPoints(RotatedRect box)
        {
            PointF[] pointsArray = new PointF[4];

            using (DisposableHandle arrayHandle = DisposableHandle.Alloc(pointsArray))
            {
                using (Mat arrayMat = new Mat(4, 2, DepthType.Cv32F, 1, arrayHandle, 8))
                {
                    using (OutputArray outArray = arrayMat.GetOutputArray())
                    {
                        cveBoxPoints(ref box, outArray);
                    }
                }
            }

            return pointsArray;
        }
Esempio n. 34
0
 private static extern void cveBoxPoints(
    ref RotatedRect box,
    IntPtr pt);
Esempio n. 35
0
 /// <summary>
 /// Calculates vertices of the input 2d box.
 /// </summary>
 /// <param name="box">The box</param>
 /// <param name="points">The output array of four vertices of rectangles.</param>
 public static void BoxPoints(RotatedRect box, IOutputArray points)
 {
    using (OutputArray oaPoints = points.GetOutputArray())
       cveBoxPoints(ref box, oaPoints);
 }
Esempio n. 36
0
 /// <summary>
 /// Calculates vertices of the input 2d box.
 /// </summary>
 /// <param name="box">The box</param>
 /// <returns>The four vertices of rectangles.</returns>
 public static PointF[] BoxPoints(RotatedRect box)
 {
    PointF[] pts = new PointF[4];
    GCHandle handle = GCHandle.Alloc(pts, GCHandleType.Pinned);
    using (Mat vp = new Mat(4, 2, DepthType.Cv32F, 1, handle.AddrOfPinnedObject(), 8))
    using (OutputArray oaVp = vp.GetOutputArray())
    {
       cveBoxPoints(ref box, oaVp);
    }
    handle.Free();
    return pts;
 }
Esempio n. 37
0
 private static extern CvEnum.RectIntersectType cveRotatedRectangleIntersection(ref RotatedRect rect1, ref RotatedRect rect2, IntPtr intersectingRegion);
Esempio n. 38
0
 /// <summary>
 /// Create an ellipse from the specific RotatedRect
 /// </summary>
 /// <param name="box2d">The RotatedRect representation of this ellipse</param>
 public Ellipse(RotatedRect box2d)
 {
    _box2D = box2d;
 }
Esempio n. 39
0
        public RectangleContour(RotatedRect rect, Size imageSize)
        {
            var size = rect.Size;
            var center = rect.Center;
            var angle = rect.Angle * Math.PI / 180f;
            var a = 0.5f * (float)Math.Sin(angle);
            var b = 0.5f * (float)Math.Cos(angle);

            unsafe
            {
                var ptr = Marshal.AllocHGlobal(HeaderSize);
                var points = (int*)((byte*)ptr + Contour.HeaderSize + SeqBlockSize);
                points[0] = Math.Max(0, Math.Min(imageSize.Width - 1, (int)(center.X - a * size.Height - b * size.Width)));
                points[1] = Math.Max(0, Math.Min(imageSize.Height - 1, (int)(center.Y + b * size.Height - a * size.Width)));
                points[2] = Math.Max(0, Math.Min(imageSize.Width - 1, (int)(center.X + a * size.Height - b * size.Width)));
                points[3] = Math.Max(0, Math.Min(imageSize.Height - 1, (int)(center.Y - b * size.Height - a * size.Width)));
                points[4] = Math.Max(0, Math.Min(imageSize.Width - 1, (int)(2 * center.X - points[0])));
                points[5] = Math.Max(0, Math.Min(imageSize.Height - 1, (int)(2 * center.Y - points[1])));
                points[6] = Math.Max(0, Math.Min(imageSize.Width - 1, (int)(2 * center.X - points[2])));
                points[7] = Math.Max(0, Math.Min(imageSize.Height - 1, (int)(2 * center.Y - points[3])));

                Rect boundingRect;
                var minX = Math.Min(Math.Min(Math.Min(points[0], points[2]), points[4]), points[6]);
                var minY = Math.Min(Math.Min(Math.Min(points[1], points[3]), points[5]), points[7]);
                var maxX = Math.Max(Math.Max(Math.Max(points[0], points[2]), points[4]), points[6]);
                var maxY = Math.Max(Math.Max(Math.Max(points[1], points[3]), points[5]), points[7]);
                boundingRect.X = minX;
                boundingRect.Y = minY;
                boundingRect.Width = maxX - minX + 1;
                boundingRect.Height = maxY - minY + 1;

                var seqBlock = (_CvSeqBlock*)((byte*)ptr + Contour.HeaderSize);
                seqBlock->start_index = 0;
                seqBlock->count = 4;
                seqBlock->data = (IntPtr)points;
                seqBlock->next = (IntPtr)seqBlock;
                seqBlock->prev = (IntPtr)seqBlock;

                var contour = (_CvContour*)ptr;
                var seqFlags = (int)SequenceElementType.Point | (int)SequenceKind.Curve | (int)SequenceFlags.Closed;
                contour->flags = (int)((seqFlags & ~CV_MAGIC_MASK) | CV_SEQ_MAGIC_VAL);
                contour->header_size = Contour.HeaderSize;
                contour->h_next = IntPtr.Zero;
                contour->h_prev = IntPtr.Zero;
                contour->v_next = IntPtr.Zero;
                contour->v_prev = IntPtr.Zero;
                contour->total = 4;
                contour->elem_size = Marshal.SizeOf(typeof(Point));
                contour->block_max = (IntPtr)((byte*)ptr + HeaderSize);
                contour->ptr = contour->block_max;
                contour->delta_elems = 128;
                contour->storage = IntPtr.Zero;
                contour->free_blocks = IntPtr.Zero;
                contour->first = (IntPtr)seqBlock;
                contour->color = 0;
                contour->rect = boundingRect;
                contour->reserved0 = 0;
                contour->reserved1 = 0;
                contour->reserved2 = 0;
                SetHandle(ptr);
            }
        }
Esempio n. 40
0
 /// <summary>
 /// Finds a rotated rectangle of the minimum area enclosing the input 2D point set.
 /// </summary>
 /// <param name="points">Input vector of 2D points</param>
 /// <returns>a circumscribed rectangle of the minimal area for 2D point set</returns>
 public static RotatedRect MinAreaRect(IInputArray points)
 {
    RotatedRect rect = new RotatedRect();
    using (InputArray iaPoints = points.GetInputArray())
       cveMinAreaRect(iaPoints, ref rect);
    return rect;
 }
Esempio n. 41
0
 private static extern void cveCamShift(
    IntPtr probImage,
    ref Rectangle window,
    ref MCvTermCriteria criteria,
    ref RotatedRect box);
Esempio n. 42
0
      public void TestMorphologyClosing()
      {
         //draw some blobs
         Image<Gray, Byte> img = new Image<Gray, byte>(400, 400);
         RotatedRect box1 = new RotatedRect(new PointF(100, 200), new SizeF(60, 80), 30.0f);
         RotatedRect box2 = new RotatedRect(new PointF(180, 250), new SizeF(70, 100), 0.0f);
         img.Draw(box1, new Gray(255.0), -1);
         img.Draw(box2, new Gray(255.0), -1);

         Image<Gray, Byte> result = img.ConcateHorizontal(MorphologyClosing(img, 10));
         //Emgu.CV.UI.ImageViewer.Show(result, "Left: original, Right: merged");
      }
Esempio n. 43
0
 public void TestGetBox2DPoints()
 {
    RotatedRect box = new RotatedRect(
       new PointF(3.0f, 2.0f),
       new SizeF(4.0f, 6.0f),
       0.0f);
    PointF[] vertices = box.GetVertices();
    //TODO: Find out why the following test fails. (x, y) convention changed.
    //Assert.IsTrue(vertices[0].Equals(new PointF(0.0f, 0.0f)));
    //Assert.IsTrue(vertices[1].Equals(new PointF(6.0f, 0.0f)));
 }
Esempio n. 44
0
      /// <summary>
      /// Draws a simple or thick elliptic arc or fills an ellipse sector. The arc is clipped by ROI rectangle. A piecewise-linear approximation is used for antialiased arcs and thick arcs. All the angles are given in degrees.
      /// </summary>
      /// <param name="img">Image</param>
      /// <param name="box">The box the define the ellipse area</param>
      /// <param name="color">Ellipse color</param>
      /// <param name="thickness">Thickness of the ellipse arc</param>
      /// <param name="lineType">Type of the ellipse boundary</param>
      /// <param name="shift">Number of fractional bits in the center coordinates and axes' values</param>
      public static void Ellipse(
          IInputOutputArray img,
          RotatedRect box,
          MCvScalar color,
          int thickness = 1,
          CvEnum.LineType lineType = CvEnum.LineType.EightConnected,
          int shift = 0)
      {
         Size axes = new Size();
         axes.Width = (int)Math.Round(box.Size.Height * 0.5);
         axes.Height = (int)Math.Round(box.Size.Width * 0.5);

         Ellipse(img, Point.Round(box.Center), axes, box.Angle, 0, 360, color, thickness, lineType, shift);
      }
Esempio n. 45
0
 /// <summary>
 /// Fits an ellipse around a set of 2D points.
 /// </summary>
 /// <param name="points">Input 2D point set</param>
 /// <returns>The ellipse that fits best (in least-squares sense) to a set of 2D points</returns>
 public static RotatedRect FitEllipse(IInputArray points)
 {
    RotatedRect ellipse = new RotatedRect();
    using (InputArray iaPoints = points.GetInputArray())
       cveFitEllipse(iaPoints, ref ellipse);
    return ellipse;
 }
Esempio n. 46
0
 private static extern void cveFitEllipse(IntPtr points, ref RotatedRect ellipse);
Esempio n. 47
0
        public void DeSkew()
        {
            Rectangle vBoundary = new Rectangle(new Point(0, 0), new Size(140, originalImage.Height));

            Emgu.CV.Cvb.CvBlobDetector bDetect = new Emgu.CV.Cvb.CvBlobDetector();
            Emgu.CV.Cvb.CvBlobs markerBlob = new Emgu.CV.Cvb.CvBlobs();

            List<Rectangle> blobs = new List<Rectangle>();

            Image<Gray, Byte> preprocessImage = originalImage.Convert<Gray, Byte>();
            preprocessImage = preprocessImage.ThresholdBinary(new Gray(200), new Gray(255));
            preprocessImage = preprocessImage.Not();

            markerBlob.Clear();

            bDetect.Detect(preprocessImage, markerBlob);
            preprocessImage.Dispose();
            preprocessImage = null;

            markerBlob.FilterByArea(250, 1800);

            foreach (Emgu.CV.Cvb.CvBlob targetBlob in markerBlob.Values)
            {
                if (vBoundary.Contains(targetBlob.BoundingBox))
                {
                    if (targetBlob.BoundingBox.Width >= targetBlob.BoundingBox.Height - 5)
                    {
                        Rectangle r = new Rectangle(targetBlob.BoundingBox.X, targetBlob.BoundingBox.Y, targetBlob.BoundingBox.Width, targetBlob.BoundingBox.Height);
                        blobs.Add(r);
                    }
                }
            }

            RectangleF temp = blobs.First();
            RectangleF temp2 = blobs.Last();

            double dY = Math.Abs(temp.Y - temp2.Y);
            double dX = Math.Abs(temp.X - temp2.X);

            double angle = Math.Atan2(dX, dY);
            angle = angle * (180 / Math.PI);

            if (temp2.X > temp.X)
            {
                angle = angle * -1;
            }

            RotatedRect rot_rec = new RotatedRect();
            rot_rec.Center = new PointF(temp.X, temp.Y);
            RotationMatrix2D rot_mat = new RotationMatrix2D(rot_rec.Center, angle, 1);
            Image<Bgr, Byte> outimage = originalImage.CopyBlank();
            CvInvoke.WarpAffine(originalImage, outimage, rot_mat, originalImage.Size, Inter.Cubic, Warp.Default, BorderType.Constant, new Bgr(Color.White).MCvScalar);

            int xOffset = 80 - (int)temp.X;
            int yOffset = 45 - (int)temp.Y;

            originalImage = outimage.Copy();

            Bitmap a = originalImage.ToBitmap();
            CanvasMove filter = new CanvasMove(new AForge.IntPoint(xOffset, yOffset), Color.White);
            a = filter.Apply(a);
            originalImage = new Image<Bgr, Byte>(a);

            a.Dispose();
            a = null;
            outimage.Dispose();
            outimage = null;
            blobs = null;
        }
Esempio n. 48
0
 private static extern void cveMinAreaRect(IntPtr points, ref RotatedRect box);
Esempio n. 49
0
 /// <summary>
 /// Finds out if there is any intersection between two rotated rectangles.
 /// </summary>
 /// <param name="rect1">First rectangle</param>
 /// <param name="rect2">Second rectangle</param>
 /// <param name="intersectingRegion">The output array of the verticies of the intersecting region. It returns at most 8 vertices. Stored as VectorOfPointF or Mat as Mx1 of type CV_32FC2.</param>
 /// <returns>The intersect type</returns>
 public static CvEnum.RectIntersectType RotatedRectangleIntersection(RotatedRect rect1, RotatedRect rect2, IOutputArray intersectingRegion)
 {
    using (OutputArray oaIntersectingRegion = intersectingRegion.GetOutputArray())
       return cveRotatedRectangleIntersection(ref rect1, ref rect2, oaIntersectingRegion);
 }
Esempio n. 50
0
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Mat frame = new Mat();
             //_capture.Retrieve(frame, 0);
             frame = new Mat("C:\\Emgu\\Dump\\ea6b5b28a66c.jpg", LoadImageType.Unchanged);
             Mat grayFrame = new Mat();
             CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
             Mat smallGrayFrame = new Mat();
             CvInvoke.PyrDown(grayFrame, smallGrayFrame);
             Mat smoothedGrayFrame = new Mat();
             CvInvoke.PyrUp(smallGrayFrame, smoothedGrayFrame);

             CvInvoke.Threshold(smoothedGrayFrame, smoothedGrayFrame, 100, 255, ThresholdType.Binary);
             //Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
             //Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
             Mat cannyFrame = new Mat();
             CvInvoke.Canny(smoothedGrayFrame, cannyFrame, 100, 60);

             //Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(100, 60);

             VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
             CvInvoke.FindContours(cannyFrame, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);
             CvInvoke.DrawContours(frame, contours, 2, new Bgr(Color.Blue).MCvScalar);
             List<RotatedRect> BL = new List<RotatedRect>();
             List<VectorOfPoint> CL = new List<VectorOfPoint>();
             for (int i = 0; i < contours.Size; i++)
             {
               using (VectorOfPoint contour = contours[i])
               using (VectorOfPoint approxContour = new VectorOfPoint())
               {
             CvInvoke.ApproxPolyDP(contour, approxContour, CvInvoke.ArcLength(contour, true) * 0.05, true);
             BL.Add(CvInvoke.MinAreaRect(approxContour));
             CL.Add(contour);
               }
             }

             VectorOfPoint maxContour = CL[0];
            double maxContourArea = CvInvoke.ContourArea(CL[0], false);
             for (int i = 0; i < CL.Count; i++)
             {
               if (CvInvoke.ContourArea(CL[i], false) > maxContourArea)
               {
             maxContourArea = CvInvoke.ContourArea(CL[i], false);
             maxContour = CL[i];
               }
             }

             RotatedRect TMP = new RotatedRect();
             TMP = CvInvoke.MinAreaRect(maxContour);
             CvInvoke.Polylines(frame, Array.ConvertAll(TMP.GetVertices(), Point.Round), true, new Bgr(Color.Pink).MCvScalar, 2);

             Image<Bgr, Byte> srcImg = frame.ToImage<Bgr, Byte>();
             srcImg.ROI = new Rectangle((int)(TMP.Center.X - 0.5 * TMP.Size.Width), (int)(TMP.Center.Y - 0.5 * TMP.Size.Height), (int)TMP.Size.Width, (int)TMP.Size.Height);
             Image<Bgr, Byte> croppedImg = srcImg.Copy();
             cannyImageBox.Image = croppedImg;
             float[,] tmp = {
                            {0, frame.Height}, //down
                            {0, 0},//left
                            {frame.Width, 0}, // up
                            {frame.Width, frame.Height} //right
                       };
             Matrix<float> sourceMat = new Matrix<float>(tmp);
             float[,] target = {
                            {0, (float)0.85 * frame.Height},
                            {0, 0},
                            {(float)0.85*frame.Width, 0},
                            {(float)0.55*frame.Width, (float)0.55*frame.Height}
                       };
             PointF[] tmpPF = new PointF[4];
             PointF[] targetPF = new PointF[4];

             for (int i = 0; i < 4; i++)
             {
               tmpPF[i].X = tmp[i, 0]; tmpPF[i].Y = tmp[i, 1];
               targetPF[i].X = target[i, 0]; targetPF[i].Y = target[i, 1];
             }

             Matrix<float> targetMat = new Matrix<float>(target);
             Mat TTT = CvInvoke.GetPerspectiveTransform(tmpPF, targetPF);
             Mat newcroppimg = new Mat();
             CvInvoke.WarpPerspective(croppedImg, newcroppimg, TTT, new System.Drawing.Size(241, 240));

            //CvInvoke.DrawContours(frame, TMP, 2, new Bgr(Color.Red).MCvScalar);

             /*
            foreach (RotatedRect box in BL)
             {
               CvInvoke.Polylines(frame, Array.ConvertAll(box.GetVertices(), Point.Round), true, new Bgr(Color.DarkOrange).MCvScalar, 2);
             }*/

             captureImageBox.Image = frame;
             grayscaleImageBox.Image = newcroppimg;
             smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
             //cannyImageBox.Image = cannyFrame;
        }
Esempio n. 51
0
 ///<summary>
 ///Create an ellipse with specific parameters
 ///</summary>
 ///<param name="center"> The center of the ellipse</param>
 ///<param name="size"> The width and height of the ellipse</param>
 ///<param name="angle"> The rotation angle in radian for the ellipse</param>
 public Ellipse(PointF center, SizeF size, float angle)
 {
    _box2D = new RotatedRect(center, size, angle);
 }