コード例 #1
0
        // Use this for initialization
        void Start()
        {
            Texture2D imgTexture = Resources.Load("chessboard") as Texture2D;

            Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC3);

            Utils.texture2DToMat(imgTexture, imgMat);
            Debug.Log("imgMat dst ToString " + imgMat.ToString());



            Imgproc.line(imgMat, new Point(50, 50), new Point(400, 105), new Scalar(0, 0, 200), 3);

            Imgproc.rectangle(imgMat, new Point(150, 200), new Point(300, 300), new Scalar(0, 200, 0), 5);

            Imgproc.circle(imgMat, new Point(500, 300), 80, new Scalar(200, 0, 0), 1);

            Imgproc.arrowedLine(imgMat, new Point(100, 500), new Point(550, 350), new Scalar(255, 255, 0), 4, Core.LINE_8, 0, 0.1);


            double angle = 100;

            Imgproc.ellipse(imgMat, new Point(200, 400), new Size(80, 150), angle, angle - 200, angle + 100, new Scalar(255, 255, 255), -1);


            int[] face = { Core.FONT_HERSHEY_SIMPLEX,        Core.FONT_HERSHEY_PLAIN,         Core.FONT_HERSHEY_DUPLEX,         Core.FONT_HERSHEY_COMPLEX,
                           Core.FONT_HERSHEY_TRIPLEX,        Core.FONT_HERSHEY_COMPLEX_SMALL, Core.FONT_HERSHEY_SCRIPT_SIMPLEX,
                           Core.FONT_HERSHEY_SCRIPT_COMPLEX, Core.FONT_ITALIC };


            Imgproc.putText(imgMat, "OpenCV", new Point(50, 50), face [0], 1.2, new Scalar(0, 0, 200), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(50, 100), face [1], 1.2, new Scalar(0, 200, 0), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(50, 150), face [2], 1.2, new Scalar(200, 0, 0), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(50, 200), face [3], 1.2, new Scalar(0, 100, 100), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(50, 250), face [4], 1.2, new Scalar(100, 100, 0), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(50, 300), face [5], 1.2, new Scalar(100, 0, 100), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(50, 350), face [6], 1.2, new Scalar(100, 100, 100), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(50, 400), face [7], 1.2, new Scalar(100, 100, 200), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(300, 50), face [0] | face [8], 1.2, new Scalar(100, 200, 100), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(300, 100), face [1] | face [8], 1.2, new Scalar(200, 100, 100), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(300, 150), face [2] | face [8], 1.2, new Scalar(200, 200, 100), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(300, 200), face [3] | face [8], 1.2, new Scalar(200, 100, 200), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(300, 250), face [4] | face [8], 1.2, new Scalar(100, 200, 200), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(300, 300), face [5] | face [8], 1.2, new Scalar(100, 200, 255), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(300, 350), face [6] | face [8], 1.2, new Scalar(100, 255, 200), 2, Imgproc.LINE_AA, false);
            Imgproc.putText(imgMat, "OpenCV", new Point(300, 400), face [7] | face [8], 1.2, new Scalar(255, 200, 100), 2, Imgproc.LINE_AA, false);



            Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);

            Utils.matToTexture2D(imgMat, texture);

            gameObject.GetComponent <Renderer> ().material.mainTexture = texture;
        }
        // Use this for initialization
        void Start()
        {
            //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
            Utils.setDebugMode(true);

            Texture2D imgTexture = Resources.Load("lena") as Texture2D;

            Mat img = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC4);

            Utils.texture2DToMat(imgTexture, img);
            Debug.Log("imgMat.ToString() " + img.ToString());


            OpenCVForUnity.ShapeModule.ThinPlateSplineShapeTransformer tps = Shape.createThinPlateSplineShapeTransformer(0);
            MatOfPoint2f sourcePoints = new MatOfPoint2f(
                new Point(0, 0),
                new Point(512, 0),
                new Point(0, 512),

                new Point(250, 200),
                new Point(400, 400),
                new Point(200, 400),

                new Point(512, 512)
                );
            MatOfPoint2f targetPoints = new MatOfPoint2f(
                new Point(0, 0),
                new Point(512, 0),
                new Point(0, 599),

                new Point(250, 120),
                new Point(450, 450),
                new Point(100, 450),

                new Point(512, 512)
                );
            MatOfDMatch matches = new MatOfDMatch(
                new DMatch(0, 0, 0),
                new DMatch(1, 1, 0),
                new DMatch(2, 2, 0),
                new DMatch(3, 3, 0),
                new DMatch(4, 4, 0),
                new DMatch(5, 5, 0),
                new DMatch(6, 6, 0)
                );


            //http://stackoverflow.com/questions/32207085/shape-transformers-and-interfaces-opencv3-0
            Core.transpose(sourcePoints, sourcePoints);
            Core.transpose(targetPoints, targetPoints);

            Debug.Log("sourcePoints " + sourcePoints.ToString());
            Debug.Log("targetPoints " + targetPoints.ToString());

            tps.estimateTransformation(targetPoints, sourcePoints, matches);

            MatOfPoint2f transPoints = new MatOfPoint2f();

            tps.applyTransformation(sourcePoints, transPoints);

            Debug.Log("sourcePoints " + sourcePoints.dump());
            Debug.Log("targetPoints " + targetPoints.dump());
            Debug.Log("transPoints " + transPoints.dump());


            Mat res = new Mat();

            tps.warpImage(img, res);

            //plot points
            Point[] sourcePointsArray = sourcePoints.toArray();
            Point[] targetPointsArray = targetPoints.toArray();
            for (int i = 0; i < sourcePointsArray.Length; i++)
            {
                Imgproc.arrowedLine(res, sourcePointsArray [i], targetPointsArray [i], new Scalar(255, 255, 0, 255), 3, Imgproc.LINE_AA, 0, 0.2);

                Imgproc.circle(res, sourcePointsArray [i], 10, new Scalar(255, 0, 0, 255), -1);
                Imgproc.circle(res, targetPointsArray [i], 10, new Scalar(0, 0, 255, 255), -1);
            }


            Texture2D texture = new Texture2D(res.cols(), res.rows(), TextureFormat.RGBA32, false);

            Utils.matToTexture2D(res, texture);

            gameObject.GetComponent <Renderer> ().material.mainTexture = texture;


            Utils.setDebugMode(false);
        }