Converting `BufferedImage` to `Mat` in OpenCV

后端 未结 9 1901
北荒
北荒 2020-11-29 04:49

How can I convert a BufferedImage to a Mat in OpenCV?

I\'m using the JAVA wrapper for OpenCV(not JavaCV

9条回答
  •  忘掉有多难
    2020-11-29 05:03

    This one worked fine for me, and it takes from 0 to 1 ms to be performed.

    public static Mat bufferedImageToMat(BufferedImage bi) {
      Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC3);
      byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
      mat.put(0, 0, data);
      return mat;
    }
    

提交回复
热议问题