Monday, June 24, 2013

Creating windows and capturing webcam with Java and OpenCV

As we saw here, we can load a picture and display it, all in java. Now we want to use OpenCV for some of this so that we can apply some signal processing to the image down the road... The trick is explained here... Basically how to "cast" the image from OpenCV (in Mat) in an Image object that works with drawImage.We found some answers in the topic but nothing was too clear:
  1. Android has a library that allows from Bitmap to Mat and viceversa: org.opencv.android.Utils. This probably would work for us as finally we want to run this on Android, but still, not for the time being (we are in PC...).
  2. http://stackoverflow.com/questions/14958643/converting-bufferedimage-to-mat-in-opencv
  3. http://stackoverflow.com/questions/15670933/opencv-java-load-image-to-gui
Did a further search for "java opencv convert mat into image -android -IplImage" and got this two that match:
  1. http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/
  2. http://enfanote.blogspot.com/2013/06/converting-java-bufferedimage-to-opencv.html
That solved! Geeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee!! Somehow, seems to be done by hand conversion, which is not very nice, but looks like there is not library call for this...

UPDATE: please see the code on the first comment below, that ngeen left. I have not tried it in here, but I used it in this other example and it works nicely!!
UPDATE2: triggered by another nice way to do this Mat to BufferedImage conversion, I decided to benchmark all these methods. See here this post for the results.

 // Import the basic graphics classes.  
 // The problem here is that we read the image with OpenCV into a Mat object.  
 // But OpenCV for java doesn't have the method "imshow", so, we got to use  
 // java for that (drawImage) that uses Image or BufferedImage (?)  
 // So, how to go from one the other...  
 import java.awt.*;  
 import java.awt.image.BufferedImage;  
 import javax.swing.*;  
 //import org.opencv.core.Core;  
 import org.opencv.core.Mat;  
 import org.opencv.highgui.Highgui;  
 public class Panel extends JPanel{  
   private static final long serialVersionUID = 1L;  
   String pic_name="resources/DSC01638.jpg";  
   Mat picture;  
   BufferedImage image;  
   /**  
    * Converts/writes a Mat into a BufferedImage.  
    *  
    * @param matrix Mat of type CV_8UC3 or CV_8UC1  
    * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY  
    */  
   public static BufferedImage matToBufferedImage(Mat matrix) {  
     int cols = matrix.cols();  
     int rows = matrix.rows();  
     int elemSize = (int)matrix.elemSize();  
     byte[] data = new byte[cols * rows * elemSize];  
     int type;  
     matrix.get(0, 0, data);  
     switch (matrix.channels()) {  
       case 1:  
         type = BufferedImage.TYPE_BYTE_GRAY;  
         break;  
       case 3:  
         type = BufferedImage.TYPE_3BYTE_BGR;  
         // bgr to rgb  
         byte b;  
         for(int i=0; i<data.length; i=i+3) {  
           b = data[i];  
           data[i] = data[i+2];  
           data[i+2] = b;  
         }  
         break;  
       default:  
         return null;  
     }  
     BufferedImage image = new BufferedImage(cols, rows, type);  
     image.getRaster().setDataElements(0, 0, cols, rows, data);  
     return image;  
   }  
   // Create a constructor method  
   public Panel(){  
     super(); // Calls the parent constructor  
     picture = Highgui.imread(pic_name);  
     // Got to cast picture into Image  
     image=matToBufferedImage(picture);  
   }  
   public void paintComponent(Graphics g){  
      g.drawImage(image,50,10,400,400, this);  
   }  
   public static void main(String arg[]){  
    // Load the native library.  
    System.loadLibrary("opencv_java245");    
    JFrame frame = new JFrame("BasicPanel");  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame.setSize(400,400);  
    Panel panel = new Panel();  
    frame.setContentPane(panel);       
    frame.setVisible(true);           
  }  
 }  

Then, we want to display the webcam. So, not read from file...
 // Import the basic graphics classes.  
 // The problem here is that we read the image with OpenCV into a Mat object.  
 // But OpenCV for java doesn't have the method "imshow", so, we got to use  
 // java for that (drawImage) that uses Image or BufferedImage.  
 // So, how to go from one the other... Here is the way...  
 import java.awt.*;  
 import java.awt.image.BufferedImage;  
 import javax.swing.*;  
 import org.opencv.core.Mat;  
 import org.opencv.highgui.VideoCapture;  
 public class Panel extends JPanel{  
   private static final long serialVersionUID = 1L;  
   private BufferedImage image;  
   // Create a constructor method  
   public Panel(){  
     super();  
   }  
   private BufferedImage getimage(){  
     return image;  
   }  
   private void setimage(BufferedImage newimage){  
     image=newimage;  
     return;  
   }  
   /**  
    * Converts/writes a Mat into a BufferedImage.  
    *  
    * @param matrix Mat of type CV_8UC3 or CV_8UC1  
    * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY  
    */  
   public static BufferedImage matToBufferedImage(Mat matrix) {  
     int cols = matrix.cols();  
     int rows = matrix.rows();  
     int elemSize = (int)matrix.elemSize();  
     byte[] data = new byte[cols * rows * elemSize];  
     int type;  
     matrix.get(0, 0, data);  
     switch (matrix.channels()) {  
       case 1:  
         type = BufferedImage.TYPE_BYTE_GRAY;  
         break;  
       case 3:  
         type = BufferedImage.TYPE_3BYTE_BGR;  
         // bgr to rgb  
         byte b;  
         for(int i=0; i<data.length; i=i+3) {  
           b = data[i];  
           data[i] = data[i+2];  
           data[i+2] = b;  
         }  
         break;  
       default:  
         return null;  
     }  
     BufferedImage image2 = new BufferedImage(cols, rows, type);  
     image2.getRaster().setDataElements(0, 0, cols, rows, data);  
     return image2;  
   }  
   public void paintComponent(Graphics g){  
      BufferedImage temp=getimage();  
      g.drawImage(temp,10,10,temp.getWidth(),temp.getHeight(), this);  
   }  
   public static void main(String arg[]){  
    // Load the native library.  
    System.loadLibrary("opencv_java245");    
    JFrame frame = new JFrame("BasicPanel");  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame.setSize(400,400);  
    Panel panel = new Panel();  
    frame.setContentPane(panel);       
    frame.setVisible(true);       
    Mat webcam_image=new Mat();  
    BufferedImage temp;  
    VideoCapture capture =new VideoCapture(0);  
    if( capture.isOpened())  
     {  
      while( true )  
      {  
        capture.read(webcam_image);  
        if( !webcam_image.empty() )  
         {  
           frame.setSize(webcam_image.width()+40,webcam_image.height()+60);  
           temp=matToBufferedImage(webcam_image);  
           panel.setimage(temp);  
           panel.repaint();  
         }  
         else  
         {  
           System.out.println(" --(!) No captured frame -- Break!");  
           break;  
         }  
        }  
       }  
       return;  
   }  
 }  

And as we are here, let's finish with the tracking of face and eyes... Tomorrow :)
PS.: Click here to see the index of these series of posts on OpenCV

31 comments:

  1. hey thank you. it's very usefull document. it helps me a lot. i use 2.4.6. i want to give a little trick for BufferedImage. see you soon.

    public BufferedImage getImage(Mat mat){
    MatOfByte mb = new MatOfByte();
    Highgui.imencode(".jpg", mat, mb);
    BufferedImage bufferedImage = null;
    try {
    bufferedImage = ImageIO.read(new ByteArrayInputStream(mb.toArray()));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return bufferedImage;
    }

    ReplyDelete
  2. import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import org.opencv.core.Point;
    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.core.MatOfRect;
    import org.opencv.core.Rect;
    import org.opencv.core.Scalar;
    import org.opencv.highgui.Highgui;
    import org.opencv.highgui.VideoCapture;
    import org.opencv.objdetect.CascadeClassifier;

    public class Panel extends JPanel{
    private static final long serialVersionUID = 1L;
    private BufferedImage image;

    // Create a constructor method
    public Panel(){
    super();

    }
    private BufferedImage getimage(){
    return image;
    }
    private void setimage(BufferedImage newimage){
    image=newimage;
    return;
    }
    /**
    * Converts/writes a Mat into a BufferedImage.
    *
    * @param matrix Mat of type CV_8UC3 or CV_8UC1
    * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
    */
    public static BufferedImage matToBufferedImage(Mat matrix) {
    int cols = matrix.cols();
    int rows = matrix.rows();
    int elemSize = (int)matrix.elemSize();
    byte[] data = new byte[cols * rows * elemSize];
    int type;
    matrix.get(0, 0, data);
    switch (matrix.channels()) {
    case 1:
    type = BufferedImage.TYPE_BYTE_GRAY;
    break;
    case 3:
    type = BufferedImage.TYPE_3BYTE_BGR;
    // bgr to rgb
    byte b;
    for(int i=0; i<data.length; i=i+3) {
    b = data[i];
    data[i] = data[i+2];
    data[i+2] = b;
    }
    break;
    default:
    return null;
    }
    BufferedImage image2 = new BufferedImage(cols, rows, type);
    image2.getRaster().setDataElements(0, 0, cols, rows, data);
    return image2;
    }
    public void paintComponent(Graphics g){
    BufferedImage temp=getimage();
    g.drawImage(temp,10,10,temp.getWidth(),temp.getHeight(), this);
    }
    public static void main(String arg[]){
    // Load the native library.
    System.loadLibrary("opencv_java246");
    JFrame frame = new JFrame("BasicPanel");
    CascadeClassifier faceDetector = new CascadeClassifier("./src/main/resources/lbpcascade_frontalface.xml");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    Panel panel = new Panel();
    frame.setContentPane(panel);
    frame.setVisible(true);
    Mat webcam_image=new Mat();
    BufferedImage temp;
    VideoCapture capture =new VideoCapture(0);
    if( capture.isOpened())
    {
    while( true )
    {
    capture.read(webcam_image);

    if( !webcam_image.empty() )
    {
    frame.setSize(webcam_image.width()+40,webcam_image.height()+60);


    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(webcam_image, faceDetections);

    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

    // Draw a bounding box around each face.
    for (Rect rect : faceDetections.toArray()) {
    Core.rectangle(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
    }



    temp=matToBufferedImage(webcam_image);
    panel.setimage(temp);
    panel.repaint();
    }
    else
    {
    System.out.println(" --(!) No captured frame -- Break!");
    break;
    }
    }
    }
    return;
    }
    }

    ReplyDelete
    Replies
    1. Hi Genaro! Thanks for posting your version!

      Delete
    2. It uses almost 300% of my CPU! is there anyway to reduce that?

      Delete
  3. Hi Friend, I looked over your Blog and i Found it Informative, I am Student of Computer Science and its my Last Term, My Project is on face detection(just a brief name), i am going to do that with Java and openCv as i looked on your Blog , that can Help me, But the Problem is that i Don't Know that From Where to take start(As i am new to computer vision and image processing), as many of your Tutorials are mixed up :) :) there is Andriod and C++ and others under the Java Tab and Same with OpenCv Tab, So i will be very thankful to you if you give me some guidance regarding from where to take Start. Thanks
    Adnan Ahmad Khan

    ReplyDelete
    Replies
    1. Hello, please check http://cell0907.blogspot.com/2013/11/opencv-how-to-get-started-and-index-of.html and let me know!

      Delete
  4. You are right on! I got to create some kind of meaningful index of all this. Will try to post it this weekend :)

    ReplyDelete
    Replies
    1. That Will Be good, and will be easy for New Students to search and read according to their Rquirements: =D, i am waiting for yours Arrangement

      Delete
    2. Hi, not sure you saw it, because I mentioned it on your original reply, but I posted http://cell0907.blogspot.com/2013/11/opencv-how-to-get-started-and-index-of.html, which I thought may be better than an index... Let me know...

      Delete
    3. Hi!, Thanks .. I was busy in my course work thats why i was not able to come here, , Ya Pretty much Sure.. This view is now better then the previous one.. I will Inform for further modifications if Required :)

      Delete
  5. I use 2.4.6
    I got the following exception while trying to run your code.

    Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0
    at java.awt.image.SampleModel.(Unknown Source)
    at java.awt.image.ComponentSampleModel.(Unknown Source)
    at java.awt.image.PixelInterleavedSampleModel.(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.(Unknown Source)
    at Panel.matToBufferedImage(Panel.java:49)
    at Panel.(Panel.java:58)
    at Panel.main(Panel.java:82)


    I had modified the code a bit :

    // Import the basic graphics classes.
    // The problem here is that we read the image with OpenCV into a Mat object.
    // But OpenCV for java doesn't have the method "imshow", so, we got to use
    // java for that (drawImage) that uses Image or BufferedImage (?)
    // So, how to go from one the other...
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.lang.reflect.Field;

    import javax.swing.*;
    //import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.highgui.Highgui;
    public class Panel extends JPanel{
    private static final long serialVersionUID = 1L;
    String pic_name="resources/DSC01638.jpg";
    Mat picture;
    BufferedImage image;
    /**
    * Converts/writes a Mat into a BufferedImage.
    *
    * @param matrix Mat of type CV_8UC3 or CV_8UC1
    * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
    */
    public static BufferedImage matToBufferedImage(Mat matrix) {
    int cols = matrix.cols();
    int rows = matrix.rows();
    int elemSize = (int)matrix.elemSize();
    byte[] data = new byte[cols * rows * elemSize];
    int type;
    matrix.get(0, 0, data);
    switch (matrix.channels()) {
    case 1:
    type = BufferedImage.TYPE_BYTE_GRAY;
    break;
    case 3:
    type = BufferedImage.TYPE_3BYTE_BGR;
    // bgr to rgb
    byte b;
    for(int i=0; i<data.length; i=i+3) {
    b = data[i];
    data[i] = data[i+2];
    data[i+2] = b;
    }
    break;
    default:
    return null;
    }
    BufferedImage image = new BufferedImage(cols, rows, type);
    image.getRaster().setDataElements(0, 0, cols, rows, data);
    return image;
    }
    // Create a constructor method
    public Panel(){
    super(); // Calls the parent constructor
    picture = Highgui.imread(pic_name);
    // Got to cast picture into Image
    image=matToBufferedImage(picture);
    }
    public void paintComponent(Graphics g){
    g.drawImage(image,50,10,400,400, this);
    }
    public static void main(String arg[]){
    System.setProperty( "java.library.path", "D:/opencv/build/java/x64/" );
    try{
    Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
    try {
    fieldSysPath.setAccessible(true);
    fieldSysPath.set(System.class.getClassLoader(), null);
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    }

    } catch (java.lang.NoSuchFieldException e) {

    }
    // Load the native library.
    System.loadLibrary("opencv_java246");
    JFrame frame = new JFrame("BasicPanel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    Panel panel = new Panel();
    frame.setContentPane(panel);
    frame.setVisible(true);
    }
    }


    Can you please help me solve this. I really need to do this. It is part of my final year engineering project

    ReplyDelete
    Replies
    1. Looking through your code, I see that you added code to find the path to the library... right? I mean, the only new portion is this:
      System.setProperty( "java.library.path", "D:/opencv/build/java/x64/" );
      try{
      Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
      try {
      fieldSysPath.setAccessible(true);
      fieldSysPath.set(System.class.getClassLoader(), null);
      } catch (IllegalAccessException e) {
      e.printStackTrace();
      }
      } catch (java.lang.NoSuchFieldException e) {
      }

      When I comment that out from your code, it works for me. Nevertheless, I don't think your issue is coming from that... More complaining about Width/Height being zero. Can you double check that you got a picture with the right name, in the right path, that the code can open?

      Delete
  6. This is what i get when i run the UPDATE:.... code :

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at test.Test.paintComponent(Test.java:66)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1000(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    --(!) No captured frame -- Break!

    line 66 is : g.drawImage(temp,10,10,temp.getWidth(),temp.getHeight(), this);

    ReplyDelete
    Replies
    1. I had this error too, did you manage to sort it?

      Delete
    2. if (this.image==null) return;
      Add this in paintComponent before drawImage

      Delete
  7. Hi, excellent post.

    I just think System.arraycopy will give you a better performance (2 times) over raster's setDataElements:

    public Image toBufferedImageArrayCopy(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;

    }

    ReplyDelete
    Replies
    1. Hi, sorry for late reply. I think you are right. Somebody had suggested the same, so I had look at it here: http://cell0907.blogspot.com.es/2013/12/from-mat-to-bufferedimage.html. I think you are talking about method 4 there, which is the winner... :)

      Delete
  8. I've run into an issue using this code for the starting point of my project. I've gotten to the point where I create a JFrame with a button to start capture video from a specified video source, but the Frame freezes when I run the above code. I'm wondering, is it not possible to run the image processing code on the same thread as the UI? Should I be using a SwingWorker or something to resolve this issue?

    ReplyDelete
  9. Hi again!
    I ran this program which aims to display the webcam, but it gives me errors. I' trying to describe it in 2 comments, becouse I exceed the limit of characters that I'm allowed to enter.
    The webcam led is on, the white "BasicPanel" appeared and I have so ugly errors:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at imgv1.Panel.paintComponent(Panel.java:66)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1100(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    ReplyDelete
    Replies
    1. .....In continuation of the above errors:
      Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at imgv1.Panel.paintComponent(Panel.java:66)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JLayeredPane.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paintToOffscreen(Unknown Source)
      at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
      at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
      at javax.swing.RepaintManager.paint(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
      at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
      at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
      at java.awt.Container.paint(Unknown Source)
      at java.awt.Window.paint(Unknown Source)
      at javax.swing.RepaintManager$3.run(Unknown Source)
      at javax.swing.RepaintManager$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.access$1100(Unknown Source)
      at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
      at java.awt.event.InvocationEvent.dispatch(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$400(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)

      Delete
    2. .....
      Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at imgv1.Panel.paintComponent(Panel.java:66)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JLayeredPane.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paintToOffscreen(Unknown Source)
      at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
      at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
      at javax.swing.RepaintManager.paint(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
      at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
      at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
      at java.awt.Container.paint(Unknown Source)
      at java.awt.Window.paint(Unknown Source)
      at javax.swing.RepaintManager$3.run(Unknown Source)
      at javax.swing.RepaintManager$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.access$1100(Unknown Source)
      at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
      at java.awt.event.InvocationEvent.dispatch(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$400(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
      --(!) No captured frame -- Break!
      At at imgv1.Panel.paintComponent(Panel.java:66) I have this line: g.drawImage(temp,10,10,temp.getWidth(),temp.getHeight(), this);
      I also tried the Can u, please, help me to debug these errors?

      Delete
    3. if (temp != null)
      g.drawImage(temp, 10, 10, temp.getWidth(), temp.getHeight(), this);

      // To solve null pointer error

      Delete
    4. Razvan if you have solved it, please share how you solved it

      Delete
  10. Wonderful post!
    Now i have a problem. I need to take a snapshot from the streaming when i click a JButton.
    How can i do this?

    tnx

    ReplyDelete
  11. hello when i execute the program i have this message


    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0
    at java.awt.image.SampleModel.(Unknown Source)
    at java.awt.image.ComponentSampleModel.(Unknown Source)
    at java.awt.image.PixelInterleavedSampleModel.(Unknown Source)
    at java.awt.image.Raster.createInterleavedRaster(Unknown Source)
    at java.awt.image.Raster.createInterleavedRaster(Unknown Source)
    at java.awt.image.BufferedImage.(Unknown Source)
    at ocv2.Mat2Image.getSpace(Mat2Image.java:26)
    at ocv2.Mat2Image.getImage(Mat2Image.java:31)
    at ocv2.c1.getOneFrame(c1.java:22)
    at ocv2.MyFrame.paint(MyFrame.java:46)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1100(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    ReplyDelete
  12. Hi Cell,

    Sorry to bump an old thread but was just passing through, saw your tests and thought I'd offer another time saving:

    You can speed up the operation even more by removing the need for an intermediate byte array altogether, instead you just pull the data out of the Mat and directly into the BufferedImage's own data byte[], no 2nd copy needed.

    Like this:

    public static BufferedImage convertMatToBufferedImage(Mat m) {
    int chans = m.channels();
    int bufType = chans > 1 ? BufferedImage.TYPE_3BYTE_BGR : BufferedImage.TYPE_BYTE_GRAY;
    BufferedImage bufImg = new BufferedImage(m.cols(), m.rows(), bufType);
    byte data[] = ((DataBufferByte) bufImg.getRaster().getDataBuffer()).getData();
    m.get(0, 0, data); // move all the pixels directly into the target buffer
    return bufImg;
    }

    Nick.

    PS: The 1st method is so horrible (and also lossy!) it makes me cringe every time I see people doing things like this.

    Been writing image processing apps in java for 20 years. Kind of leaves a mark on my soul.

    ReplyDelete
    Replies
    1. Hey Nick! Really appreciate you stopping by and spending the time to post this! A humbling honour. That's the right attitude :)

      Delete
  13. Hi Cell0907,

    I appreciate this post, it has provided a lot of information. I am relatively very new to image processing and you have made sure I understand every tiny step.

    This particular code is not working for me.

    VideoCapture capture =new VideoCapture(0);
    if( capture.isOpened()) // <-- is always returning false for me.

    I tried stackoverflow to find for possible solution, but none of them worked. I would really appreciate if you could help me out here.

    I'm using OpenCv 2.4.10
    Windows 8
    Eclipse

    ReplyDelete
  14. hiii,
    I tried to execute my code and i double checked all the setting or external jars libraries and still i m not able to execute the code
    and the error is:
    Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_1(Ljava/lang/String;)J
    at org.opencv.highgui.Highgui.imread_1(Native Method)
    at org.opencv.highgui.Highgui.imread(Highgui.java:369)
    at Denoised.Denoise(Denoised.java:12)
    at MainEntry.gray_actionPerformed(MainEntry.java:384)
    at MainEntry$SymAction.actionPerformed(MainEntry.java:299)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    please can u help me to solve the problem

    ReplyDelete