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:Reconhecimento.SaltoSegmentacaoCirculo.java

private static Point segmentarCirculo(Mat img) {
    Mat grayImg = new Mat();
    Mat circles = new Mat();
    Mat element = new Mat();

    Imgproc.cvtColor(img, grayImg, Imgproc.COLOR_BGR2GRAY);

    // filtro da mediana
    Imgproc.medianBlur(grayImg, grayImg, 5);

    // transformada circular de hough
    Imgproc.HoughCircles(grayImg, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 100, 220, 17, 5, 10);

    Point centro = new Point(0, 0);
    Point center;//from w  ww.  j a v  a2 s .c  om

    for (int x = 0; x < circles.cols(); x++) {
        double vCircle[] = circles.get(0, x);

        center = new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));

        // pegar o circulo mais embaixo
        if (centro.y < center.y) {
            centro = center.clone();
        }

        int radius = (int) Math.round(vCircle[2]);

        Core.circle(img, center, radius, new Scalar(0, 0, 255), 3, 8, 0);
    }
    return centro;
}

From source file:rfea.ProcesarImagen.java

public void DetectarRostro() {
    System.out.println("\nDetectando rostros");

    // Create a face detector from the cascade file in the resources
    // directory.

    // System.out.println(rutaDe("/recursos/haarcascade_frontalface_alt.xml"));
    CascadeClassifier faceDetector = new CascadeClassifier(rutaDe("/recursos/haarcascade_frontalface_alt.xml"));

    ////from   www . j av  a 2 s.com

    image = Highgui.imread(imagenEntrada);
    Mat temp = new Mat();
    Imgproc.cvtColor(image, temp, Imgproc.COLOR_BGRA2GRAY, 0);
    Mat temp_rgba = new Mat();
    Imgproc.cvtColor(temp, temp_rgba, Imgproc.COLOR_GRAY2BGRA, 0);
    temp_rgba.copyTo(image);
    temp.copyTo(image);
    equalizeHist(image, image);

    tmp = image;
    //    

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);
    contRostros = faceDetections.toArray().length;
    if (contRostros < 1) {
        System.out
                .println("No se an detectado rostros, profavor acerquese a la camara en una posicion adecuada");
        return;
    } else if (contRostros > 1) {
        System.out.println("se dectactaron mas de 1 rostros, seleecione 1 para evaluar");
        for (int i = 0; i < faceDetections.toArray().length; i++) {

            roiRostro = faceDetections.toArray()[i];
            Core.rectangle(image, new Point(roiRostro.x, roiRostro.y),
                    new Point(roiRostro.x + roiRostro.width, roiRostro.y + roiRostro.height),
                    new Scalar(0, 255, 0));
        }
        Principal.setRostro(image);
        new SeleccionImg().setVisible(true);

    } else {

        System.out.println(String.format("%s rostro Detectado, en la iteracion: " + cont,
                faceDetections.toArray().length));

        // Draw a bounding box around each face.
        //for (Rect roiRostro : faceDetections.toArray()) {
        for (int i = 0; i < faceDetections.toArray().length; i++) {

            roiRostro = faceDetections.toArray()[i];
            Core.rectangle(image, new Point(roiRostro.x, roiRostro.y),
                    new Point(roiRostro.x + roiRostro.width, roiRostro.y + roiRostro.height),
                    new Scalar(0, 255, 0));
        }

        // Save the visualized detection.
        String filename = imagenSalida;
        System.out.println(String.format("Guardado %s", filename));
        Highgui.imwrite(filename, image);
        recortarRostro();
        this.detectarOjos();
        this.detectarNariz();
        detectarBoca();
    }

}

From source file:rfea.ProcesarImagen.java

public void detectarOjos() {
    System.out.println("\nDetectando Ojos");

    // Create a face detector from the cascade file in the resources
    // directory.

    //System.out.println(rutaDe("/recursos/haarcascade_eye_tree_eyeglasses.xml"));
    //CascadeClassifier faceDetector = new CascadeClassifier(rutaDe("/recursos/haarcascade_eye.xml"));
    CascadeClassifier faceDetector = new CascadeClassifier(rutaDe("/recursos/haarcascade_eye.xml"));

    ///*from www .  j  a v a2s  .  c om*/

    //    image = Highgui.imread(imagenEntrada);
    tmp = null;
    tmp = imgRostro.clone();

    //    

    //vector<Rect> leftEye, rightEye;

    int leftX = (int) Math.round(tmp.cols() * EYE_SX);
    int topY = (int) Math.round(tmp.rows() * EYE_SY);
    int widthX = (int) Math.round(tmp.cols() * EYE_SW);
    int heightY = (int) Math.round(tmp.rows() * EYE_SH);
    int rightX = (int) Math.round(tmp.cols() * (1.0 - EYE_SX - EYE_SW));
    //Mat topLeftOfFace = tmp.clone().submat(new Rect(leftX, topY, widthX, heightY));
    //Mat topRightOfFace = tmp.clone().submat(new Rect(rightX, topY, widthX, heightY));
    //Imgproc.cvGetSize();
    System.out.println("columnas: " + tmp.cols() + " filas: " + tmp.rows());
    System.out.println("leftx: " + leftX + " topy: " + topY + " anchox: " + widthX + " alto: " + heightY
            + " rightx" + rightX);
    Mat topLeftOfFace = tmp.clone().submat(new Rect(leftX, topY, widthX, heightY));
    Mat topRightOfFace = tmp.clone().submat(new Rect(rightX, topY, widthX, heightY));

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect deteccionOjos = new MatOfRect();
    faceDetector.detectMultiScale(topLeftOfFace, deteccionOjos);
    faceDetector.detectMultiScale(topRightOfFace, deteccionOjos);
    contRostros = deteccionOjos.toArray().length;
    if (contRostros < 1 && cont < 3) {
        cont++;
        detectarOjos();
        return;
    }

    System.out.println(
            String.format("%s ojos Detectado, en la iteracion: " + cont, deteccionOjos.toArray().length));

    // Draw a bounding box around each face.
    //for (Rect roiRostro : faceDetections.toArray()) {
    for (int i = 0; i < deteccionOjos.toArray().length; i++) {

        roiOjos[i] = deteccionOjos.toArray()[i];
        switch (i) {
        case 0:
            Core.rectangle(topLeftOfFace, new Point(roiOjos[i].x, roiOjos[i].y),
                    new Point(roiOjos[i].x + roiOjos[i].width, roiOjos[i].y + roiOjos[i].height),
                    new Scalar(0, 255, 0));
            Core.rectangle(tmp, new Point(roiOjos[i].x + leftX, roiOjos[i].y + topY),
                    new Point(roiOjos[i].x + roiOjos[i].width + leftX, roiOjos[i].y + roiOjos[i].height + topY),
                    new Scalar(0, 255, 0));
            String filename = "ojos_0.png";
            System.out.println(String.format("Guardado %s", filename));
            Highgui.imwrite(filename, topLeftOfFace);
            break;
        case 1:
            Core.rectangle(topRightOfFace, new Point(roiOjos[i].x - (roiOjos[i].width / 2), roiOjos[i].y),
                    new Point(roiOjos[i].x + roiOjos[i].width, roiOjos[i].y + roiOjos[i].height),
                    new Scalar(0, 255, 0));
            Core.rectangle(tmp, new Point(roiOjos[i].x + rightX - (roiOjos[i].width / 2), roiOjos[i].y + topY),
                    new Point(roiOjos[i].x + roiOjos[i].width + rightX - (roiOjos[i].width / 2),
                            roiOjos[i].y + roiOjos[i].height + topY),
                    new Scalar(0, 255, 0));

            filename = "ojos_1.png";
            System.out.println(String.format("Guardado %s", filename));
            Highgui.imwrite(filename, topRightOfFace);
        default:

        }

    }

    // Save the visualized detection.
    String filename = "ojos.png";
    System.out.println(String.format("Guardado %s", filename));
    Highgui.imwrite(filename, tmp);
    // recortaOjos();

}

From source file:rfea.ProcesarImagen.java

public void detectarBoca() {//haarcascade_mcs_nose.xml
    System.out.println("\nDetectando Boca");
    int contboc = 0;

    // Create a face detector from the cascade file in the resources
    // directory.

    //System.out.println(rutaDe("/recursos/haarcascade_eye.xml"));
    CascadeClassifier faceDetector = new CascadeClassifier(rutaDe("/recursos/haarcascade_mcs_mouth.xml"));

    ////  www .  j  a v a2  s. c om

    //    image = Highgui.imread(imagenEntrada);
    tmp = null;
    tmp = imgRostro.clone();
    /*int bX,bY,bTopX,bTopY;
    int startX = (int)Math.round(tmp.cols() * 0.25);
    int startY = (int)Math.round((tmp.rows()/2) + (tmp.rows()/2)*0.25);
    int widthX = (int)Math.round(tmp.cols() - startX);
    int heightY = (int)Math.round(tmp.rows() - (tmp.rows()/2)*0.25);
    System.out.println("columnas: "+tmp.cols()+" filas: "+tmp.rows());
    System.out.println("ancho: "+tmp.width()+" filas: "+tmp.height());
    System.out.println("x: "+startX+" y: "+startY+" anchox: "+widthX+" alto: "+heightY);
    Mat regionBoca = tmp.clone().submat(startX, startY, widthX, heightY);*/
    //    

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect deteccionBoca = new MatOfRect();
    faceDetector.detectMultiScale(tmp, deteccionBoca);
    contRostros = deteccionBoca.toArray().length;

    // Draw a bounding box around each face.
    //for (Rect roiRostro : faceDetections.toArray()) {
    for (int i = 0; i < deteccionBoca.toArray().length; i++) {

        if (deteccionBoca.toArray()[i].y > tmp.height() + (tmp.height() / 2)) {//tomala boca detectada en la region dela itad de la cara hacia abajo
            roiBoca = deteccionBoca.toArray()[i];
            contboc++;
            Core.rectangle(tmp, new Point(roiBoca.x, roiBoca.y),
                    new Point(roiBoca.x + roiBoca.width, roiBoca.y + roiBoca.height), new Scalar(0, 255, 0));
        }
    }
    System.out.println(String.format("%s Boca procesada, en la iteracion: " + cont, contboc));

    // Save the visualized detection.
    String filename = "boca.png";
    System.out.println(String.format("Guardado %s", filename));
    Highgui.imwrite(filename, tmp);
    recortaBoca();

}

From source file:rfea.ProcesarImagen.java

public void detectarNariz() {//
    System.out.println("\nDetectando Nariz");
    int contnar = 0;

    // Create a face detector from the cascade file in the resources
    // directory.

    //System.out.println(rutaDe("/recursos/haarcascade_eye.xml"));
    CascadeClassifier faceDetector = new CascadeClassifier(rutaDe("/recursos/haarcascade_mcs_nose.xml"));

    ////  ww  w  . j  a  va  2s .c o  m

    //    image = Highgui.imread(imagenEntrada);
    tmp = null;
    tmp = imgRostro.clone();

    //    

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect detectarNariz = new MatOfRect();
    faceDetector.detectMultiScale(tmp, detectarNariz);
    contRostros = detectarNariz.toArray().length;
    if (contRostros < 1 && cont < 3) {
        cont++;
        detectarOjos();
        return;
    }

    // Draw a bounding box around each face.
    //for (Rect roiRostro : faceDetections.toArray()) {
    for (int i = 0; i < detectarNariz.toArray().length; i++) {

        roiNariz = detectarNariz.toArray()[i];
        if (roiNariz.y > tmp.height() / 4) {//tomala boca detectada en la region del centro de la cara hacia abajo
            contnar++;
            Core.rectangle(tmp, new Point(roiNariz.x, roiNariz.y),
                    new Point(roiNariz.x + roiNariz.width, roiNariz.y + roiNariz.height),
                    new Scalar(0, 255, 0));
        }
    }
    System.out.println(String.format("%s Nariz Detectado, en la iteracion: " + cont, contnar));

    // Save the visualized detection.
    String filename = "nariz.png";
    System.out.println(String.format("Guardado %s", filename));
    Highgui.imwrite(filename, tmp);
    recortaNariz();

}

From source file:samples.DetectFaceDemo.java

public void run() {
    System.out.println("\nRunning DetectFaceDemo");

    // Create a face detector from the cascade file in the resources
    // directory.
    CascadeClassifier faceDetector = new CascadeClassifier("lbpcascade_frontalface.xml");
    Mat image = Imgcodecs.imread("lena1.png");

    // 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.
    int i = 0;/*from w  ww . j a v a2  s . co  m*/
    for (Rect rect : faceDetections.toArray()) {
        Mat croped = new Mat(image, rect);
        Imgcodecs.imwrite("face" + (++i) + ".png", croped);
        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));

    }

    // Save the visualized detection.
    String filename = "faceDetection.png";
    System.out.println(String.format("Writing %s", filename));
    Imgcodecs.imwrite(filename, image);
}

From source file:samples.GripPipeline.java

License:Open Source License

/**
 * Segment an image based on hue, saturation, and luminance ranges.
 *
 * @param input The image on which to perform the HSL threshold.
 * @param hue The min and max hue//www. j  a v a 2  s  .c  om
 * @param sat The min and max saturation
 * @param lum The min and max luminance
 * @param out The image in which to store the output.
 */
private void hslThreshold(Mat input, double[] hue, double[] sat, double[] lum, Mat out) {
    Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2HLS);
    Core.inRange(out, new Scalar(hue[0], lum[0], sat[0]), new Scalar(hue[1], lum[1], sat[1]), out);
}

From source file:samples.GripVision.java

License:Open Source License

/**
 * This method is called to detect objects in the acquired image frame.
 *
 * @param image specifies the image to be processed.
 * @param buffers specifies the preallocated buffer to hold the detected targets (not used since no
 *        preallocated buffer required).
 * @return detected objects, null if none detected.
 *//*w ww. j  av a2 s. c  om*/
@Override
public Rect[] detectObjects(Mat image, Rect[] buffers) {
    //
    // Process the image to detect the targets we are looking for and put them into targetRects.
    //
    synchronized (targetRects) {
        targetRects[0] = getTargetRect(gripRedTarget, image);
        targetRects[1] = getTargetRect(gripBlueTarget, image);

        if (videoOutEnabled) {
            drawRectangles(image, targetRects, new Scalar(0, 255, 0), 0);
        }
    }

    return targetRects;
}

From source file:samples.LWF.java

private static void save_meshed_images(double[][] puntos, File carpetaalmacen, File image, Mat mat,
        int[][] delaunay_triangles) {
    Mat mat_copy = mat.clone();/*from  w  ww .  ja v  a2s .c  om*/
    int radii = 1000;
    for (double[] punto : puntos) {
        Imgproc.ellipse(mat_copy, new Point(punto), new Size(radii, radii), 0, 0, 0, new Scalar(0, 255, 0));
        //            Imgproc.line(mat_copy, null, null, null);
    }
    for (int[] tri : faceTemplateTriangles) {
        Imgproc.line(mat_copy, new Point(puntos[tri[0] - 1]), new Point(puntos[tri[1] - 1]),
                new Scalar(0, 255, 0));
        Imgproc.line(mat_copy, new Point(puntos[tri[1] - 1]), new Point(puntos[tri[2] - 1]),
                new Scalar(0, 255, 0));
        Imgproc.line(mat_copy, new Point(puntos[tri[2] - 1]), new Point(puntos[tri[0] - 1]),
                new Scalar(0, 255, 0));
    }
    Imgcodecs.imwrite(carpetaalmacen.getAbsolutePath() + "\\" + image.getName(), mat_copy);
}

From source file:samples.LWF.java

private static void save_mesh_images(double[][] puntos, File carpetaalmacen, File image, Mat mat,
        int[][] delaunay_triangles) {
    Mat lienzo = new Mat(300, 300, CV_8UC3, new Scalar(0, 0, 0));
    Mat lienzo2 = new Mat(300, 300, CV_8UC3, new Scalar(0, 0, 0));
    double escala = 128;
    for (int[] tri : faceTemplateTriangles) {
        Imgproc.line(lienzo, new Point(escala * Shape3D[tri[0] - 1][0], escala * Shape3D[tri[0] - 1][1]),
                new Point(escala * Shape3D[tri[1] - 1][0], escala * Shape3D[tri[1] - 1][1]),
                new Scalar(0, 255, 0));
        Imgproc.line(lienzo, new Point(escala * Shape3D[tri[1] - 1][0], escala * Shape3D[tri[1] - 1][1]),
                new Point(escala * Shape3D[tri[2] - 1][0], escala * Shape3D[tri[2] - 1][1]),
                new Scalar(0, 255, 0));
        Imgproc.line(lienzo, new Point(escala * Shape3D[tri[2] - 1][0], escala * Shape3D[tri[2] - 1][1]),
                new Point(escala * Shape3D[tri[0] - 1][0], escala * Shape3D[tri[0] - 1][1]),
                new Scalar(0, 255, 0));

        Imgproc.line(lienzo2, new Point(escala * Shape3D[tri[0] - 1][2], escala * Shape3D[tri[0] - 1][1]),
                new Point(escala * Shape3D[tri[1] - 1][2], escala * Shape3D[tri[1] - 1][1]),
                new Scalar(0, 255, 0));
        Imgproc.line(lienzo2, new Point(escala * Shape3D[tri[1] - 1][2], escala * Shape3D[tri[1] - 1][1]),
                new Point(escala * Shape3D[tri[2] - 1][2], escala * Shape3D[tri[2] - 1][1]),
                new Scalar(0, 255, 0));
        Imgproc.line(lienzo2, new Point(escala * Shape3D[tri[2] - 1][2], escala * Shape3D[tri[2] - 1][1]),
                new Point(escala * Shape3D[tri[0] - 1][2], escala * Shape3D[tri[0] - 1][1]),
                new Scalar(0, 255, 0));

    }/*w  w  w.  j  a  v a 2 s.  c o  m*/
    Imgcodecs.imwrite(carpetaalmacen.getAbsolutePath() + "\\frontal_" + image.getName(), lienzo);
    Imgcodecs.imwrite(carpetaalmacen.getAbsolutePath() + "\\lateral_" + image.getName(), lienzo2);
}