Example usage for org.opencv.core Core PCACompute

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

Introduction

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

Prototype

public static void PCACompute(Mat data, Mat mean, Mat eigenvectors) 

Source Link

Usage

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  ww  w .j ava 2s. c  o  m*/
 * 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;
}