Exception in thread "main" java.lang.IllegalArgumentException

asked 2015-05-16 11:19:12 -0600

Brunts gravatar image

Hi I get this error msg Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0. Image is loaded in mat type then is converted to BufferedImage for displaying. in between some OpenCV function will be used. But i get bunch of error msg please can you tell me what is wrong?

public class Tutorial1 {

 public static void main(String[] args){

     System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
         Tutorial1 mat2Img = new Tutorial1();
         System.out.println("ok");
         mat2Img.converter();

 }

 public void converter(){

     Mat imageInMat = Imgcodecs.imread(getClass().getResource("image.jpg").getPath());
     BufferedImage bufImage =  Mat2BufferedImage(imageInMat);
     displayImage(bufImage);
 }

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

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);
    }}
edit retag flag offensive close merge delete

Comments

your image was probably not loaded (or found). check imageInMat.empty() after the load attempt.

berak gravatar imageberak ( 2015-05-16 11:40:35 -0600 )edit

yes it is empty :( but file is in bin folder where can be error?

Brunts gravatar imageBrunts ( 2015-05-16 12:42:30 -0600 )edit

get rid of getClass().getResource("image.jpg").getPath() and try an absolute path ?

berak gravatar imageberak ( 2015-05-16 13:11:02 -0600 )edit
1

Yay it worked thx. I was trying to do this for 3 hours I'm noob :D

Brunts gravatar imageBrunts ( 2015-05-16 13:20:43 -0600 )edit