Example usage for org.opencv.core Mat width

List of usage examples for org.opencv.core Mat width

Introduction

In this page you can find the example usage for org.opencv.core Mat width.

Prototype

public int width() 

Source Link

Usage

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * /*from   w w  w .j  ava 2s .c  o  m*/
 * Convert a 4 channel OpenCV Mat object into 
 * pixels to be shoved into a 4 channel ARGB PImage's
 * pixel array.
 * 
 * @param m
 *       An RGBA Mat we want converted 
 * @return
 *       An int[] formatted to be the pixels of a PImage
 */
public int[] matToARGBPixels(Mat m) {
    int pImageChannels = 4;
    int numPixels = m.width() * m.height();
    int[] intPixels = new int[numPixels];
    byte[] matPixels = new byte[numPixels * pImageChannels];

    m.get(0, 0, matPixels);
    ByteBuffer.wrap(matPixels).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(intPixels);
    return intPixels;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

public PImage getSnapshot(Mat m) {
    PImage result = parent.createImage(m.width(), m.height(), PApplet.ARGB);
    toPImage(m, result);
    return result;
}

From source file:gab.opencv.OpenCVProcessingUtils.java

License:Open Source License

public static Mat imitate(Mat m) {
    return new Mat(m.height(), m.width(), m.type());
}

From source file:gab.opencv.OpenCVProcessingUtils.java

License:Open Source License

/**
 * Convert an OpenCV Mat object into a PImage
 * to be used in other Processing code./*ww  w . java  2s  . co m*/
 * Copies the Mat's pixel data into the PImage's pixel array.
 * Iterates over each pixel in the Mat, i.e. expensive.
 * 
 * (Mainly used internally by OpenCV. Inspired by toCv()
 * from KyleMcDonald's ofxCv.)
 * 
 * @param m
 *          A Mat you want converted
 * @param img
 *          The PImage you want the Mat converted into.
 */
public void toPImage(Mat m, PImage img) {
    img.loadPixels();

    if (m.channels() == 3) {
        byte[] matPixels = new byte[width * height * 3];
        m.get(0, 0, matPixels);
        for (int i = 0; i < m.width() * m.height() * 3; i += 3) {
            img.pixels[PApplet.floor(i / 3)] = parent.color(matPixels[i + 2] & 0xFF, matPixels[i + 1] & 0xFF,
                    matPixels[i] & 0xFF);
        }
    } else if (m.channels() == 1) {
        byte[] matPixels = new byte[width * height];
        m.get(0, 0, matPixels);
        for (int i = 0; i < m.width() * m.height(); i++) {
            img.pixels[i] = parent.color(matPixels[i] & 0xFF);
        }
    } else if (m.channels() == 4) {
        byte[] matPixels = new byte[width * height * 4];
        m.get(0, 0, matPixels);
        for (int i = 0; i < m.width() * m.height() * 4; i += 4) {
            img.pixels[PApplet.floor(i / 4)] = parent.color(matPixels[i + 2] & 0xFF, matPixels[i + 1] & 0xFF,
                    matPixels[i] & 0xFF, matPixels[i + 3] & 0xFF);
        }
    }

    img.updatePixels();
}

From source file:gov.nasa.jpl.memex.pooledtimeseries.PoT.java

License:Apache License

static ArrayList<double[][]> computeGradients(Mat frame, int dim) {
    byte frame_array[] = new byte[(int) frame.total()];
    frame.get(0, 0, frame_array);/*from   ww w. j ava  2 s  .  c  o m*/

    ArrayList<double[][]> gradients = new ArrayList<double[][]>();

    for (int k = 0; k < dim; k++) {
        double angle = Math.PI * (double) k / (double) dim;

        double dx = Math.cos(angle) * 0.9999999;
        double dy = Math.sin(angle) * 0.9999999;

        double[][] grad = new double[frame.width()][frame.height()];

        for (int i = 0; i < frame.cols(); i++) {
            for (int j = 0; j < frame.rows(); j++) {
                if (i <= 1 || j <= 1 || i >= frame.cols() - 2 || j >= frame.rows() - 2) {
                    grad[i][j] = 0;
                } else {
                    double f1 = interpolatePixel(frame_array, frame.cols(), (double) i + dx, (double) j + dy);
                    double f2 = interpolatePixel(frame_array, frame.cols(), (double) i - dx, (double) j - dy);

                    double diff = f1 - f2;
                    if (diff < 0)
                        diff = diff * -1;
                    if (diff >= 256)
                        diff = 255;

                    grad[i][j] = diff;
                }
            }
        }

        gradients.add(grad);
    }

    return gradients;
}

From source file:houghtransform.Capture.java

private static BufferedImage convertMatToBufferedImage(Mat mat) {
    byte[] data = new byte[mat.width() * mat.height() * (int) mat.elemSize()];
    int type;/* w  w w. j a  va  2s .c o m*/
    mat.get(0, 0, data);

    switch (mat.channels()) {
    case 1:
        type = BufferedImage.TYPE_BYTE_GRAY;
        break;
    case 3:
        type = BufferedImage.TYPE_3BYTE_BGR;
        // bgr to rgb    
        byte b;
        for (int i = 0; i < data.length; i = i + 3) {
            b = data[i];
            data[i] = data[i + 2];
            data[i + 2] = b;
        }
        break;
    default:
        throw new IllegalStateException("Unsupported number of channels");
    }

    BufferedImage out = new BufferedImage(mat.width(), mat.height(), type);

    out.getRaster().setDataElements(0, 0, mat.width(), mat.height(), data);

    return out;
}

From source file:info.jmfavreau.bifrostcore.imageprocessing.ImageToColor.java

License:Open Source License

private Mat compute_roi(Mat original) {
    Mat roi = new Mat();
    Imgproc.cvtColor(original, roi, Imgproc.COLOR_BGR2GRAY, 0);
    roi.setTo(new Scalar(0, 0, 0));
    int x = original.width();
    int y = original.height();
    int cx = x / 2;
    int cy = y / 2;
    int r = Math.min(cx, cy) * 2 / 3;
    Core.circle(roi, new Point(cx, cy), r, new Scalar(255, 255, 255), -1, 8, 0);
    return roi;//from www.ja va2s  .  c  o m
}

From source file:io.appium.java_client.ScreenshotState.java

License:Apache License

private static Mat resizeFirstMatrixToSecondMatrixResolution(Mat first, Mat second) {
    if (first.width() != second.width() || first.height() != second.height()) {
        final Mat result = new Mat();
        final Size sz = new Size(second.width(), second.height());
        Imgproc.resize(first, result, sz);
        return result;
    }//  w ww  .j  a v a  2 s . c  o m
    return first;
}

From source file:io.appium.java_client.ScreenshotState.java

License:Apache License

/**
 * Compares two valid java bitmaps and calculates similarity score between them.
 *
 * @param refImage   reference image/*  w ww  .  j  a v  a  2  s .  co m*/
 * @param tplImage   template
 * @param resizeMode one of possible enum values. Set it either to <em>TEMPLATE_TO_REFERENCE_RESOLUTION</em> or
 *                   <em>REFERENCE_TO_TEMPLATE_RESOLUTION</em> if given bitmaps have different dimensions
 * @return similarity score value in range (-1.0, 1.0). 1.0 is returned if the images are equal
 * @throws ScreenshotComparisonError if provided images are not valid or have
 *                                   different resolution, but resizeMode has been set to <em>NO_RESIZE</em>
 */
public static double getOverlapScore(BufferedImage refImage, BufferedImage tplImage, ResizeMode resizeMode) {
    Mat ref = prepareImageForComparison(refImage);
    if (ref.empty()) {
        throw new ScreenshotComparisonError("Reference image cannot be converted for further comparison");
    }
    Mat tpl = prepareImageForComparison(tplImage);
    if (tpl.empty()) {
        throw new ScreenshotComparisonError("Template image cannot be converted for further comparison");
    }
    switch (resizeMode) {
    case TEMPLATE_TO_REFERENCE_RESOLUTION:
        tpl = resizeFirstMatrixToSecondMatrixResolution(tpl, ref);
        break;
    case REFERENCE_TO_TEMPLATE_RESOLUTION:
        ref = resizeFirstMatrixToSecondMatrixResolution(ref, tpl);
        break;
    default:
        // do nothing
    }

    if (ref.width() != tpl.width() || ref.height() != tpl.height()) {
        throw new ScreenshotComparisonError(
                "Resolutions of template and reference images are expected to be equal. "
                        + "Try different resizeMode value.");
    }

    Mat res = new Mat(ref.rows() - tpl.rows() + 1, ref.cols() - tpl.cols() + 1, CvType.CV_32FC1);
    Imgproc.matchTemplate(ref, tpl, res, Imgproc.TM_CCOEFF_NORMED);
    return Core.minMaxLoc(res).maxVal;
}

From source file:io.github.jakejmattson.facialrecognition.ImageFrame.java

License:Open Source License

/**
 * Convert an OpenCV Mat to a Java BufferedImage.
 *
 * @param matrix//from  w w  w .  ja  v a 2s .c om
 *       OpenCV Mat
 *
 * @return BufferedImage
 */
private static BufferedImage convertMatToImage(Mat matrix) {
    int width = matrix.width();
    int height = matrix.height();
    int type = matrix.channels() != 1 ? BufferedImage.TYPE_3BYTE_BGR : BufferedImage.TYPE_BYTE_GRAY;

    if (type == BufferedImage.TYPE_3BYTE_BGR)
        Imgproc.cvtColor(matrix, matrix, Imgproc.COLOR_BGR2RGB);

    byte[] data = new byte[width * height * (int) matrix.elemSize()];
    matrix.get(0, 0, data);

    BufferedImage out = new BufferedImage(width, height, type);
    out.getRaster().setDataElements(0, 0, width, height, data);

    return out;
}