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?
your original Mat is empty ? please go and check !
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()"
new Mat() won't do. ( has no pixels, no width, no height ).
give it a real image
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
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