Example usage for org.opencv.core Scalar Scalar

List of usage examples for org.opencv.core Scalar Scalar

Introduction

In this page you can find the example usage for org.opencv.core Scalar Scalar.

Prototype

public Scalar(double v0, double v1, double v2) 

Source Link

Usage

From source file:arlocros.ComputePose.java

License:Apache License

public boolean computePose(Mat rvec, Mat tvec, Mat image2) throws NyARException, FileNotFoundException {
    // convert image to NyAR style for processing
    final INyARRgbRaster imageRaster = NyARImageHelper.createFromMat(image2);

    // create new marker system configuration
    i_config = new NyARMarkerSystemConfig(i_param);
    markerSystemState = new NyARMarkerSystem(i_config);
    // Create wrapper that passes cam pictures to marker system
    cameraSensorWrapper = new NyARSensor(i_screen_size);
    ids = new int[markerPatterns.size()];
    patternmap = new HashMap<>();
    for (int i = 0; i < markerPatterns.size(); i++) {
        // create marker description from pattern file and add to marker
        // system
        ids[i] = markerSystemState.addARMarker(arCodes.get(i), 25, markerConfig.getMarkerSize());
        patternmap.put(ids[i], markerPatterns.get(i));
    }//from  w  w  w  .  j  a  va  2s.  c  o m

    cameraSensorWrapper.update(imageRaster);
    markerSystemState.update(cameraSensorWrapper);

    // init 3D point list
    final List<Point3> points3dlist = new ArrayList<>();
    final List<Point> points2dlist = new ArrayList<>();

    for (final int id : ids) {
        // process only if this marker has been detected
        if (markerSystemState.isExistMarker(id) && markerSystemState.getConfidence(id) > 0.7) {
            // read and add 2D points
            final NyARIntPoint2d[] vertex2d = markerSystemState.getMarkerVertex2D(id);
            Point p = new Point(vertex2d[0].x, vertex2d[0].y);
            points2dlist.add(p);
            p = new Point(vertex2d[1].x, vertex2d[2].y);
            points2dlist.add(p);
            p = new Point(vertex2d[2].x, vertex2d[2].y);
            points2dlist.add(p);
            p = new Point(vertex2d[3].x, vertex2d[3].y);
            points2dlist.add(p);

            //            final MatOfPoint mop = new MatOfPoint();
            //            mop.fromList(points2dlist);
            //            final List<MatOfPoint> pts = new ArrayList<>();
            //            pts.add(mop);

            // read and add corresponding 3D points
            points3dlist.addAll(markerConfig.create3dpointlist(patternmap.get(id)));

            if (visualization) {
                // draw red rectangle around detected marker
                Core.rectangle(image2, new Point(vertex2d[0].x, vertex2d[0].y),
                        new Point(vertex2d[2].x, vertex2d[2].y), new Scalar(0, 0, 255));
                final String markerFile = patternmap.get(id).replaceAll(".*4x4_", "").replace(".patt", "");
                Core.putText(image2, markerFile,
                        new Point((vertex2d[2].x + vertex2d[0].x) / 2.0, vertex2d[0].y - 5), 4, 1,
                        new Scalar(250, 0, 0));
            }
        }

    }
    // load 2D and 3D points to Mats for solvePNP
    final MatOfPoint3f objectPoints = new MatOfPoint3f();
    objectPoints.fromList(points3dlist);
    final MatOfPoint2f imagePoints = new MatOfPoint2f();
    imagePoints.fromList(points2dlist);

    if (visualization) {
        // show image with markers detected
        Imshow.show(image2);
    }

    // do not call solvePNP with empty intput data (no markers detected)
    if (points2dlist.size() == 0) {
        objectPoints.release();
        imagePoints.release();
        return false;
    }

    // uncomment these lines if using RANSAC-based pose estimation (more
    // shaking)
    Mat inliers = new Mat();

    Calib3d.solvePnPRansac(objectPoints, imagePoints, cameraMatrix, distCoeffs, rvec, tvec, false, 300, 5, 16,
            inliers, Calib3d.CV_P3P);
    ArMarkerPoseEstimator.getLog()
            .info("Points detected: " + points2dlist.size() + " inliers: " + inliers.size());

    objectPoints.release();
    imagePoints.release();

    // avoid publish zero pose if localization failed
    if (inliers.rows() == 0) {
        inliers.release();
        return false;
    }

    inliers.release();
    return true;
}

From source file:attendance_system_adder.cv.image.java

public Mat getFaceDetec(Mat image) {
    Mat face = null;/*from   w w  w.  j a  v a 2s.co m*/

    System.out.println("\nRunning DetectFaceDemo");

    CascadeClassifier faceDetector = new CascadeClassifier(".\\resource\\haarcascade_frontalface_default.xml");

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);

    //    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
    //    // Draw a bounding box around each face.
    for (Rect rect : faceDetections.toArray()) {
        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0));
        face = new Mat(image, rect);
    }
    //    // Save the visualized detection.
    //    String filename = "faceDetection.png";
    //    System.out.println(String.format("Writing %s", filename));
    //    imwrite(filename, image);
    //FaceRecognizer fr;//= new LBPHFaceRecognizer();
    return face;
}

From source file:attendance_system_adder.cv.image.java

public Mat FaceDetec(Mat image) {
    Mat face = null;/* w  w w . j a  v a  2s  .co m*/

    System.out.println("\nRunning DetectFaceDemo");

    CascadeClassifier faceDetector = new CascadeClassifier(".\\resource\\haarcascade_frontalface_default.xml");

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);

    //    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
    //    // Draw a bounding box around each face.
    for (Rect rect : faceDetections.toArray()) {
        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0));

    }
    return image;
}

From source file:balldetection.BallDetection.java

/**
 * @param args the command line arguments
 * @throws java.io.IOException/* w ww  .j a v a  2 s  .  c  om*/
 */
public static void main(String[] args) throws IOException {

    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
            | javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE,
                null, ex);
    }
    //</editor-fold>

    CameraWindow cWindow = new CameraWindow();
    cWindow.setVisible(true);

    int radius = 0;
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    //intialization of matrices
    Mat circles = new Mat();
    gray = new Mat();
    hsv = new Mat();
    filter = new Mat();
    dst = new Mat();

    camera = new VideoCapture(0);
    Mat frame = new Mat();
    Webcam.ImagePanel panel = Webcam.createPanel(camera, "src");
    Webcam.ImagePanel panel2 = Webcam.createPanel(camera, "filter");
    Webcam.ImagePanel panel3 = Webcam.createPanel(camera, "dst");

    while (true) {

        camera.read(frame);
        src = frame;

        GaussianBlur(src, src, new Size(3, 3), 2, 2);
        Imgproc.cvtColor(src, hsv, Imgproc.COLOR_BGR2HSV);
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);

        Core.inRange(gray, new Scalar(20, 100, 100), new Scalar(30, 255, 255), gray);
        Core.inRange(hsv, new Scalar(cWindow.get_hLower(), cWindow.get_sLower(), cWindow.get_vLower()),
                new Scalar(cWindow.get_hUpper(), cWindow.get_sUpper(), cWindow.get_vUpper()), filter);

        Core.inRange(src, new Scalar(cWindow.get_hLower(), cWindow.get_sLower(), cWindow.get_vLower()),
                new Scalar(cWindow.get_hUpper(), cWindow.get_sUpper(), cWindow.get_vUpper()), dst);

        double[] temp = hsv.get(hsv.rows() / 2, hsv.cols() / 2);
        System.out.println(temp[0] + ", " + temp[1] + ", " + temp[2] + ", " + radius);
        //System.out.println("Current Distance from ball: " + ((2.5366*radius) - 123.02));

        Imgproc.HoughCircles(filter, circles, CV_HOUGH_GRADIENT, cWindow.get_dp(), filter.rows() / 2,
                cWindow.get_param1(), cWindow.get_param2(), cWindow.get_minCircleSize(),
                cWindow.get_maxCircleSize());

        for (int i = 0; i < circles.cols(); i++) {
            Point center = new Point(Math.round(circles.get(0, i)[0]), Math.round(circles.get(0, i)[1]));
            radius = (int) Math.round(circles.get(0, i)[2]);
            // draw the circle center
            Core.circle(src, center, 3, new Scalar(0, 255, 0), -1, 8, 0);
            // draw the circle outline
            Core.circle(src, center, radius, new Scalar(0, 0, 255), 3, 8, 0);
            //System.out.println("" + circles.get(0,0)[0] + ", " + circles.get(0,0)[1] + ", " + circles.get(0,0)[2]);
        }

        panel.updateImage(toBufferedImage(src));
        panel2.updateImage(toBufferedImage(filter));
        panel3.updateImage(toBufferedImage(dst));
    }
}

From source file:bollettini.BullettinCompiler.java

private void writeCC(String CC) {
    Point CC1 = new Point(415, 73);
    Point CC2 = new Point(890, 73);
    Point CC3 = new Point(1425, 73);

    for (int i = 0; i < 12 && i < CC.length(); i++) {
        Core.putText(bullettin, String.valueOf(CC.charAt(CC.length() - i - 1)), CC1, Core.FONT_HERSHEY_COMPLEX,
                0.8, new Scalar(0, 0, 0));
        CC1.x -= 25;//w w w . j a v  a  2 s .co  m

        Core.putText(bullettin, String.valueOf(CC.charAt(CC.length() - i - 1)), CC2, Core.FONT_HERSHEY_COMPLEX,
                0.8, new Scalar(0, 0, 0));
        CC2.x -= 25;

        Core.putText(bullettin, String.valueOf(CC.charAt(CC.length() - i - 1)), CC3, Core.FONT_HERSHEY_COMPLEX,
                0.8, new Scalar(0, 0, 0));
        CC3.x -= 25;
    }
}

From source file:bollettini.BullettinCompiler.java

private void writeE(String E, String EL) {
    E = E.replace('.', ',');
    String part[] = E.split(",");

    Point E1 = new Point(315, 125);
    Point E2 = new Point(790, 125);
    Point E3 = new Point(1795, 73);

    for (int i = 0; i < 8 && i < part[0].length(); i++) {
        Core.putText(bullettin, String.valueOf(part[0].charAt(part[0].length() - i - 1)), E1,
                Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
        E1.x -= 25;//from www.ja  va2s .c o  m

        Core.putText(bullettin, String.valueOf(part[0].charAt(part[0].length() - i - 1)), E2,
                Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
        E2.x -= 25;

        Core.putText(bullettin, String.valueOf(part[0].charAt(part[0].length() - i - 1)), E3,
                Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
        E3.x -= 25;
    }

    Point E12 = new Point(345, 125);
    Point E22 = new Point(820, 125);
    Point E32 = new Point(1825, 73);
    if (part.length == 1) {
        String temp[] = new String[2];
        temp[0] = part[0];
        temp[1] = "00";
        part = temp;
    } else if (part[1].length() == 1) {
        part[1] = part[1] + "0";
    } else if (part[1].length() > 2) {
        String temp = String.valueOf(part[1].charAt(0)) + String.valueOf(part[1].charAt(1));
        part[1] = temp;
    }

    for (int i = 0; i < 2; i++) {
        Core.putText(bullettin, String.valueOf(part[1].charAt(i)), E12, Core.FONT_HERSHEY_COMPLEX, 0.8,
                new Scalar(0, 0, 0));
        E12.x += 25;

        Core.putText(bullettin, String.valueOf(part[1].charAt(i)), E22, Core.FONT_HERSHEY_COMPLEX, 0.8,
                new Scalar(0, 0, 0));
        E22.x += 25;

        Core.putText(bullettin, String.valueOf(part[1].charAt(i)), E32, Core.FONT_HERSHEY_COMPLEX, 0.8,
                new Scalar(0, 0, 0));
        E32.x += 25;
    }

    String temp = "";
    for (int i = 0; i < EL.length(); i++) {
        if (EL.charAt(i) != '/') {
            temp += String.valueOf(EL.charAt(i));
        } else {
            i = EL.length();
        }
    }

    EL = temp + "/" + part[1];

    this.writeELetter(EL);
}

From source file:bollettini.BullettinCompiler.java

private void writeELetter(String ELetter) {
    Point L1 = new Point(80, 160);
    Point L2 = new Point(555, 160);
    Point L3 = new Point(1240, 112);
    Core.putText(bullettin, ELetter, L1, Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
    Core.putText(bullettin, ELetter, L2, Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
    Core.putText(bullettin, ELetter, L3, Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
}

From source file:bollettini.BullettinCompiler.java

private void writeReciever(String reciever) {
    //massimo 24 caratteri
    String temp = "";
    int offset = 26;
    if (reciever.length() > offset) {
        temp = reciever.substring(0, offset);
    } else {/* ww w .ja  va 2  s  .  c o m*/
        temp = reciever;
    }
    Core.putText(bullettin, temp, new Point(93, 188), Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
    Core.putText(bullettin, temp, new Point(567, 188), Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));

    Point R = new Point(1000, 159);
    for (int i = 0; i < 68 && i < reciever.length(); i++) {
        Core.putText(bullettin, String.valueOf(reciever.charAt(i)), R, Core.FONT_HERSHEY_COMPLEX, 0.8,
                new Scalar(0, 0, 0));
        R.x += 25.3;
        if (i == 33)
            R.set(new double[] { 1000, 190 });
    }
}

From source file:bollettini.BullettinCompiler.java

private void writeCausal(String purpose) {
    Core.putText(bullettin, purpose, new Point(17, 237), Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
    Core.putText(bullettin, purpose, new Point(490, 237), Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
    Core.putText(bullettin, purpose, new Point(996, 237), Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
}

From source file:bollettini.BullettinCompiler.java

private void writePerformer(String performer) {
    String temp = "";
    int offset = 24;
    if (performer.length() > offset) {
        temp = performer.substring(0, offset);
    } else {//from   w  w  w.ja  v a2s.c o m
        temp = performer;
    }
    Core.putText(bullettin, temp, new Point(95, 500), Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));
    Core.putText(bullettin, temp, new Point(569, 500), Core.FONT_HERSHEY_COMPLEX, 0.8, new Scalar(0, 0, 0));

    Point A = new Point(1278, 317);
    for (int i = 0; i < 46 && i < performer.length(); i++) {
        Core.putText(bullettin, String.valueOf(performer.charAt(i)), A, Core.FONT_HERSHEY_COMPLEX, 0.8,
                new Scalar(0, 0, 0));
        A.x += 25;
        if (i == 22)
            A.set(new double[] { 1278, 348 });
    }
}