Example usage for org.opencv.core Mat height

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

Introduction

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

Prototype

public int height() 

Source Link

Usage

From source file:formularios.FrmCamera.java

public BufferedImage MatToBufferedImage(Mat frame) {
    //Mat() to BufferedImage
    int type = 0;
    if (frame.channels() == 1) {
        type = BufferedImage.TYPE_BYTE_GRAY;
    } else if (frame.channels() == 3) {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }//from   ww  w  .j a  v  a 2s.  c  o m
    BufferedImage image = new BufferedImage(frame.width(), frame.height(), type);
    WritableRaster raster = image.getRaster();
    DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
    byte[] data = dataBuffer.getData();
    frame.get(0, 0, data);

    return image;
}

From source file:fr.olympicinsa.riocognized.facedetector.tools.Treatment.java

public static Mat resize(Mat image, int maxW) {
    Mat resizeImage = new Mat();
    log.info("Image size: " + image.width() + "," + image.height());
    double f = (double) image.width() / (double) maxW;
    log.info("Resize factor : " + f);
    int w = (int) (image.width() / f);
    int h = (int) (image.height() / f);
    Size sz = new Size(w, h);
    log.info("Resizing image to : " + w + "," + h);
    Imgproc.resize(image, resizeImage, sz);
    return resizeImage;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Helper to create a new OpenCV Mat whose channels and
 * bit-depth mask an existing Mat./*from   ww  w.  java  2 s.  c  o m*/
 * 
 * @param m
 *       The Mat to match
 * @return
 *       A new Mat
 */
public static Mat imitate(Mat m) {
    return new Mat(m.height(), m.width(), m.type());
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * /* w ww  .  ja v  a 2  s .c om*/
 * @param src
 *       A Mat of type 8UC4 with channels arranged as BGRA.
 * @return
 *       A Mat of type 8UC1 in grayscale.
 */
public static Mat gray(Mat src) {
    Mat result = new Mat(src.height(), src.width(), CvType.CV_8UC1);
    Imgproc.cvtColor(src, result, Imgproc.COLOR_BGRA2GRAY);

    return result;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * /*ww w  .java2s .  com*/
 * 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 . j av  a  2 s.  c  o 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);//w  w w  .jav  a 2  s  .  co  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;/*from   w  w  w.j a  v  a2 s.c om*/
    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;
}