Example usage for org.opencv.core MatOfByte MatOfByte

List of usage examples for org.opencv.core MatOfByte MatOfByte

Introduction

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

Prototype

public MatOfByte(byte... a) 

Source Link

Usage

From source file:com.jeremydyer.nifi.CropImageProcessor.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile original = session.get();
    if (original == null) {
        return;/*from  w w  w .ja v a2s  .  co m*/
    }

    session.read(original, new InputStreamCallback() {
        @Override
        public void process(InputStream inputStream) throws IOException {

            try {
                byte[] imgData = IOUtils.toByteArray(inputStream);
                Mat image = Imgcodecs.imdecode(new MatOfByte(imgData), Imgcodecs.CV_LOAD_IMAGE_COLOR);
                cropImage(session, context, original, image);
            } catch (Exception ex) {
                getLogger().error(ex.getMessage());
                ex.printStackTrace();
            }
        }
    });

    session.transfer(original, REL_ORIGINAL);

}

From source file:com.jeremydyer.nifi.FaceDetectionProcessor.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile original = session.get();
    if (original == null) {
        return;//from   ww  w.j  a  v  a 2  s.co  m
    }

    session.read(original, new InputStreamCallback() {
        @Override
        public void process(InputStream inputStream) throws IOException {

            try {
                byte[] imgData = IOUtils.toByteArray(inputStream);
                Mat image = Imgcodecs.imdecode(new MatOfByte(imgData), Imgcodecs.CV_LOAD_IMAGE_COLOR);
                detectFaces(session, context, original, image);
            } catch (Exception ex) {
                getLogger().error(ex.getMessage());
                ex.printStackTrace();
            }
        }
    });

    session.transfer(original, REL_ORIGINAL);

}

From source file:com.jeremydyer.nifi.ObjectDetectionProcessor.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile original = session.get();
    if (original == null) {
        return;//from  w  ww .  j  a  va 2s  .co m
    }

    final JSONObject JSON = new JSONObject(context.getProperty(DETECTION_DEFINITION_JSON).getValue());

    session.read(original, new InputStreamCallback() {
        @Override
        public void process(InputStream inputStream) throws IOException {

            try {
                byte[] imgData = IOUtils.toByteArray(inputStream);
                Mat image = Imgcodecs.imdecode(new MatOfByte(imgData), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

                //Loops through all of the detection definitions
                JSONArray dds = JSON.getJSONArray("DetectionDefinition");

                for (int i = 0; i < dds.length(); i++) {
                    JSONObject dd = dds.getJSONObject(i);
                    detectObjects(session, original, dd, image);
                }

            } catch (Exception ex) {
                getLogger().error(ex.getMessage());
                ex.printStackTrace();
            }
        }
    });

    session.transfer(original, REL_ORIGINAL);

}

From source file:com.oetermann.imageclassifier.ImageClassifier.java

License:Open Source License

public String matchName(String matcherName, byte[] data, int minMatches) {
    return matchName(matcherName, Imgcodecs.imdecode(new MatOfByte(data), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED),
            minMatches);//from w w w .  ja v  a  2s  . com
}

From source file:cx.uni.jk.mms.iaip.mat.MatModel.java

License:Open Source License

/**
 * loads an image with Java ImageIO and decodes it with OpenCV built-in
 * functions.//from   w  w  w  .j a  va2  s.  c  o  m
 * 
 * gotcha: OpenCV may run into a library version mismatch. @see <a href=
 * "http://www.answers.opencv.org/question/34412/solved-opencv-libpng-version-mismatch/"
 * >posting on OpenCV forums</a>.
 */
@Deprecated
protected Mat loadImageWithJavaImageIOAndDecodeWithOpenCV(Path path) throws IOException {
    this.logger.finer("Loading image with Java ImageIO, decoding with OpenCV library.");
    /**
     * Note: can not use Highgui.imread() with a Path. Must read whole file
     * into memory before copying into Mat.
     */
    BufferedImage img = ImageIO.read(path.toUri().toURL());
    Mat bufMat = new MatOfByte(((DataBufferByte) img.getRaster().getDataBuffer()).getData());
    return Highgui.imdecode(bufMat, Highgui.IMREAD_GRAYSCALE);
}

From source file:cx.uni.jk.mms.iaip.mat.MatModel.java

License:Open Source License

/**
 * loads and decodes an image with OpenCV built-in functions.
 * // w ww  . j a v  a  2  s  . c  om
 * gotcha: OpenCV may run into a library version mismatch. @see <a href=
 * "http://www.answers.opencv.org/question/34412/solved-opencv-libpng-version-mismatch/"
 * >posting on OpenCV forums</a>.
 */
@Deprecated
protected Mat loadAndDecodeImageWithOpenCV(Path path) throws IOException {
    this.logger.finer("Loading and decoding image with OpenCV library.");
    /**
     * Note: can not use Highgui.imread() with a Path. Must read whole file
     * into memory before decoding with Highgui.imdecode().
     */
    Mat bufMat = new MatOfByte(Files.readAllBytes(path));
    return Highgui.imdecode(bufMat, Highgui.IMREAD_GRAYSCALE);
}

From source file:es.upv.riromu.platanus.image.ImageUtil.java

License:Open Source License

public static Mat readInputStreamIntoMat(InputStream inputStream) throws IOException {
    // Read into byte-array
    byte[] temporaryImageInMemory = readStream(inputStream);

    // Decode into mat. Use any IMREAD_ option that describes your image appropriately
    Mat outputImage = Imgcodecs.imdecode(new MatOfByte(temporaryImageInMemory), Imgcodecs.IMREAD_GRAYSCALE);

    return outputImage;
}

From source file:org.usfirst.frc.team766.lib.AxisCamera.java

License:Open Source License

/**
 * Grabs an image from the axis IP camera and returns it as a Mat object that can be easily used with openCV.
 *
 * @return <code>true</code> upon success, <code>false</code> on a failure
 *//*from w w  w  .j  ava  2 s . c  o  m*/
public Mat getImage() {
    Mat out;
    if (m_imageData.limit() == 0) {
        System.out.println("Can't connect to Axis Cam");
        return new Mat();
    }

    synchronized (m_imageDataLock) {
        MatOfByte in = new MatOfByte(m_imageData.array());
        try {
            FileOutputStream fOut = new FileOutputStream(new File("/tmp/img.jpg"));
            fOut.write(m_imageData.array());
            fOut.close();
        } catch (IOException e) {
            System.out.println("Couldn't connect output stream");
            e.printStackTrace();
        }

        out = Highgui.imdecode(in, Highgui.CV_LOAD_IMAGE_COLOR);
    }

    m_freshImage = false;
    return out;
}

From source file:org.wso2.carbon.cep.objectdetection.siddhiextension.ObjectDetectionExtension.java

License:Open Source License

/**
 * Detect objects using OpenCV.// w  ww .  j ava  2 s  .  c  o  m
 *
 * @param imageHex
 *            the image hex string
 * @param cascadePath
 *            the cascade path
 * @return the detected object count
 */
private long detectObjects(String imageHex, String cascadePath) {
    long objectCount = 0;
    try {
        // conversion to Mat
        byte[] imageByteArr = (byte[]) new Hex().decode(imageHex);
        Mat image = Highgui.imdecode(new MatOfByte(imageByteArr), Highgui.IMREAD_UNCHANGED);

        // initializing classifier
        CascadeClassifier cClassifier = new CascadeClassifier();
        cClassifier.load(cascadePath);

        // pre-processing
        Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY);
        Imgproc.equalizeHist(image, image);

        // detecting objects
        MatOfRect imageRect = new MatOfRect();
        cClassifier.detectMultiScale(image, imageRect);

        // image count
        objectCount = ((Integer) imageRect.toList().size()).longValue();
    } catch (DecoderException e) {
        e.printStackTrace();
    }

    return objectCount;
}

From source file:ru.caramel.juniperbot.web.service.OpenCVService.java

License:Open Source License

public static Mat getMat(BufferedImage image) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", byteArrayOutputStream);
    byteArrayOutputStream.flush();/* w ww  .  ja  v a  2 s.  c om*/
    return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()),
            Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
}