Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

convert your Mat to a BufferedImage, then draw it the usual way:

Mat m = Highgui.imread("some.png");
Mat m2 = new Mat();
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
    Imgproc.cvtColor(m,m2,Imgproc.COLOR_BGR2RGB);
    type = BufferedImage.TYPE_3BYTE_BGR;
}
byte [] b = new byte[m.channels()*m.cols()*m.rows()];
m2.get(0,0,b);
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
image.getRaster().setDataElements(0, 0, m.cols(),m.rows(), b);  

// later:
public synchronized void update(Graphics g)
{
    g.drawImage(image,0,0,this);    
}

convert your Mat to a BufferedImage, then draw it the usual way:

Mat m = Highgui.imread("some.png");
Mat m2 = new Mat();
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()];
m2.get(0,0,b);
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);  

// later:
public synchronized void update(Graphics g)
{
    g.drawImage(image,0,0,this);    
}

assuming, you're talking about desktop-java here (android is a total different story),

convert your Mat to a BufferedImage, then draw it the usual way:

Mat m = Highgui.imread("some.png");
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);  

// later:
public synchronized void update(Graphics g)
{
    g.drawImage(image,0,0,this);    
}