JavaFX, Mat BufferedImage, OpenCV

asked 2015-03-16 13:56:27 -0500

Mark24 gravatar image

Hi, I'm 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;

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

           } catch (Exception ex) {


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>(

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

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


at application.ImageProc.img2Mat( // 282 is the line NO.4 in the img2Mat method above.

at application.ImageProc.hallo( //344 is the line NO. 3 in the hello method above.

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

edit retag flag offensive close merge delete


static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

berak gravatar imageberak ( 2015-03-16 14:20:41 -0500 )edit