Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Exception Error while trying to run the code

I am new to OpenCV and i got no experience in graphics and algorithms. Anyway on to the question. I have this method which is currently giving me problems that I cannot solve. I have little experience of what is going on.

public BufferedImage 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;
}

This method returns an exception on "BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);" which is;

Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0 at java.awt.image.SampleModel.<init>(Unknown Source) at java.awt.image.ComponentSampleModel.<init>(Unknown Source) at java.awt.image.PixelInterleavedSampleModel.<init>(Unknown Source) at java.awt.image.Raster.createInterleavedRaster(Unknown Source) at java.awt.image.Raster.createInterleavedRaster(Unknown Source) at java.awt.image.Raster.createInterleavedRaster(Unknown Source) at java.awt.image.ComponentColorModel.createCompatibleWritableRaster(Unknown Source) at java.awt.image.BufferedImage.<init>(Unknown Source) at Interface.CameraPanel.toBufferedImage(CameraPanel.java:73) at Interface.CameraPanel.setCamera(CameraPanel.java:52) at Interface.UserUI.components(UserUI.java:31) at Project.main(Project.java:12)

can anyone tell me what is wrong? and what has to be done?