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, double v3) 

Source Link

Usage

From source file:com.joravasal.keyface.FindFacesView.java

License:Open Source License

@Override
protected Bitmap processFrame(VideoCapture camera) {
    //Log.i(tag,"Processing frame for our delight");

    Mat mRgbaAux = new Mat();
    Mat mGrayAux = new Mat();
    camera.retrieve(mRgbaAux, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
    camera.retrieve(mGrayAux, Highgui.CV_CAP_ANDROID_GREY_FRAME);
    //Correct the direction of the image
    mRgba = correctCameraImage(mRgbaAux);
    mGray = correctCameraImage(mGrayAux);

    AlgorithmReturnValue resExample = null;
    //We look for faces in the captured images
    if (cascade != null) {
        int faceSize = Math.round(mGray.rows() * KeyFaceActivity.minFaceSize);
        List<Rect> faces = new LinkedList<Rect>();
        try {//ww w.j  a va 2s  . co  m
            cascade.detectMultiScale(mGray, faces, 1.1, 2, 2, new Size(faceSize, faceSize));
        } catch (CvException e) {
            System.err.println(e.getMessage());
        }
        for (Rect r : faces) { //For each face

            //The Rectangle commented is the area that will be used to check the face,
            //but an ellipse is shown instead, I think it looks better.
            //Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0,0,255,100), 3);

            String nombre = null;

            // We try to recognize it
            AlgorithmReturnValue res = KeyFaceActivity.recogAlgorithm.recognizeFace(mGray.submat(r));
            resExample = res;
            if (res.getResult() != -1) {
                //if it worked, we find the name
                nombre = findName(res.getResult());
            }
            Point center = new Point(r.x + (r.width / 2), r.y + (r.height / 2));
            //If nombre is null we have no name, thus is unrecognized and draw a red circle, together with the text "Unknown"
            if (nombre == null) {
                Core.ellipse(mRgba, center, new Size(r.width / 2 - 5, r.height / 2 + 20), 0, 0, 360,
                        new Scalar(255, 0, 0, 30), 3);
                Core.rectangle(mRgba, new Point(r.x + 45, r.y + r.height + 20),
                        new Point(r.x + 200, r.y + r.height + 60), new Scalar(70, 50, 50, 255), Core.FILLED);
                Core.putText(mRgba, "Unknown", new Point(r.x + 50, r.y + r.height + 50),
                        Core.FONT_HERSHEY_PLAIN, 2, new Scalar(200, 200, 200, 100));

                //Check if the user is tryaing to save a new face
                if (KeyFaceActivity.addingFaces && faces.size() == 1) {
                    //All is in order, we save a new image and update our account of faces. We update the recognizer data as well.
                    addFaceToDB(mGray, r, savedFaces);

                    KeyFaceActivity.toastHandler.post(new Runnable() {
                        public void run() {
                            KeyFaceActivity.prefs.edit()
                                    .putInt("savedFaces", KeyFaceActivity.prefs.getInt("savedFaces", 0) + 1)
                                    .apply();
                        }
                    });

                    /*KeyFaceActivity.lock.lock();
                    try {
                       KeyFaceActivity.faceAdded = true;
                       KeyFaceActivity.addingFaces = false;
                       KeyFaceActivity.condition.signalAll();
                    }
                    finally {
                       KeyFaceActivity.lock.unlock();
                    }
                    */

                    if (!KeyFaceActivity.recogAlgorithm.updateData(false)) {
                        System.err.println("Couldn't update the recognition algorithm with the new picture.");
                    }
                    KeyFaceActivity.addingFaces = false;

                    KeyFaceActivity.toastHandler.post(new Runnable() {
                        public void run() {
                            Toast.makeText(KeyFaceActivity.globalappcontext, "Face saved successfully!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                //The user tried to save a face when there was more than one, it fails and sends a message to the user.
                else if (KeyFaceActivity.addingFaces && faces.size() > 1) {
                    KeyFaceActivity.toastHandler.post(new Runnable() {
                        public void run() {
                            Toast.makeText(KeyFaceActivity.globalappcontext,
                                    "Make sure there is only one face!", Toast.LENGTH_SHORT).show();
                        }
                    });
                    KeyFaceActivity.addingFaces = false;
                }
            }

            else { //We know this face!
                Core.ellipse(mRgba, center, new Size(r.width / 2 - 5, r.height / 2 + 20), 0, 0, 360,
                        new Scalar(0, 255, 0, 100), 3);
                Core.rectangle(mRgba, new Point(r.x + 45, r.y + r.height + 20),
                        new Point(r.x + 200, r.y + r.height + 60), new Scalar(50, 70, 50, 255), Core.FILLED);
                Core.putText(mRgba, nombre, new Point(r.x + 50, r.y + r.height + 50), Core.FONT_HERSHEY_PLAIN,
                        2, new Scalar(0, 255, 0, 100));
                if (KeyFaceActivity.addingFaces && faces.size() == 1) {
                    //If the user tries to save a face when it is already known we don let him.
                    KeyFaceActivity.toastHandler.post(new Runnable() {
                        public void run() {
                            Toast.makeText(KeyFaceActivity.globalappcontext, "This face is already known!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                    KeyFaceActivity.addingFaces = false;
                }
            }
        }
        //If there is no face we tell the user there was a mistake
        if (KeyFaceActivity.addingFaces && faces.size() <= 0) {
            KeyFaceActivity.toastHandler.post(new Runnable() {
                public void run() {
                    Toast.makeText(KeyFaceActivity.globalappcontext, "No face found!", Toast.LENGTH_SHORT)
                            .show();
                }
            });
            KeyFaceActivity.addingFaces = false;
        }
    }

    savedFaces = KeyFaceActivity.prefs.getInt("savedFaces", savedFaces);

    if (KeyFaceActivity.prefs.getBoolean("showData", false)) {
        try {
            if (resExample != null) {
                //background rectangle for extra info on PCA
                Core.rectangle(mRgba, new Point(0, mRgba.height() - 100),
                        new Point(mRgba.width(), mRgba.height()), new Scalar(50, 50, 50, 50), Core.FILLED);
                //Data for closest image 
                Core.putText(mRgba, "1st", new Point(5, mRgba.height() - 80), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
                Core.putText(mRgba, Integer.toString(resExample.getClosestImage()),
                        new Point(5, mRgba.height() - 55), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
                Core.putText(mRgba, Double.toString(resExample.getDistClosestImage() / 100000).substring(0, 6),
                        new Point(5, mRgba.height() - 30), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
                //Data for second closest image
                Core.putText(mRgba, "2nd", new Point(180, mRgba.height() - 80), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
                Core.putText(mRgba, Integer.toString(resExample.getSecondClosestImage()),
                        new Point(180, mRgba.height() - 55), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
                Core.putText(mRgba,
                        Double.toString(resExample.getDistSecondClosestImage() / 100000).substring(0, 6),
                        new Point(180, mRgba.height() - 30), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
                //Data for farthest image
                Core.putText(mRgba, "Last", new Point(355, mRgba.height() - 80), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
                Core.putText(mRgba, Integer.toString(resExample.getFarthestImage()),
                        new Point(355, mRgba.height() - 55), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
                Core.putText(mRgba, Double.toString(resExample.getDistFarthestImage() / 100000).substring(0, 6),
                        new Point(355, mRgba.height() - 30), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
                //Num images and threshold
                Core.putText(mRgba, "Images:" + savedFaces, new Point(15, mRgba.height() - 5),
                        Core.FONT_HERSHEY_PLAIN, 2, new Scalar(250, 250, 250, 200));
                Core.putText(mRgba,
                        "Th:" + Double.toString(resExample.getThreshold() / 100000).substring(0,
                                Math.min(6, Double.toString(resExample.getThreshold() / 100000).length())),
                        new Point(240, mRgba.height() - 5), Core.FONT_HERSHEY_PLAIN, 2,
                        new Scalar(250, 250, 250, 200));
            } else {
                Core.rectangle(mRgba, new Point(0, mRgba.height() - 30), new Point(200, mRgba.height()),
                        new Scalar(50, 50, 50, 50), Core.FILLED);
                Core.putText(mRgba, "Images:" + savedFaces, new Point(15, mRgba.height() - 5),
                        Core.FONT_HERSHEY_PLAIN, 2, new Scalar(250, 250, 250, 200));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);

    if (Utils.matToBitmap(mRgba, bmp))
        return bmp;

    bmp.recycle();
    return null;
}

From source file:com.orange.documentare.core.image.segmentation.SegmentationDrawer.java

License:Open Source License

private void drawGlyphsSegmentation() {
    Scalar colorRect = new Scalar(255, 0, 0, 0);
    for (SegmentationRect rect : imageSegmentation.getDigitalTypes()) {
        Point tl = new Point(rect.x(), rect.y());
        Point br = new Point(tl.x + rect.width(), tl.y + rect.height());
        Core.rectangle(imageMat, tl, br, colorRect, 1);
    }/*from  www  .  ja v a  2s .  c om*/
}

From source file:com.orange.documentare.core.image.segmentation.SegmentationDrawer.java

License:Open Source License

private void drawBlocksSegmentation() {
    Scalar colorLine = new Scalar(0, 0, 255, 0);
    for (Block block : imageSegmentation.getBlocks()) {
        Core.rectangle(imageMat, new Point(block.x(), block.y() - 1),
                new Point(block.x() + block.width(), block.y() + block.height() + 1), colorLine, 1);
    }/*from ww  w  . j a v  a2 s.com*/
}

From source file:com.orange.documentare.core.image.segmentation.SegmentationDrawer.java

License:Open Source License

private void drawSpace(DigitalType r) {
    Scalar colorSpace = new Scalar(64, 64, 64, 0);
    Core.rectangle(imageMat, new Point(r.x() + 1, r.y()), new Point(r.x() + r.width() - 1, r.y() + r.height()),
            colorSpace, -1);/*from  www  . j a  v  a 2 s.c  om*/
}

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 w  w  w  . j ava  2s.  co 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.raulh82vlc.face_detection_sample.opencv.render.FaceDrawerOpenCV.java

License:Apache License

public static void drawIrisCircle(Mat matrixRgba, Core.MinMaxLocResult minMaxLocResult) {
    Imgproc.circle(matrixRgba, minMaxLocResult.minLoc, 2, new Scalar(255, 255, 255, 255), 2);
}

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

License:Apache License

public static void drawMatchedEye(Point matchLocTx, Point matchLocTy, Mat matrixRgba) {
    Imgproc.rectangle(matrixRgba, matchLocTx, matchLocTy, new Scalar(255, 255, 0, 255));
}

From source file:eu.fpetersen.robobrain.behavior.followobject.ColorBlobDetector.java

License:Open Source License

public ColorBlobDetector(Scalar hsvColor) {
    CONTOUR_COLOR = new Scalar(255, 0, 0, 255);
    setHsvColor(hsvColor);
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * //from  w w w. j  a v a 2  s  .  c  om
 * Adjust the contrast of the image. Works on color or black and white images.
 * 
 * @param amt
 *          Amount of contrast to apply. 0-1.0 reduces contrast. Above 1.0 increases contrast.
 * 
 **/
public void contrast(float amt) {
    Scalar modifier;
    if (useColor) {
        modifier = new Scalar(amt, amt, amt, 1);

    } else {
        modifier = new Scalar(amt);
    }

    Core.multiply(getCurrentMat(), modifier, getCurrentMat());
}