jeremy-ellis-tech / Xamarin.Android.OpenCV

C# bindings for the OpenCV Android SDK
MIT License
92 stars 44 forks source link

perspective transform example #18

Closed RodrigodeMoura closed 7 years ago

RodrigodeMoura commented 7 years ago

Hi,

Someone already implemented a perspective transform function in xamarin opencv?

I have done this.

        ImageView iView = FindViewById<ImageView>(Resource.Id.imageView1);

        using (Bitmap img = BitmapFactory.DecodeResource(Resources, Resource.Drawable.example))
        {
            if (img != null)
            {
                Mat m = new Mat();
                 grayM = new Mat();
                Mat edged = new Mat();
                Mat img2 = new Mat();
                Mat img3 = new Mat();
                Mat orig = new Mat();
                Utils.BitmapToMat(img, m);

                Imgproc.GaussianBlur(m, m, new OpenCV.Core.Size(3, 3), 0);
                Imgproc.CvtColor(m, m, Imgproc.ColorBgr2gray);
                Imgproc.AdaptiveThreshold(m, m, 255, Imgproc.AdaptiveThreshGaussianC, Imgproc.ThreshBinary, 75, 10);
                Core.Bitwise_not(m, m);

                Imgproc.CvtColor(m, img2, Imgproc.ColorGray2rgb);

                Imgproc.CvtColor(m, img3, Imgproc.ColorGray2rgb);

                Mat lines = new Mat();
                Imgproc.HoughLinesP(m, lines, 1, Math.PI / 180, 80, 400, 10);
                for(int i=0; i< lines.Rows(); i++)
                {
                    double[] l = lines.Get(i, 0);
                    Imgproc.Line(img2, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3);

                }

                List<Point> corners = new List<Point>();
                Teste3 teste = new Teste3();
                for (int i = 0; i < lines.Rows(); i++)
                {
                    for (int j = i + 1; j < lines.Rows(); j++)
                    {
                        Point pt = teste.computeIntersect(lines.Get(i,0), lines.Get(j,0));
                        if (pt.X >= 0 && pt.Y >= 0 && pt.X < m.Rows() && pt.Y < m.Cols())
                            corners.Add(pt);
                    }
                }

                Point center = new Point(0, 0);

                for (int i = 0; i < corners.Count; i++)
                {
                    center.X += corners[i].X  / corners.Count;
                    center.Y += corners[i].Y / corners.Count;
                }

                teste.sortCorners(corners, center);

                Rect r = Imgproc.BoundingRect(new MatOfPoint(corners.ToArray()));

                Mat quad = Mat.Zeros(r.Height, r.Width, CvType.Cv8uc3);

                List<Point> quad_pts = new List<Point>();
                ////original
                quad_pts.Add(new Point(0, 0));
                quad_pts.Add(new Point(quad.Cols(), 0));
                quad_pts.Add(new Point(quad.Cols(), quad.Rows()));
                quad_pts.Add(new Point(0, quad.Rows()));

                Mat cornerPts = Converters.Vector_Point2f_to_Mat(corners);
                Mat resultPts = Converters.Vector_Point2f_to_Mat(quad_pts);

                Mat transmtx = Imgproc.GetPerspectiveTransform(cornerPts, resultPts);
                Imgproc.WarpPerspective(img3, quad, transmtx, quad.Size());

                using (Bitmap bm = Bitmap.CreateBitmap(quad.Cols(), quad.Rows(), Bitmap.Config.Argb8888))
                {
                    Utils.MatToBitmap(quad, bm);

                    iView.SetImageBitmap(bm);
                }

                m.Release();
                grayM.Release();
            }
jeremy-ellis-tech commented 7 years ago

Thanks.