jarvis.module.colourtracking.Mat2Image.java Source code

Java tutorial

Introduction

Here is the source code for jarvis.module.colourtracking.Mat2Image.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jarvis.module.colourtracking;

import java.awt.image.BufferedImage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

/**
 *
 * @author Huiying S. http://computervisionandjava.blogspot.co.uk/2013/10/java-opencv-webcam.html
 */

public class Mat2Image {
    Mat mat = new Mat();
    BufferedImage img;
    byte[] dat;

    public Mat2Image() {
    }

    public Mat2Image(Mat mat) {
        getSpace(mat);
    }

    public void getSpace(Mat mat) {
        this.mat = mat;
        int w = mat.cols(), h = mat.rows();
        if (dat == null || dat.length != w * h * 3)
            dat = new byte[w * h * 3];
        if (img == null || img.getWidth() != w || img.getHeight() != h
                || img.getType() != BufferedImage.TYPE_3BYTE_BGR)
            img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
    }

    BufferedImage getImage(Mat mat) {
        //----------Additional line-----------------------------------------------------
        Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2BGR);
        //------------------------------------------------------------------------------
        getSpace(mat);
        mat.get(0, 0, dat);
        img.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), dat);
        return img;
    }

    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }
}