opencv-java / getting-started

Getting started with OpenCV and JavaFX
105 stars 176 forks source link

mat2Image #2

Closed maxizu closed 8 years ago

maxizu commented 8 years ago

Hey @luigidr at FXHelloCVController.java Lines 160-168 you convert a mat to a JavaFX Image. Sometimes this fct (random time) will not convert a mat correctly and the ImageViewer stops working(Will not receive a correct image anymore). Atm i dont know why, but i will find it out. I found this code which works well and is more performant. http://stackoverflow.com/questions/29120869/how-to-convert-matopencv-to-imagejavafx
Performance: Mat -> BufferedImage -> JavaFXImage http://cell0907.blogspot.de/2013/12/from-mat-to-bufferedimage.html -->

private Image mat2Image(Mat frame) {
try{
 return SwingFXUtils.toFXImage(matToBufferedImage(frame),null);
} catch (Exception e) {
 System.out.println("Cant convert mat");
 return null;
}
}
public BufferedImage matToBufferedImage(Mat matBGR){   
          int width = matBGR.width(), height = matBGR.height(), channels = matBGR.channels() ;  
          byte[] sourcePixels = new byte[width * height * channels];  
          matBGR.get(0, 0, sourcePixels);  
              //TYPE_BYTE_GRAY - gray images , TYPE_3BYTE_BGR - 3 Channles
          image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);  
          final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();  
          System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);  
          return image;  
}  
maxizu commented 8 years ago

Have a look at prq #4 . Fixed #2 and #3

luigidr commented 8 years ago

Hey @maxizu,

thanks for opening this issue. Since I have never come across this bug, can you provide more details? In particular, for curiosity and, possibly, for further testing:

I know that the piece of code present in the project is not performant, so your suggestion is welcomed. My only "concern" is that you need to import awt packages... but I can survive to this thing. :)

I would like to think about it for some time before modifying the code in the project, read the links you provided, and perform some tests. Then, I will propagate the modification to the other projects in this organization that uses the same method.

maxizu commented 8 years ago

Hey @luigidr , I am running OpenCV 3.1.0 , Java 1.8.0_91-b14 on ubuntu 14.04.4. I captured a lot of images from the webcam and sometimes the ImageViewer stopped working (not periodically). I debbuged the code and saved the image bevor and after the mat2Image function. The image which was saved at the beginning of the fct was fine. The Image which was saved after running the fct was (I think is was null). Since i changed the fct to the suggested one from blogspot i dont have any problems anymore.

luigidr commented 8 years ago

Perfect, thanks for the clarification. I never tried with OpenCV 3.1.0 nor with a Java version newer than 1.8.0_31.

luigidr commented 8 years ago

Solved by bc68dd37a5df6538eaa1a05fd895a79ca758777f.