Example usage for org.opencv.core Core sortIdx

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

Introduction

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

Prototype

public static void sortIdx(Mat src, Mat dst, int flags) 

Source Link

Usage

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

License:Open Source License

public String recognize(Mat img, String expectedLabel) {
    // Ignore/*w w w .  j  a  v a  2  s.c o  m*/
    img = img.reshape(1, 1);
    // Subtract mean
    img.convertTo(img, CvType.CV_32F);
    Core.subtract(img, Psi, img);
    // Project to subspace
    Mat projected = getFeatureVector(img);
    // Save all points of image for tSNE
    img.convertTo(img, CvType.CV_8U);
    addImage(projected, expectedLabel, true);
    //addImage(projected, expectedLabel);
    Mat distance = new Mat(Omega.rows(), 1, CvType.CV_64FC1);
    for (int i = 0; i < Omega.rows(); i++) {
        double dist = Core.norm(projected.row(0), Omega.row(i), Core.NORM_L2);
        distance.put(i, 0, dist);
    }
    Mat sortedDist = new Mat(Omega.rows(), 1, CvType.CV_8UC1);
    Core.sortIdx(distance, sortedDist, Core.SORT_EVERY_COLUMN + Core.SORT_ASCENDING);
    // Give back the name of the found person
    int index = (int) (sortedDist.get(0, 0)[0]);
    return labelMap.getKey(labelList.get(index));
}

From source file:karthik.Barcode.MatrixBarcode.java

License:Open Source License

private Mat calcProbabilityTilings(int tileSize) {
    // calculates probability of each tile being in a 2D barcode region
    // tiles must be square
    assert (searchParams.RECT_HEIGHT == searchParams.RECT_WIDTH) : "RECT_HEIGHT and RECT_WIDTH must be equal in searchParams imageSpecificParams";

    int probMatTileSize = (int) (tileSize * (searchParams.PROB_MAT_TILE_SIZE / (1.0 * searchParams.tileSize)));
    int threshold_min_gradient_edges = (int) (tileSize * tileSize
            * searchParams.THRESHOLD_MIN_GRADIENT_EDGES_MULTIPLIER);

    int right_col, bottom_row;
    int prob_mat_right_col, prob_mat_bottom_row;

    Mat imgWindow; // used to hold sub-matrices from the image that represent the window around the current point
    Mat prob_window; // used to hold sub-matrices into probability matrix that represent window around current point

    int num_edges;
    double prob;/*from w ww. j a  v  a 2 s  .c  o  m*/
    int max_angle_idx, second_highest_angle_index, max_angle_count, second_highest_angle_count, angle_diff;

    img_details.probabilities.setTo(ZERO_SCALAR);

    for (int i = 0, row_offset = 0; i < rows; i += tileSize, row_offset += probMatTileSize) {
        // first do bounds checking for bottom right of tiles

        bottom_row = java.lang.Math.min((i + tileSize), rows);
        prob_mat_bottom_row = java.lang.Math.min((row_offset + probMatTileSize), img_details.probMatRows);

        for (int j = 0, col_offset = 0; j < cols; j += tileSize, col_offset += probMatTileSize) {

            // then calculate the column locations of the rectangle and set them to -1 
            // if they are outside the matrix bounds                
            right_col = java.lang.Math.min((j + tileSize), cols);
            prob_mat_right_col = java.lang.Math.min((col_offset + probMatTileSize), img_details.probMatCols);

            // calculate number of edges in the tile using the already calculated integral image 
            num_edges = (int) calc_rect_sum(img_details.edgeDensity, img_details.histNumRows,
                    img_details.histNumCols, i, bottom_row, j, right_col);

            if (num_edges < threshold_min_gradient_edges)
                // if gradient density is below the threshold level, prob of matrix code in this tile is 0
                continue;

            for (int r = 0; r < img_details.bins; r++) {

                img_details.histArray[r] = (int) calc_rect_sum(img_details.histIntegralArrays[r],
                        img_details.histNumRows, img_details.histNumCols, i, bottom_row, j, right_col);
            }

            hist = Converters.vector_int_to_Mat(Arrays.asList(img_details.histArray));
            Core.sortIdx(hist, histIdx, Core.SORT_EVERY_COLUMN + Core.SORT_DESCENDING);

            max_angle_idx = (int) histIdx.get(0, 0)[0];
            max_angle_count = (int) hist.get(max_angle_idx, 0)[0];

            second_highest_angle_index = (int) histIdx.get(1, 0)[0];
            second_highest_angle_count = (int) hist.get(second_highest_angle_index, 0)[0];

            angle_diff = Math.abs(max_angle_idx - second_highest_angle_index);

            // formula below is modified from Szentandrasi, Herout, Dubska paper pp. 4
            prob = 0;
            if (angle_diff != 1) // ignores tiles where there is just noise between adjacent bins in the histogram
                prob = 2.0 * Math.min(max_angle_count, second_highest_angle_count)
                        / (max_angle_count + second_highest_angle_count);

            prob_window = img_details.probabilities.submat(row_offset, prob_mat_bottom_row, col_offset,
                    prob_mat_right_col);
            prob_window.setTo(new Scalar((int) (prob * 255)));

        } // for j
    } // for i

    return img_details.probabilities;

}