Example usage for org.opencv.core Mat put

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

Introduction

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

Prototype

public int put(int row, int col, byte[] data) 

Source Link

Usage

From source file:org.sikuli.script.Image.java

License:MIT License

protected static Mat createMat(BufferedImage img) {
    if (img != null) {
        Debug timer = Debug.startTimer("Mat create\t (%d x %d) from \n%s", img.getWidth(), img.getHeight(),
                img);/*w  w  w .  ja  v a2 s  .c  o  m*/
        Mat mat_ref = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC4);
        timer.lap("init");
        byte[] data;
        BufferedImage cvImg;
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        int[] nBits = { 8, 8, 8, 8 };
        ColorModel cm = new ComponentColorModel(cs, nBits, true, false, Transparency.TRANSLUCENT,
                DataBuffer.TYPE_BYTE);
        SampleModel sm = cm.createCompatibleSampleModel(img.getWidth(), img.getHeight());
        DataBufferByte db = new DataBufferByte(img.getWidth() * img.getHeight() * 4);
        WritableRaster r = WritableRaster.createWritableRaster(sm, db, new Point(0, 0));
        cvImg = new BufferedImage(cm, r, false, null);
        timer.lap("empty");
        Graphics2D g = cvImg.createGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();
        timer.lap("created");
        data = ((DataBufferByte) cvImg.getRaster().getDataBuffer()).getData();
        mat_ref.put(0, 0, data);
        Mat mat = new Mat();
        timer.lap("filled");
        Imgproc.cvtColor(mat_ref, mat, Imgproc.COLOR_RGBA2BGR, 3);
        timer.end();
        return mat;
    } else {
        return null;
    }
}

From source file:org.surmon.pattern.importer.impl.JPEGImporter.java

private static PatternImage importJPEG(int id, File file) {

    BufferedImage tempImage;/*from w w  w. j  ava2s. c om*/
    WritableRaster tempRaster;
    byte[] values;

    try {
        tempImage = ImageIO.read(file);
        tempRaster = tempImage.getRaster();
        values = new byte[tempImage.getWidth() * tempImage.getHeight()];
        int c = 0;
        for (int y = 0; y < tempImage.getHeight(); y++) {
            for (int x = 0; x < tempImage.getWidth(); x++) {
                values[c++] = (byte) tempRaster.getSample(x, y, 0);
            }
        }

        Mat mat = new Mat(tempImage.getHeight(), tempImage.getWidth(), CvType.CV_8UC1);
        mat.put(0, 0, values);

        return new PatternImage(id, mat);

    } catch (IOException ex) {
        Logger.getLogger("Loading").log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:org.surmon.pattern.importer.impl.MRCImporter.java

/**
 * Provides import from MRC.//from w ww.j a  v  a2 s  .  com
 *
 * @param file file to import from
 * @return imported data
 */
private ImageStack importMRC(String path) {
    BufferedImageReader r = new BufferedImageReader();

    try {
        r.setId(path);
        final Dimension3D dim = new Dimension3D(r.getSizeX(), r.getSizeY(), r.getImageCount());
        List<PatternImage> images = new ArrayList<>(dim.getDepth());

        if (p != null) {
            p.switchToDeterminate(dim.getDepth());
        }

        for (int i = 0; i < dim.getDepth(); i++) {
            Mat mat = new Mat(dim.getHeight(), dim.getWidth(), CvType.CV_8UC1);
            mat.put(0, 0, r.openBytes(i));
            images.add(new PatternImage(i, mat));
            updateProgress(i);
        }

        r.close();
        return new ImageStack(images);

    } catch (FormatException | IOException ex) {
        Logger.getLogger(PDataImporterFactory.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:org.surmon.pattern.importer.impl.TIFFImporter.java

@Override
public ImageStack importData(String path) {

    //get image reader for tiff
    final BufferedImageReader reader = new BufferedImageReader();
    PatternImage[] images = null;// www . j  ava  2  s.c  o  m
    try {
        reader.setId(path);

        int x = reader.getSizeX();
        int y = reader.getSizeY();
        int t = reader.getImageCount();
        images = new PatternImage[t];

        if (p != null) {
            p.switchToDeterminate(t);
        }

        for (int i = 0; i < t; i++) {
            Mat mat = new Mat(y, x, CvType.CV_8UC1);
            mat.put(0, 0, reader.openBytes(i));
            images[i] = new PatternImage(i, mat);
            updateProgress(i);
        }

        System.out.println("Images loaded");
        reader.close();
    } catch (FormatException | IOException ex) {
        Exceptions.printStackTrace(ex);
    }

    return new ImageStack(Arrays.asList(images));
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.CoordinateMath.java

License:Open Source License

/**
 * Rotate a point around the x axis and store the result in another point.
 * //ww  w.  ja  v a2 s  .  c o m
 * @param point the point to rotate
 * @param destPoint the output point
 * @param angle the angle to rotate (in radians)
 */
public static void rotateX(Mat point, Mat destPoint, double angle) {
    Mat rotateMat = Mat.eye(3, 3, point.type());

    double c = Math.cos(angle);
    double s = Math.sin(angle);

    rotateMat.put(1, 1, c);
    rotateMat.put(1, 2, -s);
    rotateMat.put(2, 1, s);
    rotateMat.put(2, 2, c);

    matMult(rotateMat, point, destPoint);
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.CoordinateMath.java

License:Open Source License

/**
 * Rotate a point around the y axis and store the result in another point.
 * /* w  w w .j a v a2  s.  c  o  m*/
 * @param point the point to rotate
 * @param destPoint the output point
 * @param angle the angle to rotate (in radians)
 */
public static void rotateY(Mat point, Mat destPoint, double angle) {
    Mat rotateMat = Mat.eye(3, 3, point.type());

    double c = Math.cos(angle);
    double s = Math.sin(angle);

    rotateMat.put(0, 0, c);
    rotateMat.put(0, 2, s);
    rotateMat.put(2, 0, -s);
    rotateMat.put(2, 2, c);

    matMult(rotateMat, point, destPoint);
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.CoordinateMath.java

License:Open Source License

/**
 * Rotate a point around the z axis and store the result in another point.
 * /* w  w  w. java 2 s  .c om*/
 * @param point the point to rotate
 * @param destPoint the output point
 * @param angle the angle to rotate (in radians)
 */
public static void rotateZ(Mat point, Mat destPoint, double angle) {
    Mat rotateMat = Mat.eye(3, 3, point.type());

    double c = Math.cos(angle);
    double s = Math.sin(angle);

    rotateMat.put(0, 0, c);
    rotateMat.put(0, 1, -s);
    rotateMat.put(1, 0, s);
    rotateMat.put(1, 1, c);

    matMult(rotateMat, point, destPoint);
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.CoordinateMath.java

License:Open Source License

/**
 * Translate a point in space./*from  w  w  w .  j  a  v a2 s . com*/
 * 
 * @param vec the input point
 * @param destVec the output point
 * @param x the x translation
 * @param y the y translation
 * @param z the z translation
 */
public static void translate(Mat vec, Mat destVec, double x, double y, double z) {
    Mat translateVec = new Mat(3, 1, CvType.CV_64FC1);
    translateVec.put(0, 0, x);
    translateVec.put(1, 0, y);
    translateVec.put(2, 0, z);

    Core.add(vec, translateVec, destVec);
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.HighGoalProcessor.java

License:Open Source License

@Override
public void preProcess(Mat image) {
    // Update camera exposure
    if (camera != null) {
        camera.setExposure(VisionParameters.getAimingExposure());
        camera.setAutoExposure(VisionParameters.getAimingAutoExposure());
    }// w  ww.  j a  va 2s  . co  m

    // Save a snapshot if requested
    if (VisionParameters.shouldTakeSnapshot()) {
        String path = System.getProperty("user.home") + "/vision_snapshots/" + System.currentTimeMillis()
                + ".png";
        if (!Imgcodecs.imwrite(path, image)) {
            System.err.println("Could not save snapshot to: " + path);
        }
    }

    // Encode the estimated image timestamp into the top left corner
    byte[] timestamp = new byte[9];
    Utils.longToBytes((long) ((Timer.getFPGATimestamp() - ESTIMATED_CAMERA_LATENCY) * 1000), timestamp);
    image.put(0, 0, timestamp);
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.Target.java

License:Open Source License

private static Mat matFrom2DArray(double[][] array) {
    Mat mat = new Mat(array.length, array[0].length, CvType.CV_64F);

    for (int r = 0; r < array.length; r++) {
        mat.put(r, 0, array[r]);
    }//from  w  w w  .  ja v  a  2s  . c  o  m
    return mat;
}