Example usage for org.opencv.imgproc Imgproc rectangle

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

Introduction

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

Prototype

public static void rectangle(Mat img, Point pt1, Point pt2, Scalar color, int thickness) 

Source Link

Usage

From source file:app.GUI2.java

/**
 * this function preproces image to draw helper figures on top of image and
 * then calls updateView()//from ww  w.j  a va2  s  .  c om
 */
private void updateDrawings(Mat unprocesedImage) {
    try {
        image = unprocesedImage.clone();
        // draw Points as circles
        if (pointA1 != null) {
            Imgproc.circle(image, pointA1, 10, new Scalar(0, 0, 128), 2);
        }
        if (pointA2 != null) {
            Imgproc.circle(image, pointA2, 10, new Scalar(0, 0, 255), 2);
        }
        if (pointB1 != null) {
            Imgproc.circle(image, pointB1, 10, new Scalar(128, 0, 0), 2);
        }
        if (pointB2 != null) {
            Imgproc.circle(image, pointB2, 10, new Scalar(255, 0, 0), 2);
        }
        // draw rectangles of selected circles
        if (pointA1 != null & pointA2 != null) {
            Imgproc.rectangle(image, pointA1, pointA2, new Scalar(0, 0, 255), 2);
        }
        if (pointB1 != null & pointB2 != null) {
            Imgproc.rectangle(image, pointB1, pointB2, new Scalar(255, 0, 0), 2);
        }

        updateView(image);
    } catch (Exception e) {
        System.err.println("Tryied to Draw without loading image" + e);
    }

}

From source file:ch.zhaw.facerecognitionlibrary.Helpers.MatOperation.java

License:Open Source License

public static Point drawRectangleOnPreview(Mat img, Rect face, boolean front_camera) {
    if (front_camera) {
        int topLeftX = (int) (img.cols() - (face.tl().x + face.width));
        int bottomRightX = (int) (img.cols() - (face.br().x) + face.width);
        Point tl = new Point(topLeftX, face.tl().y);
        Point br = new Point(bottomRightX, face.br().y);
        Imgproc.rectangle(img, tl, br, FACE_RECT_COLOR, THICKNESS);
        return tl;
    } else {/*ww w  .  j a va2s.  c  o m*/
        Imgproc.rectangle(img, face.tl(), face.br(), FACE_RECT_COLOR, THICKNESS);
        return face.tl();
    }
}

From source file:com.carver.paul.truesight.ImageRecognition.RecognitionModel.java

License:Open Source License

/**
 * Draws the line onto the image, used for debugging.
 * @param img//from   w  w  w .java  2 s  .co m
 */
public void Draw(Mat img) {
    Imgproc.rectangle(img, new org.opencv.core.Point(rect.left, rect.top),
            new org.opencv.core.Point(rect.right, rect.bottom), new Scalar(0, 255, 0), 2);
}

From source file:com.example.afs.makingmusic.process.ImageGenerator.java

License:Open Source License

@Override
public void process(Frame frame) {
    Mat image = frame.getImageMatrix();/*from  w ww . j a va2s  . co  m*/
    for (MusicAnnotation musicAnnotation : frame.getMusicAnnotations()) {
        Scalar color;
        Type type = musicAnnotation.getType();
        switch (type) {
        case NEW:
            color = GREEN;
            break;
        case ACTIVE:
            color = BLUE;
            break;
        case DUPLICATE:
            color = YELLOW;
            break;
        case OVERFLOW:
            color = RED;
            break;
        default:
            throw new UnsupportedOperationException();
        }
        Rect item = musicAnnotation.getItem();
        Imgproc.rectangle(image, item.br(), item.tl(), color, 1);
        //Imgproc.putText(image, musicAnnotation.getInstrument().getName(), item.tl(), Core.FONT_HERSHEY_PLAIN, 1d, color);
    }
    BufferedImage bufferedImage = toBufferedImage(image);
    frame.setBufferedImage(bufferedImage);
}

From source file:com.minio.io.alice.MainActivity.java

License:Open Source License

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    if (srcMat != null) {
        srcMat.release();//ww  w  .j  a v a2s  .co  m
    }

    srcMat = inputFrame.rgba();

    if (matVideoWriter.isRecording()) {
        matVideoWriter.write(srcMat, webSocket);
    }
    if (serverReply != null) {

        if (serverReply.isReply() == true) {
            if (XDebug.LOG) {
                Log.i(MainActivity.TAG, " Alice found someone");
            }
            Imgproc.rectangle(srcMat, serverReply.getP1(), serverReply.getP2(), serverReply.getScalar(),
                    serverReply.getThickness());
            if (serverReply.getZoom() != 0)
                mOpenCvCameraView.increaseZoom(serverReply.getZoom());
            return srcMat;
        }

        if (serverReply.getDisplay()) {
            // Wake up if the display is set to true
            if (XDebug.LOG) {
                Log.i(MainActivity.TAG, " Alice Wakes up");
                Log.i(MainActivity.TAG, String.valueOf(serverReply.isReply()));
            }
            return srcMat;
        } else {
            if (XDebug.LOG) {
                Log.i(MainActivity.TAG, "Alice Sleeps");
            }
            // return a black mat when server replies with false for Display.
            blackMat = srcMat.clone();
            blackMat.setTo(new Scalar(0, 0, 0));
            return blackMat;
        }
    } else {
        // return black frame unless woken up explicitly by server.
        blackMat = srcMat.clone();
        blackMat.setTo(new Scalar(0, 0, 0));
        return blackMat;
    }

}

From source file:com.raulh82vlc.face_detection_sample.opencv.render.FaceDrawerOpenCV.java

License:Apache License

public static void drawFaceShapes(Rect face, Mat matrixRGBA) {
    Point start = face.tl();//from www .  j  av  a2 s.  c o m
    int h = (int) start.y + (face.height / 2);
    int w = (int) start.x + (face.width / 2);
    Imgproc.rectangle(matrixRGBA, start, face.br(), FACE_RECT_COLOR, 3);
    Point center = new Point(w, h);
    Imgproc.circle(matrixRGBA, center, 10, new Scalar(255, 0, 0, 255), 3);
}

From source file:com.raulh82vlc.face_detection_sample.opencv.render.FaceDrawerOpenCV.java

License:Apache License

public static void drawEyeRectangle(Rect eyeArea, Mat matrixRgba) {
    Imgproc.rectangle(matrixRgba, eyeArea.tl(), eyeArea.br(), new Scalar(255, 0, 0, 255), 2);
}

From source file:com.serenegiant.usbcameratest.MainActivity.java

License:Apache License

@Override
public boolean onTouchEvent(MotionEvent e) {
    //?/* ww  w .j  a  v  a  2 s  .  com*/
    if (createTemplateFlag) {
        if (templateRect.x == -1 || templateRect.y == -1) {
            templateRect.x = (int) e.getX();
            templateRect.y = (int) e.getY();
        } else {
            templateRect.width = (int) e.getX() - templateRect.x;
            templateRect.height = (int) e.getY() - templateRect.y;
        }
        textView1.setText("tl:" + templateRect.tl());

        //???
        showImg = searchImg.clone();
        Imgproc.rectangle(showImg, templateRect.tl(), templateRect.br(), new Scalar(0, 0, 255), 5);
        Bitmap bitmap = Bitmap.createBitmap(showImg.width(), showImg.height(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(showImg, bitmap);
        imageView1.setImageBitmap(bitmap);
    }

    return true;
}

From source file:edu.fiu.cate.breader.BaseSegmentation.java

/**
 * Capture button has been press. Obtain the high resolution image and the low resolution 
 * data. Once captured, the images are corrected. 
 *//*from w w  w  . j a  v  a  2s  .com*/
public void captureEvent() {
    long t0, t1;
    t0 = System.currentTimeMillis();
    t1 = t0;
    byte[][][] img = getHidefImage();
    System.out.println("HiRez Capture: " + (System.currentTimeMillis() - t0) / 1000.0);
    new IViewer("HiRez", ImageManipulation.getBufferedImage(img));

    t0 = System.currentTimeMillis();
    Rect bound = null;
    try {
        bound = highRes(BReaderTools.byteArrayToMat(ITools.toGrayscale(img)));
    } catch (java.lang.Exception e) {
    }
    System.out.println("First bounding box: " + (System.currentTimeMillis() - t0) / 1000.0);

    //      Mat imgMat = BReaderTools.byteArrayToMat(img);
    //      Imgproc.rectangle(imgMat, bound.tl(), bound.br(), new Scalar(255,255,0), 8);

    byte[][] low = ITools.normalize(normImgCropped);
    t0 = System.currentTimeMillis();
    Rect boundLow = null;
    try {
        boundLow = lowResDist(BReaderTools.byteArrayToMat(low));
    } catch (java.lang.Exception e) {
    }
    System.out.println("second bounding box: " + (System.currentTimeMillis() - t0) / 1000.0);

    if (bound == null || boundLow == null) {
        tts.doTTS("Document outside field of view. Please realign and press capture again.");
        return;
    }

    if ((bound.x + bound.width + 100) >= img[0][0].length || (bound.y + bound.height + 100) >= img[0].length) {
        tts.doTTS("Document outside field of view. Please realign and press capture again.");
        return;
    }

    //Show the cropped height map with the bounding box
    Mat color = new Mat();
    Imgproc.cvtColor(BReaderTools.byteArrayToMat(low), color, Imgproc.COLOR_GRAY2BGR);
    Imgproc.rectangle(color, boundLow.tl(), boundLow.br(), new Scalar(255, 255, 0), 1);
    new IViewer("LowRes Bounding Box", BReaderTools.bufferedImageFromMat(color));

    Imgproc.cvtColor(BReaderTools.byteArrayToMat(ITools.toGrayscale(img)), color, Imgproc.COLOR_GRAY2BGR);
    Imgproc.rectangle(color, bound.tl(), bound.br(), new Scalar(255, 255, 0), 8);
    new IViewer("HighRes Bounding Box", BReaderTools.bufferedImageFromMat(color));

    //      System.out.println(bound.height+", "+bound.width+": "+(double)bound.width/(double)bound.height);
    //      System.out.println(boundLow.height+", "+boundLow.width+": "+(double)boundLow.width/(double)boundLow.height);

    double rW = (double) bound.width / (double) boundLow.width;
    double rH = (double) bound.height / (double) boundLow.height;
    int h = 0, w = 0, yO = 0, xO = 0;
    double s = 0;

    if (rH < rW) {
        s = rH;
        h = boundLow.height;
        w = (int) (bound.width / rH);
        if ((w - boundLow.width) % 2 == 0) {
            xO = (boundLow.width - w) / 2;
        }
    } else {
        s = rW;
        h = (int) (bound.height / rW);
        w = boundLow.width;
        if ((h - boundLow.height) % 2 == 0) {
            yO = (boundLow.height - h) / 2;
        }
    }

    //show the high resolution image cropped
    byte[][][] hiRez = new byte[img.length][][];
    t0 = System.currentTimeMillis();
    for (int i = 0; i < img.length; i++) {
        hiRez[i] = ITools.crop(bound.x, bound.y, bound.x + bound.width, bound.y + bound.height, img[i]);
    }
    System.out.println("Cropping HiRez: " + (System.currentTimeMillis() - t0) / 1000.0);

    //Show the IR amplitude image cropped
    //      byte[][] amp = ITools.normalize(amplitudes);
    //      byte[][] ampRez = resize(amp, (float)s);
    //      int x0 = (int) ((boundLow.x+xO+40)*s), y0 = (int) ((boundLow.y+yO+25)*s);
    //      ampRez = ITools.crop(x0, y0, x0+bound.width, y0+bound.height, ampRez);
    //      new IViewer(ImageManipulation.getGrayBufferedImage(ampRez));

    //Show the Amplitude image in bounding box
    //      Rect nBound = new Rect(boundLow.x+xO+40, boundLow.y+yO+25, w, h);
    //      Mat gray = new Mat();
    //      Imgproc.cvtColor(BReaderTools.byteArrayToMat(ITools.normalize(amplitudes)), gray,Imgproc.COLOR_GRAY2BGR);
    //      Imgproc.rectangle(gray, nBound.tl(), nBound.br(), new Scalar(255,255,0), 1);
    //      new IViewer(BReaderTools.bufferedImageFromMat(gray));

    //Crop the distance image and prepare for correction
    float[][] distRez;
    Mat destRezM = new Mat();
    switch (disp.getInterpolationMethod()) {
    case 1:
        Imgproc.resize(BReaderTools.floatArrayToMat(normImg), destRezM, new Size(0, 0), s, s,
                Imgproc.INTER_LINEAR);//resize image
        break;
    case 2:
        Imgproc.resize(BReaderTools.floatArrayToMat(normImg), destRezM, new Size(0, 0), s, s,
                Imgproc.INTER_CUBIC);//resize image
        break;
    case 3:
        Imgproc.resize(BReaderTools.floatArrayToMat(normImg), destRezM, new Size(0, 0), s, s,
                Imgproc.INTER_LANCZOS4);//resize image
        break;
    }
    distRez = BReaderTools.matToFloatArray(destRezM);
    int xCentOff = (img[0][0].length - bound.width) / 2 - bound.x;
    int yCentOff = (img[0].length - bound.height) / 2 - bound.y;
    int x0 = (int) ((boundLow.x + xO + 40) * s), y0 = (int) ((boundLow.y + yO + 25) * s);
    distRez = ITools.crop(x0, y0, x0 + bound.width, y0 + bound.height, distRez);
    distRez = multiply(distRez, -100);

    byte[][][] foldCorrected = new byte[hiRez.length][][];
    t0 = System.currentTimeMillis();
    for (int i = 0; i < hiRez.length; i++) {
        foldCorrected[i] = BReaderTools.foldCorrection(hiRez[i], distRez, xCentOff, yCentOff);
    }
    System.out.println("Fold Correction: " + (System.currentTimeMillis() - t0) / 1000.0);

    float[][] distRezPushed = BReaderTools.foldCorrection(distRez,
            (distRez[0].length - boundLow.width) / 2 - boundLow.x,
            (distRez.length - boundLow.height) / 2 - boundLow.y);

    byte[][][] extensionCorrected = new byte[hiRez.length][][];
    t0 = System.currentTimeMillis();
    for (int i = 0; i < hiRez.length; i++) {
        extensionCorrected[i] = LuWang.extentionWithLinearInterpolation(foldCorrected[i], distRez);
    }
    System.out.println("Extension Correction: " + (System.currentTimeMillis() - t0) / 1000.0);

    new IViewer("Heigths", ImageManipulation.getGrayBufferedImage(ITools.normalize(distRez)));
    new IViewer("HiRez", ImageManipulation.getBufferedImage(hiRez));
    //      new IViewer("Corrected",ImageManipulation.getBufferedImage(foldCorrected));
    //      new IViewer("Heigths",ImageManipulation.getGrayBufferedImage(ITools.normalize(distRezPushed)));
    //      new IViewer("Flat",ImageManipulation.getBufferedImage(foldCorrected));
    //      new IViewer("Extension",ImageManipulation.getBufferedImage(extensionCorrected));
    System.out.println("Overall time: " + (System.currentTimeMillis() - t1) / 1000.0);

    SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd-hh-mm-ss");
    String time = format.format(new Date(System.currentTimeMillis()));

    // Save Corrected High Rez.
    String imgPath = saveDir + "/correctedImage-" + time + ".tiff";
    switch (disp.getCorrectionMethod()) {
    case 1: {
        ImageManipulation.writeImage(hiRez, imgPath);
        new IViewer("Correction Results: None", ImageManipulation.getBufferedImage(hiRez));
    }
        break;
    case 2: {
        ImageManipulation.writeImage(foldCorrected, imgPath);
        new IViewer("Correction Results: Flattening", ImageManipulation.getBufferedImage(foldCorrected));
    }
        break;
    case 3: {
        ImageManipulation.writeImage(extensionCorrected, imgPath);
        new IViewer("Correction Results: Flattening + Extension",
                ImageManipulation.getBufferedImage(extensionCorrected));
    }
        break;
    }

    try {
        String text = abbyy.processImage(imgPath, saveDir + "/text-" + time + ".txt");
        System.out.println("Done!!!!");
        tts.doTTS(text);
    } catch (java.lang.NullPointerException e) {
        tts.doTTS("ABBYY License expired.");
    }
    saveData(time, img, hiRez, distRez, boundLow, bound);

}

From source file:edu.wpi.first.wpilibj.examples.axiscamera.Robot.java

License:Open Source License

@Override
public void robotInit() {
    m_visionThread = new Thread(() -> {
        // Get the Axis camera from CameraServer
        AxisCamera camera = CameraServer.getInstance().addAxisCamera("axis-camera.local");
        // Set the resolution
        camera.setResolution(640, 480);/*w  w  w  .j a va2s  .  c  o m*/

        // Get a CvSink. This will capture Mats from the camera
        CvSink cvSink = CameraServer.getInstance().getVideo();
        // Setup a CvSource. This will send images back to the Dashboard
        CvSource outputStream = CameraServer.getInstance().putVideo("Rectangle", 640, 480);

        // Mats are very memory expensive. Lets reuse this Mat.
        Mat mat = new Mat();

        // This cannot be 'true'. The program will never exit if it is. This
        // lets the robot stop this thread when restarting robot code or
        // deploying.
        while (!Thread.interrupted()) {
            // Tell the CvSink to grab a frame from the camera and put it
            // in the source mat.  If there is an error notify the output.
            if (cvSink.grabFrame(mat) == 0) {
                // Send the output the error.
                outputStream.notifyError(cvSink.getError());
                // skip the rest of the current iteration
                continue;
            }
            // Put a rectangle on the image
            Imgproc.rectangle(mat, new Point(100, 100), new Point(400, 400), new Scalar(255, 255, 255), 5);
            // Give the output stream a new image to display
            outputStream.putFrame(mat);
        }
    });
    m_visionThread.setDaemon(true);
    m_visionThread.start();
}