JavaCv: how to convert IplImage toBufferedImage

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

I have to convert an IplImage to a BufferedImage... what should be a simple task is becoming very intricate for me. I'm using JavaCv 1.0 on Mac OSX (which links with OpenCV 3.0).

In the old JavaCV API there was the IplImage method #getBufferedImage, but I can't find it anymore in new 1.0 API. So I tried to convert IplImage to byte array and byte array to BufferedImage. I found this solution to perform such conversion:

IplImage inputImg = cvLoadImage(imgName); //imgName is a JPEG absolute path byte[] imageData = IplImageToByteArray( inputImg); //defined below ByteArrayInputStream bais = new ByteArrayInputStream(imageData); BufferedImage inputImage = ImageIO.read(bais); //Always return null 

where IplImageToByteArray can be defined in one of the following ways:

public static byte[] IplImageToByteArray(IplImage src) {     ByteBuffer byteBuffer = src.getByteBuffer();     byte[] barray = new byte[byteBuffer.remaining()];     byteBuffer.get(barray);     return barray; }  public static byte[] IplImageToByteArray2(IplImage src) {     byte[] barray = new byte[src.imageSize()];     src.getByteBuffer().get(barray);     return barray; } 

The returned byte array is the same in both cases, but ImageIO.read returns always null value. I've no idea about what I'm getting wrong.

回答1:

public static BufferedImage IplImageToBufferedImage(IplImage src) {     OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();     Java2DFrameConverter paintConverter = new Java2DFrameConverter();     Frame frame = grabberConverter.convert(src);     return paintConverter.getBufferedImage(frame,1); } 


回答2:

use "org.bytedeco.javacv.Java2DFrameUtils", and you will know what I said.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!