Example usage for org.opencv.imgproc Imgproc drawMarker

List of usage examples for org.opencv.imgproc Imgproc drawMarker

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc drawMarker.

Prototype

public static void drawMarker(Mat img, Point position, Scalar color) 

Source Link

Usage

From source file:org.firstinspires.ftc.teamcode.libraries.VuforiaBallLib.java

protected BallColor getBallColor() {
    //get frame from vuforia
    try {/*  w  w  w.  j a va2 s. c o m*/
        Mat out = getFrame();

        //get vuforia's real position matrix
        Matrix34F goodCodeWritten = ((VuforiaDefaultListenerShim) relicTemplate.getListener()).getRealPose();

        if (goodCodeWritten == null)
            return BallColor.Undefined;

        this.tempMark = RelicRecoveryVuMark.from(relicTemplate);

        final float[][] ballPoints = new float[4][2];
        //the actual color determination
        for (int i = 8; i < point.length; i++)
            ballPoints[i - 8] = Tool.projectPoint(camCal, goodCodeWritten, point[i]).getData();

        Point imagePoints[] = new Point[ballPoints.length];
        //convert points to opencv language
        for (int i = 0; i < ballPoints.length; i++)
            imagePoints[i] = new Point((int) ballPoints[i][0], (int) ballPoints[i][1]);

        //calculate points from projection
        //find the midpoint between the two points
        int leftXPoint = (int) ((imagePoints[0].x + imagePoints[1].x) / 2.0);
        int leftYPoint = (int) ((imagePoints[0].y + imagePoints[1].y) / 2.0);
        //find the y distande between the two
        int leftDist = (int) (Math.abs(imagePoints[0].y - imagePoints[1].y) / 2.0);
        int[] leftBall = new int[] { leftXPoint - (leftDist / 2), leftYPoint - (leftDist / 2) };

        //find the midpoint between the two points
        int rightXPoint = (int) ((imagePoints[2].x + imagePoints[3].x) / 2.0);
        int rightYPoint = (int) ((imagePoints[2].y + imagePoints[3].y) / 2.0);
        //find the y distande between the two
        int rightDist = (int) (Math.abs(imagePoints[2].y - imagePoints[3].y) / 2.0);
        int[] rightBall = new int[] { rightXPoint - (rightDist / 2), rightYPoint - (rightDist / 2) };
        //operation: subsquare
        //take a square mat we are 100% sure will have a ball in it
        //sum it up and find the average color

        Scalar leftColor = drawSquare(out, leftBall, leftDist);
        Scalar rightColor = drawSquare(out, rightBall, rightDist);

        if (displayData) {
            float[][] squarePoints = new float[8][2];
            for (int i = 0; i < 8; i++)
                squarePoints[i] = Tool.projectPoint(camCal, goodCodeWritten, point[i]).getData();
            Point[] squarePointRay = new Point[squarePoints.length];
            for (int i = 0; i < squarePoints.length; i++)
                squarePointRay[i] = new Point((int) squarePoints[i][0], (int) squarePoints[i][1]);

            Scalar green = new Scalar(0, 255, 0);
            for (int i = 0; i < 2; i++)
                for (int o = 0; o < 4; o++)
                    Imgproc.line(out, squarePointRay[o == 0 ? 3 + i * 4 : i * 4 + o - 1],
                            squarePointRay[i * 4 + o], green);

            //connect the rectangles
            for (int i = 0; i < 4; i++)
                Imgproc.line(out, squarePointRay[i], squarePointRay[i + 4], green);

            for (int i = 0; i < imagePoints.length; i++)
                Imgproc.drawMarker(out, imagePoints[i], green);

            //flip it for display
            Core.flip(out, out, -1);

            drawFrame(out);
        }

        if (leftColor != null && rightColor != null) {
            if (leftColor.val[0] < leftColor.val[1] && rightColor.val[0] > rightColor.val[1])
                return BallColor.LeftBlue;
            else if (leftColor.val[0] > leftColor.val[1] && rightColor.val[0] < rightColor.val[1])
                return BallColor.LeftRed;
            else
                return BallColor.Indeterminate;
        } else
            return BallColor.Undefined;
    } catch (Exception e) {
        Log.e("OPENCV", e.getMessage());
        return BallColor.Undefined;
    }
}

From source file:org.firstinspires.ftc.teamcode.opmodes.demo.VumarkHiJackVideo.java

@Override
public void init_loop() {
    RelicRecoveryVuMark vuMark = RelicRecoveryVuMark.from(relicTemplate);
    if (vuMark != RelicRecoveryVuMark.UNKNOWN) {

        /* Found an instance of the template. In the actual game, you will probably
         * loop until this condition occurs, then move on to act accordingly depending
         * on which VuMark was visible. */
        telemetry.addData("VuMark", "%s visible", vuMark);

        /* For fun, we also exhibit the navigational pose. In the Relic Recovery game,
         * it is perhaps unlikely that you will actually need to act on this pose information, but
         * we illustrate it nevertheless, for completeness. */
        //qualcomm is stupid and didn't let me see the real vuforia matrix, so I shimmed it
        //heres where is gets good
        VuforiaDefaultListenerShim writeGoodCode = (VuforiaDefaultListenerShim) relicTemplate.getListener();

        OpenGLMatrix pose = writeGoodCode.getRawPose();
        telemetry.addData("Pose", format(pose));

        /* We further illustrate how to decompose the pose into useful rotational and
         * translational components */
        if (pose != null) {
            //alternate projection!
            //get vuforia's real position matrix
            Matrix34F goodCodeWritten = writeGoodCode.getRealPose();

            //reset imagePoints
            final float[][] vufPoints = new float[point.length][2];

            //use vuforias projectPoints method to project all those box points
            for (int i = 0; i < point.length; i++) {
                //project
                vufPoints[i] = Tool.projectPoint(camCal, goodCodeWritten, point[i]).getData();
                //convert to opencv language

                //telemetry.addData("point", "num: %d, x: %f.2, y: %f.2", i, vufPoints[i][0], vufPoints[i][1]);
            }/*ww w. j ava2 s.c  o  m*/

            //telemetry.addData("Camera Size", "w: %f.2, h: %f.2", camCal.getSize().getData()[0], camCal.getSize().getData()[1]);

            //get frame from vuforia
            try {
                VuforiaLocalizer.CloseableFrame frame = ray.take();

                int img = 0;
                for (; img < frame.getNumImages(); img++) {
                    //telemetry.addData("Image format " + img, frame.getImage(img).getFormat());
                    if (frame.getImage(img).getFormat() == PIXEL_FORMAT.RGB888)
                        break;
                }

                Image mImage = frame.getImage(img);

                //stick it in a Mat for "display purposes"
                Mat out = new Mat(mImage.getHeight(), mImage.getWidth(), CV_8UC3);

                java.nio.ByteBuffer color = mImage.getPixels();
                byte[] ray = new byte[color.limit()];
                color.rewind();
                color.get(ray);
                out.put(0, 0, ray);

                frame.close();

                //convert points, halfing distances b/c vuforia does that internally so we gotta fix it
                for (int i = 0; i < vufPoints.length; i++)
                    imagePoints[i] = new Point((int) vufPoints[i][0], (int) vufPoints[i][1]);

                Scalar green = new Scalar(0, 255, 0);
                for (int i = 0; i < 2; i++)
                    for (int o = 0; o < 4; o++)
                        Imgproc.line(out, imagePoints[o == 0 ? 3 + i * 4 : i * 4 + o - 1],
                                imagePoints[i * 4 + o], green);

                //connect the rectangles
                for (int i = 0; i < 4; i++)
                    Imgproc.line(out, imagePoints[i], imagePoints[i + 4], green);

                for (int i = 8; i < imagePoints.length; i++)
                    Imgproc.drawMarker(out, imagePoints[i], green);

                //calculate points from projection
                //find the midpoint between the two points
                int leftXPoint = (int) ((imagePoints[8].x + imagePoints[9].x) / 2.0);
                int leftYPoint = (int) ((imagePoints[8].y + imagePoints[9].y) / 2.0);
                //find the y distande between the two
                leftDist = (int) (Math.abs(imagePoints[8].y - imagePoints[9].y) / 2.0);
                leftBall = new int[] { leftXPoint - (leftDist / 2), leftYPoint - (leftDist / 2) };

                //find the midpoint between the two points
                int rightXPoint = (int) ((imagePoints[10].x + imagePoints[11].x) / 2.0);
                int rightYPoint = (int) ((imagePoints[10].y + imagePoints[11].y) / 2.0);
                //find the y distande between the two
                rightDist = (int) (Math.abs(imagePoints[10].y - imagePoints[11].y) / 2.0);
                rightBall = new int[] { rightXPoint - (rightDist / 2), rightYPoint - (rightDist / 2) };

                //operation: subsquare
                //take a square mat we are 100% sure will have a ball in it
                //sum it up and find the average color

                leftColor = drawSquare(out, leftBall, leftDist);
                rightColor = drawSquare(out, rightBall, rightDist);

                //flip it for display
                Core.flip(out, out, -1);

                matQueue.add(out);

                //display!
                mView.getHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Mat frame = matQueue.take();
                            //convert to bitmap
                            Utils.matToBitmap(frame, bm);
                            frame.release();
                            mView.invalidate();
                        } catch (InterruptedException e) {
                            //huh
                        }
                    }
                });
            } catch (Exception e) {
                Log.e("OPENCV", e.getLocalizedMessage());
            }
        }
    } else {
        telemetry.addData("VuMark", "not visible");
    }
}

From source file:org.firstinspires.ftc.teamcode.opmodes.demo.VumarkOpenCV.java

@Override
public void init_loop() {
    RelicRecoveryVuMark vuMark = RelicRecoveryVuMark.from(relicTemplate);
    if (vuMark != RelicRecoveryVuMark.UNKNOWN) {

        /* Found an instance of the template. In the actual game, you will probably
         * loop until this condition occurs, then move on to act accordingly depending
         * on which VuMark was visible. */
        telemetry.addData("VuMark", "%s visible", vuMark);

        /* For fun, we also exhibit the navigational pose. In the Relic Recovery game,
         * it is perhaps unlikely that you will actually need to act on this pose information, but
         * we illustrate it nevertheless, for completeness. */
        //qualcomm is stupid and didn't let me see the real vuforia matrix, so I shimmed it
        //heres where is gets good
        VuforiaDefaultListenerShim writeGoodCode = (VuforiaDefaultListenerShim) relicTemplate.getListener();

        OpenGLMatrix pose = writeGoodCode.getRawPose();
        telemetry.addData("Pose", format(pose));

        /* We further illustrate how to decompose the pose into useful rotational and
         * translational components */
        if (pose != null) {
            //alternate projection!
            //get vuforia's real position matrix
            Matrix34F goodCodeWritten = writeGoodCode.getRealPose();

            //reset imagePoints
            final float[][] vufPoints = new float[point.length][2];

            //use vuforias projectPoints method to project all those box points
            for (int i = 0; i < point.length; i++) {
                //project
                vufPoints[i] = Tool.projectPoint(camCal, goodCodeWritten, point[i]).getData();
                //convert to opencv language

                //telemetry.addData("point", "num: %d, x: %f.2, y: %f.2", i, vufPoints[i][0], vufPoints[i][1]);
            }// www.  j av  a 2 s . com

            //telemetry.addData("Camera Size", "w: %f.2, h: %f.2", camCal.getSize().getData()[0], camCal.getSize().getData()[1]);

            //get frame from vuforia
            try {
                VuforiaLocalizer.CloseableFrame frame = ray.take();

                int img = 0;
                for (; img < frame.getNumImages(); img++) {
                    //Log.i("OPENCV", "Image " + img + " " + frame.getImage(img).getWidth());
                    if (frame.getImage(img).getWidth() == size[0])
                        break;
                }

                final Mat out;
                if (img < frame.getNumImages())
                    out = getMatFromImage(frame.getImage(img));
                else
                    throw new IllegalArgumentException("No frame with matching width!");

                frame.close();

                //convert points, halfing distances b/c vuforia does that internally so we gotta fix it
                for (int i = 0; i < vufPoints.length; i++)
                    imagePoints[i] = new Point((int) vufPoints[i][0] / 2, (int) vufPoints[i][1] / 2);

                Scalar color = new Scalar(0, 255, 0);
                for (int i = 0; i < 2; i++)
                    for (int o = 0; o < 4; o++)
                        Imgproc.line(out, imagePoints[o == 0 ? 3 + i * 4 : i * 4 + o - 1],
                                imagePoints[i * 4 + o], color);

                //connect the rectangles
                for (int i = 0; i < 4; i++)
                    Imgproc.line(out, imagePoints[i], imagePoints[i + 4], color);

                for (int i = 8; i < imagePoints.length; i++)
                    Imgproc.drawMarker(out, imagePoints[i], color);

                //flip it for display
                Core.flip(out, out, -1);

                matQueue.add(out);

                //display!
                mView.getHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Mat frame = matQueue.take();
                            //convert to bitmap
                            Utils.matToBitmap(frame, bm);
                            frame.release();
                            mView.invalidate();
                        } catch (InterruptedException e) {
                            //huh
                        }
                    }
                });
            } catch (Exception e) {
                Log.e("OPENCV", e.getLocalizedMessage());
            }
        }
    } else {
        telemetry.addData("VuMark", "not visible");
    }
}