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[] vals) 

Source Link

Usage

From source file:cx.uni.jk.mms.iaip.filter.LogOfOnePlusAbs.java

License:Open Source License

@Override
public Mat convert(Mat mat) {

    /** make absolute values and log */
    Mat tempMat = mat.clone();//from ww w .ja  v a2  s.c  o m
    Core.absdiff(tempMat, new Scalar(0.0d), tempMat);
    Core.add(tempMat, new Scalar(1.0d), tempMat);
    Core.log(tempMat, tempMat);

    /** find contrast and brightness to fit into 8 bit */
    MinMaxLocResult mmlr = Core.minMaxLoc(tempMat);
    double min = Math.min(mmlr.minVal, 0);
    double max = mmlr.maxVal;
    double alpha = 256.0d / (max - min);
    double beta = -min * alpha;

    /** conversion to 8 bit Mat applying contrast alpha and brightness beta */
    Mat byteMat = new MatOfByte();
    tempMat.convertTo(byteMat, CvType.CV_8U, alpha, beta);

    return byteMat;
}

From source file:cx.uni.jk.mms.iaip.filter.LogRedBlue.java

License:Open Source License

@Override
public Mat convert(Mat mat) {

    MinMaxLocResult negativeMmlr, positiveMmlr;
    double min, max, alpha, beta;

    /** negative values to positive and log */
    Mat negativeMat = mat.clone();/*from   w ww.j  ava  2s  .c o  m*/
    Core.min(negativeMat, new Scalar(0.0d), negativeMat);
    Core.multiply(negativeMat, new Scalar(-1.0d), negativeMat);
    Core.add(negativeMat, new Scalar(1.0d), negativeMat);
    Core.log(negativeMat, negativeMat);

    /** positve values log */
    Mat positiveMat = mat.clone();
    Core.max(positiveMat, new Scalar(0.0d), positiveMat);
    Core.add(positiveMat, new Scalar(1.0d), positiveMat);
    Core.log(positiveMat, positiveMat);

    /** find common contrast and brightness to fit into 8 bit */
    negativeMmlr = Core.minMaxLoc(negativeMat);
    positiveMmlr = Core.minMaxLoc(positiveMat);
    min = 0;
    max = Math.max(negativeMmlr.maxVal, positiveMmlr.maxVal);
    alpha = 256.0d / (max - min);
    beta = -min * alpha;

    /** conversion of both matrices to 8 bit */
    negativeMat.convertTo(negativeMat, CvType.CV_8UC1, alpha, beta);
    positiveMat.convertTo(positiveMat, CvType.CV_8UC1, alpha, beta);

    /** combine both matrices into one 8 bit 3 channel rgb picture */
    Mat tempMat = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC3);
    List<Mat> mixSrcMats = new ArrayList<>();
    mixSrcMats.add(negativeMat); // 1 channel: 0
    mixSrcMats.add(positiveMat); // 1 channel: 1
    List<Mat> mixDstMats = new ArrayList<>();
    mixDstMats.add(tempMat); // 3 channels: 0-2
    MatOfInt fromToMat = new MatOfInt(0, 0 /* neg->red */, -1, 1/*
                                                                * null->green
                                                                */, 1, 2 /*
                                                                          * pos-
                                                                          * >
                                                                          * blue
                                                                          */);
    Core.mixChannels(mixSrcMats, mixDstMats, fromToMat);

    return tempMat;
}

From source file:cx.uni.jk.mms.iaip.filter.LogYellowCyan.java

License:Open Source License

@Override
public Mat convert(Mat mat) {

    MinMaxLocResult negativeMmlr, positiveMmlr;
    double min, max, alpha, beta;

    /** negative values to positive and log */
    Mat negativeMat = mat.clone();//from w ww  .  ja  v a2 s.c  o m
    Core.min(negativeMat, new Scalar(0.0d), negativeMat);
    Core.multiply(negativeMat, new Scalar(-1.0d), negativeMat);
    Core.add(negativeMat, new Scalar(1.0d), negativeMat);
    Core.log(negativeMat, negativeMat);

    /** positve values log */
    Mat positiveMat = mat.clone();
    Core.max(positiveMat, new Scalar(0.0d), positiveMat);
    Core.add(positiveMat, new Scalar(1.0d), positiveMat);
    Core.log(positiveMat, positiveMat);

    /** find common contrast and brightness to fit into 8 bit */
    negativeMmlr = Core.minMaxLoc(negativeMat);
    positiveMmlr = Core.minMaxLoc(positiveMat);
    min = 0;
    max = Math.max(negativeMmlr.maxVal, positiveMmlr.maxVal);
    alpha = 256.0d / (max - min);
    beta = -min * alpha;

    /** conversion of both matrices to 8 bit */
    negativeMat.convertTo(negativeMat, CvType.CV_8UC1, alpha, beta);
    positiveMat.convertTo(positiveMat, CvType.CV_8UC1, alpha, beta);

    /** create additional mat for saturated green */
    Mat brightMat = negativeMat.clone();
    Core.max(negativeMat, positiveMat, brightMat);
    // Core.absdiff(brightMat, new Scalar(255.0d), brightMat);
    // Core.multiply(brightMat, new Scalar(1.0d/3.0d), brightMat);

    /** combine all matrices into one 8 bit 3 channel rgb picture */
    Mat tempMat = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC3);
    List<Mat> mixSrcMats = new ArrayList<>();
    mixSrcMats.add(negativeMat); // 1 channel: 0
    mixSrcMats.add(positiveMat); // 1 channel: 1
    mixSrcMats.add(brightMat); // 1 channel: 2
    List<Mat> mixDstMats = new ArrayList<>();
    mixDstMats.add(tempMat); // 3 channels: 0-2
    MatOfInt fromToMat = new MatOfInt(0, 0 /* neg->red */, 2, 1/*
                                                               * avg->green
                                                               */, 1, 2 /*
                                                                         * pos-
                                                                         * >
                                                                         * blue
                                                                         */);
    Core.mixChannels(mixSrcMats, mixDstMats, fromToMat);

    return tempMat;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * /*from   w  ww  .j ava2  s.c o  m*/
 * Filter the image for values between a lower and upper bound.
 * Converts the current image into a binary image with white where pixel
 * values were within bounds and black elsewhere.
 * 
 * @param lowerBound
 * @param upperBound
 */
public void inRange(int lowerBound, int upperBound) {
    Core.inRange(getCurrentMat(), new Scalar(lowerBound), new Scalar(upperBound), getCurrentMat());
}

From source file:imageprocess.HistogramProcessor.java

public static Mat getHistogramImage(Mat image) {

    // Compute histogram first
    Mat hist = getGrayHistogram(image);/*from   www  . jav  a2s  . com*/
    // Get min and max bin values

    MinMaxLocResult locPeak = Core.minMaxLoc(hist);
    double maxVal = locPeak.maxVal;
    double minVal = locPeak.minVal;

    // Image on which to display histogram
    Mat histImg = new Mat(image.rows(), image.rows(), CV_8U, new Scalar(255));

    // set highest point at 90% of nbins
    int hpt = (int) (0.9 * 256);

    // Draw vertical line for each bin 
    for (int h = 0; h < 256; h++) {

        double[] f = hist.get(h, 0);
        float binVal = (float) f[0];
        int intensity = (int) (binVal * hpt / maxVal);
        Core.line(histImg, new Point(h, 256.0d), new Point(h, 256.0d - intensity), Scalar.all(0));
    }
    return histImg;
}

From source file:in.fabinpaul.sixthsense.ColorBlobDetectionFragment.java

License:Apache License

@Override
public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    for (int i = 0; i < 4; i++) {
        mDetector[i] = new ColorBlobDetector();
        mBlobColorRgba[i] = new Scalar(255);
        mBlobColorHsv[i] = new Scalar(255);
    }// w  w  w .  j ava2s. c  om
    CONTOUR_COLOR = new Scalar(255, 0, 0, 255);
    getActivity().invalidateOptionsMenu();

}

From source file:in.fabinpaul.sixthsense.ColorBlobDetectionFragment.java

License:Apache License

private Scalar converScalarHsv2Rgba(Scalar hsvColor) {
    Mat pointMatRgba = new Mat();
    Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
    Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);

    return new Scalar(pointMatRgba.get(0, 0));
}

From source file:javaapplication1.JavaApplication1.java

public static void main(String[] args) {
    // you must load the OpenCV library like this before trying to do
    // anything with OpenCV!
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    // Print OpenCV version
    System.out.println("Welcome to OpenCV " + Core.VERSION);

    // In OpenCV, the most important data type is the Matrix, Mat.
    //// w w  w  . j  a v  a  2s  .c o m
    // Here, we create a matrix that has 5 rows and 10 columns.  It
    // stores an 8-bit type with a single channel.  In other words, a
    // matrix of bytes.  We'll initialize every element to 0.
    Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));

    // Dump information about the matrix
    System.out.println("OpenCV Mat: " + m);

    // set row 1 to be all 1s, and then column 5 to be all 5s
    Mat mr1 = m.row(1);
    mr1.setTo(new Scalar(1));
    Mat mc5 = m.col(5);
    mc5.setTo(new Scalar(5));

    // Dump the actual matrix contents
    System.out.println("OpenCV Mat data:\n" + m.dump());

    Ocv ocv = new Ocv();
    ocv.getFilePath();

    /**
     * Find faces in an image.
     *
     * @param filter Path to the xml face finding filter to use
     * @param input Path to the input image file
     * @param output Path to the output image file
     */
    //ocv.findFaces("lbpcascade_frontalface.xml", "C:\\Users\\Wellesley\\Documents\\GitHub\\CSE398\\opencvTutorial\\JavaApplication1\\src\\javaapplication1\\lena.png", "../javaapplication1");
    ocv.setOutput("step2.png");
    ocv.findFaces("", "", "");
    ocv.setOutput("step3.png");
    ocv.cropEachFace("", "");
    ocv.setOutput("step4.png");
    ocv.resizeEachFace("", "");
    ocv.setOutput("step6.png");
    ocv.makeFacesGray("", "", "");
    ocv.setOutput("step8.png");
    ocv.blendWithGray50("", "");
    ocv.setOutput("step10.png");
    ocv.doSobel("", "");
    ocv.setOutput("step11.png");
    ocv.directManip("", "");
}

From source file:karthik.Barcode.MatrixBarcode.java

License:Open Source License

private void calcGradientDirectionAndMagnitude() {
    // calculates magnitudes and directions of gradients in the image
    // results are stored in appropriate matrices in img_details object

    Imgproc.Scharr(img_details.src_grayscale, img_details.scharr_x, CvType.CV_32F, 1, 0);
    Imgproc.Scharr(img_details.src_grayscale, img_details.scharr_y, CvType.CV_32F, 0, 1);

    // calc angle using Core.phase function - quicker than using atan2 manually
    Core.phase(img_details.scharr_x, img_details.scharr_y, img_details.gradient_direction, true);

    // convert angles from 180-360 to 0-180 range and set angles from 170-180 to 0
    Core.inRange(img_details.gradient_direction, scalarDict.get(180), scalarDict.get(360), img_details.mask);
    Core.add(img_details.gradient_direction, scalarDict.get(-180), img_details.gradient_direction,
            img_details.mask);// w w  w. ja  v a  2 s  . c o  m
    Core.inRange(img_details.gradient_direction, scalarDict.get(170), scalarDict.get(180), img_details.mask);
    img_details.gradient_direction.setTo(ZERO_SCALAR, img_details.mask);

    // convert type after modifying angle so that angles above 360 don't get truncated
    img_details.gradient_direction.convertTo(img_details.gradient_direction, CvType.CV_8U);
    if (DEBUG_IMAGES)
        write_Mat("angles.csv", img_details.gradient_direction);

    // calculate magnitude of gradient, normalize and threshold
    Core.magnitude(img_details.scharr_x, img_details.scharr_y, img_details.gradient_magnitude);
    Core.normalize(img_details.gradient_magnitude, img_details.gradient_magnitude, 0, 255, Core.NORM_MINMAX,
            CvType.CV_8U);
    Imgproc.threshold(img_details.gradient_magnitude, img_details.gradient_magnitude, 50, 255,
            Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);

    // set angle to DUMMY_ANGLE = 255 at all points where gradient magnitude is 0 i.e. where there are no edges
    // these angles will be ignored in the histogram calculation since that counts only up to 180
    Core.inRange(img_details.gradient_magnitude, ZERO_SCALAR, ZERO_SCALAR, img_details.mask);
    img_details.gradient_direction.setTo(scalarDict.get(DUMMY_ANGLE), img_details.mask);
    // add 1 to gradient directions so that gradients of 0 can be located
    Core.add(img_details.gradient_direction, new Scalar(1), img_details.gradient_direction);

    // calculate integral image for edge density
    img_details.edgeDensity = calcEdgeDensityIntegralImage();

    // calculate histograms for each tile
    calcHistograms();

    if (DEBUG_IMAGES) {
        write_Mat("magnitudes.csv", img_details.gradient_magnitude);
        write_Mat("angles_modified.csv", img_details.gradient_direction);
    }
}

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  ava  2s .c  om
    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;

}