How to display image on JAVA release
Hi everyone,
In new release OpenCV 2.4.4, how can I display image ( Mat ) on window using JAVA. I couldn't find the JAVA method from API?
Hi everyone,
In new release OpenCV 2.4.4, how can I display image ( Mat ) on window using JAVA. I couldn't find the JAVA method from API?
Window management from OpenCV highgui module is no wrapper to Java yet. You need to use some Java UI frameworks like SWING or wrap it to Java manually using JNI.
You can use the next code to transform a cvMat element into a java element: BufferedImage or Image:
public BufferedImage Mat2BufferedImage(Mat m){
// source: http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/
// Fastest code
// The output can be assigned either to a BufferedImage or to an Image
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;
}
And then display it with:
public void displayImage(Image img2)
{
//BufferedImage img=ImageIO.read(new File("/HelloOpenCV/lena.png"));
ImageIcon icon=new ImageIcon(img2);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(img2.getWidth(null)+50, img2.getHeight(null)+50);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Asked: 2013-02-26 23:52:02 -0600
Seen: 7,687 times
Last updated: Apr 22 '14
Negative Values When Getting Pixels in Java API
OpenCV Java + Google App Engine [closed]
Can't build opencv2.4.4 with java support [on Ubuntu 12.04]
how do I generate the Java bindings for the gpu module?
Unresolved inclusion in OpenCV+Android tutorial
How to convert Floating point image to 32-bit single-channel?
How to translate this to Java?
android: how to put a column into Mat