Package astro.util

Source Code of astro.util.ImgConverter

/*
* 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 astro.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

/**
* @author Tamás Szeleczki <sztforums@gmail.com>
* @see <a href="http://szelinet.hu/thesis/">BSc Thesis in Engineering Information Technology</a>
*/
public class ImgConverter {
    public static Mat toMat( BufferedImage img ) {
        Mat m = new Mat(img.getWidth(), img.getHeight(), CvType.CV_8UC4);
        byte[] pixels = ((DataBufferByte)img.getRaster().getDataBuffer()).getData();
        m.put(0, 0, pixels);
        return m;
    }
   
    public static Image toBufferedImage( Mat m ) {
        int type = BufferedImage.TYPE_BYTE_GRAY;
        if ( m.channels() > 1 ) {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
       
        int bufferSize = m.channels()*m.cols()*m.rows();
        byte [] b = new byte[bufferSize];
        m.get(0,0,b); // get all the pixels
        BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
       
        System.arraycopy(b, 0, targetPixels, 0, b.length)
       
        return image;
    }
}
TOP

Related Classes of astro.util.ImgConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.