Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JavaFX, BufferedImage to Mat, OpenCV

3723a45c-2ce3-45b6-a56f-2fb77e21d8bdMar 16 2015 — edited Mar 17 2015

Hi, Im new to JavaFX and OpenCV. The Problem is, that JavaFX can't handle Mat objects. First I load an image as a BufferedImage. Then i have to convert this to Mat. Do my image Processing with OpenCV and again convert the result image to a BufferedImage to display in on my UI. Here are the methods for converting Buff to Mat and Mat to Buff I found on the internet:

public static Mat img2Mat(BufferedImage image)

    {

  byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();

  Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);

  mat.put(0, 0, data);

  return mat;

     }

public static BufferedImage mat2Img(Mat mat) {

     BufferedImage image = new BufferedImage(mat.width(), mat.height(), BufferedImage.TYPE_3BYTE_BGR);

     WritableRaster raster = image.getRaster();

     DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();

     byte[] data = dataBuffer.getData();

     mat.get(0, 0, data);

     return image;

  }

Next I do some OpenCV stuff:

public static BufferedImage hello(BufferedImage img) {

   

  Mat source  = img2Mat(img); //Convert Buff to Mat

  BufferedImage outImg = null;

       try{

          System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

          //Mat source = Highgui.imread("chart.png",  Highgui.CV_LOAD_IMAGE_COLOR);

          Mat destination = new Mat(source.rows(),source.cols(),source.type());

         

          destination = source;

          int erosion_size = 5;

  

          Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new  Size(2*erosion_size + 1, 2*erosion_size+1));

          Imgproc.erode(source, destination, element);

         

          outImg = mat2Img(destination); //Convert Mat to Buff

   

       }catch (Exception e) {

          System.out.println("error: " + e.getMessage());

       }

      

       return outImg;

    }

Finally I call the hello method in my controller:

void menuItemTestFired(ActionEvent event) {

   

    try {

              result = ImageProc.hallo(bImage);//bImage is an image I've loaded before.

             

              imageView.setImage(SwingFXUtils.toFXImage(result, null));

           

              Main.primary.sizeToScene();

           } catch (Exception ex) {

    ex.printStackTrace();

             }

    

     }

Unfortunately I get a lot of errors. Here are the first one:

Caused by: java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat(III)J

  at org.opencv.core.Mat.n_Mat(Native Method)

  at org.opencv.core.Mat.<init>(Mat.java:471)

public Mat(int rows, int cols, int type)

    {

        nativeObj = n_Mat(rows, cols, type);// This is th line 471

        return;

    }

  at application.ImageProc.img2Mat(ImageProc.java:282)

// 282 is the line NO.4 in the img2Mat method above.


  at application.ImageProc.hallo(ImageProc.java:344)

//344 is then line NO. 3 in the hello method above.

Can't figure out why this doesn't work. -.-

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 14 2015
Added on Mar 16 2015
2 comments
6,619 views