Example usage for org.opencv.core Core PCAProject

List of usage examples for org.opencv.core Core PCAProject

Introduction

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

Prototype

public static void PCAProject(Mat data, Mat mean, Mat eigenvectors, Mat result) 

Source Link

Usage

From source file:ch.zhaw.facerecognitionlibrary.Recognition.Eigenfaces.java

License:Open Source License

public Mat getFeatureVector(Mat original) {
    Mat projected = new Mat();
    Core.PCAProject(original, Psi, eigVectors, projected);
    return projected;
}

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

License:Open Source License

/**
 * Given a Mat object (data structure from OpenCV) with a face on it, 
 * it will try to find if the face is recognized from the data saved.
 * It applies a change in size to match the one needed.
 * /* w w w . j a  v  a 2s .  com*/
 * @return An integer that specifies which vector is recognized with the given Mat
 * */
public AlgorithmReturnValue recognizeFace(Mat face) {
    if (numImages < 2) {
        return new AlgorithmReturnValue();
    }
    Imgproc.resize(face, face, imageSize); //Size must be equal to the size of the saved faces 

    Mat analyze = new Mat(1, imgLength, CvType.CV_32FC1);
    Mat X = analyze.row(0);
    try {
        face.reshape(1, 1).convertTo(X, CvType.CV_32FC1);
    } catch (CvException e) {
        return new AlgorithmReturnValue();
    }
    Mat res = new Mat();
    Core.PCAProject(analyze, average, eigenfaces, res);
    return findClosest(res);
}

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

License:Open Source License

/**
 * It has no input, it will add the last image (when numerically ordered)
 * to the array of images and calculate the new PCA subspace.
 * //from   w w w  . jav  a 2 s .com
 * PCA won't work properly if newimage is true.
 * 
 * @return A boolean that specifies if everything went fine.
 * */
public boolean updateData(boolean newimage) {
    if (newimage) { //There is some error with this code, if newimage is true.
        //Probably it is the matrix.create() function. Later when PCA is done, the projection will be wrong.
        //So this code is never used at the moment, and newimage should be used as false always.
        //It uses more instructions, but until a solution is found it must stay as it is.
        numImages++;
        try {
            File directory = new File(imagesDir);
            if (!directory.exists()) {
                throw new IOException("Path to file could not be opened.");
            }
            String lfile = imagesDir + "/Face" + (numImages - 1) + ".png";
            Mat img = Highgui.imread(lfile, 0);
            if (img.empty())
                throw new IOException("Opening image number " + (numImages - 1) + " failed.");
            //we adapt the old matrices to new sizes
            sum.create(numImages, imgLength, CvType.CV_32FC1);
            projectedTraining.create(numImages, numImages, CvType.CV_32FC1);

            //and add the new image to the array of images
            img.reshape(1, 1).convertTo(sum.row(numImages - 1), CvType.CV_32FC1);

        } catch (IOException e) {
            System.err.println(e.getMessage());
            return false;
        }
    } else {
        numImages = KeyFaceActivity.prefs.getInt("savedFaces", numImages);
        sum = new Mat(numImages, imgLength, CvType.CV_32FC1);
        projectedTraining = new Mat(numImages, numImages, CvType.CV_32FC1);

        for (int i = 0; i < numImages; i++) { //opens each image and appends it as a column in the matrix Sum
            String lfile = imagesDir + "/Face" + i + ".png";
            try {
                Mat img = Highgui.imread(lfile, 0);
                //Other way of loading image data
                //Mat img = Utils.bitmapToMat(BitmapFactory.decodeFile(lfile));
                if (img.empty())
                    throw new IOException("Opening image number " + i + " failed.");
                //We add the image to the correspondent row in the matrix of images (sum)
                img.reshape(1, 1).convertTo(sum.row(i), CvType.CV_32FC1);
            } catch (IOException e) {
                System.err.println(e.getMessage());
                return false;
            }
        }
    }

    if (numImages > 1) {
        average = new Mat();
        eigenfaces = new Mat();
        Core.PCACompute(sum, average, eigenfaces);
        for (int i = 0; i < numImages; i++) {
            Core.PCAProject(sum.row(i), average, eigenfaces, projectedTraining.row(i));
        }
    }

    return true;
}