Ejemplo n.º 1
0
        private MatOfPoint OrderCornerPoints(MatOfPoint corners)
        {
            if (corners.size().area() <= 0 || corners.rows() < 4)
            {
                return(corners);
            }

            // rearrange the points in the order of upper left, upper right, lower right, lower left.
            using (Mat x = new Mat(corners.size(), CvType.CV_32SC1))
                using (Mat y = new Mat(corners.size(), CvType.CV_32SC1))
                    using (Mat d = new Mat(corners.size(), CvType.CV_32SC1))
                        using (Mat dst = new Mat(corners.size(), CvType.CV_32SC2))
                        {
                            Core.extractChannel(corners, x, 0);
                            Core.extractChannel(corners, y, 1);

                            // the sum of the upper left points is the smallest and the sum of the lower right points is the largest.
                            Core.add(x, y, d);
                            Core.MinMaxLocResult result = Core.minMaxLoc(d);
                            dst.put(0, 0, corners.get((int)result.minLoc.y, 0));
                            dst.put(2, 0, corners.get((int)result.maxLoc.y, 0));

                            // the difference in the upper right point is the smallest, and the difference in the lower left is the largest.
                            Core.subtract(y, x, d);
                            result = Core.minMaxLoc(d);
                            dst.put(1, 0, corners.get((int)result.minLoc.y, 0));
                            dst.put(3, 0, corners.get((int)result.maxLoc.y, 0));

                            dst.copyTo(corners);
                        }
            return(corners);
        }
Ejemplo n.º 2
0
    private int CompareContourRows(MatOfPoint mp1, MatOfPoint mp2)
    {
        int rows1 = mp1.rows();
        int rows2 = mp2.rows();

        if (rows1 == rows2) { return 0; }
        else if (rows1 > rows2) { return -1; }
        else { return 1; }
    }
Ejemplo n.º 3
0
        private Mat PerspectiveTransform(Mat image, MatOfPoint corners)
        {
            if (corners.size().area() <= 0 || corners.rows() < 4)
            {
                return(image);
            }

            Point[] pts = corners.toArray();
            Point   tl  = pts[0];
            Point   tr  = pts[1];
            Point   br  = pts[2];
            Point   bl  = pts[3];

            double widthA   = Math.Sqrt((br.x - bl.x) * (br.x - bl.x) + (br.y - bl.y) * (br.y - bl.y));
            double widthB   = Math.Sqrt((tr.x - tl.x) * (tr.x - tl.x) + (tr.y - tl.y) * (tr.y - tl.y));
            int    maxWidth = Math.Max((int)widthA, (int)widthB);

            double heightA   = Math.Sqrt((tr.x - br.x) * (tr.x - br.x) + (tr.y - br.y) * (tr.y - br.y));
            double heightB   = Math.Sqrt((tl.x - bl.x) * (tl.x - bl.x) + (tl.y - bl.y) * (tl.y - bl.y));
            int    maxHeight = Math.Max((int)heightA, (int)heightB);

            maxWidth  = (maxWidth < 1) ? 1 : maxWidth;
            maxHeight = (maxHeight < 1) ? 1 : maxHeight;

            Mat src = new Mat();

            corners.convertTo(src, CvType.CV_32FC2);
            Mat dst = new Mat(4, 1, CvType.CV_32FC2);

            dst.put(0, 0, 0, 0, maxWidth - 1, 0, maxWidth - 1, maxHeight - 1, 0, maxHeight - 1);

            // compute and apply the perspective transformation matrix.
            Mat outputMat            = new Mat(maxHeight, maxWidth, image.type(), new Scalar(0, 0, 0, 255));
            Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src, dst);

            Imgproc.warpPerspective(image, outputMat, perspectiveTransform, new Size(outputMat.cols(), outputMat.rows()));

            // return the transformed image.
            return(outputMat);
        }