1 | initial version |
Hi,
I can't assure you this is the fastest way to do it, but it takes around 2-3 ms per frame (640x480) on a similar computer. General idea is natively inverting BGR -> RGB and using BufferedImage setDataElements.
public Image toBufferedImageNoEncode(Mat m){
long time = System.currentTimeMillis();
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
Mat m2 = new Mat();
Imgproc.cvtColor(m,m2,Imgproc.COLOR_BGR2RGB);
type = BufferedImage.TYPE_3BYTE_BGR;
m = m2;
}
byte [] b = new byte[m.channels()*m.cols()*m.rows()];
m.get(0,0,b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
image.getRaster().setDataElements(0, 0, m.cols(),m.rows(), b);
System.out.println("To buffer: " + (System.currentTimeMillis() - time));
return image;
}