Exception Error while trying to run the code

asked 2014-01-30 08:34:41 -0600

Joseph118 gravatar image

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?

edit retag flag offensive close merge delete

Comments

1

your original Mat is empty ? please go and check !

berak gravatar imageberak ( 2014-01-30 08:40:10 -0600 )edit

this is a method and when it is called it displays this exception. i send a mat as a perimeter; "Mat webcam_image = new Mat()"

Joseph118 gravatar imageJoseph118 ( 2014-01-30 09:05:19 -0600 )edit

new Mat() won't do. ( has no pixels, no width, no height ).

give it a real image

berak gravatar imageberak ( 2014-01-30 09:07:27 -0600 )edit

oh i got it now... and i didn't mention it but i am trying to display my webcam on my GUI as my first goal, so later on i can try implement something else

Joseph118 gravatar imageJoseph118 ( 2014-01-30 09:16:32 -0600 )edit

damn it i had my webcam turned of no wonder it didn't work! Thanks dude i would of spent hours of research for nothing if it wasn't you :p

Joseph118 gravatar imageJoseph118 ( 2014-01-30 09:18:38 -0600 )edit