How to load BufferedImage into opencv_core.Mat?

431 views
Skip to first unread message

Darrel Riekhof

unread,
May 2, 2014, 11:56:46 AM5/2/14
to jav...@googlegroups.com
Using latest bytedeco stuff with opencv 2.4.9

I'm trying to do this:

BufferedImage bi = VeraImgUtils.matToBufferedImage(matFace, null);
try {
bi = ImageIO.read(new File("C:/dev/workspace-kgicv/JavaCV-samples/src/Rice.jpg"));
} catch (IOException e) {
e.printStackTrace();
}

JOptionPane.showMessageDialog(null, "", "face bi", JOptionPane.INFORMATION_MESSAGE,
new ImageIcon(bi));
mm.copyFrom(bi);  // line 190

BufferedImage is loaded and shows up in the JOptionPane.

I'm getting this stacktrace:

Exception in thread "Thread-4" java.lang.NullPointerException
at org.bytedeco.javacpp.helper.opencv_core$AbstractArray.getByteBuffer(opencv_core.java:164)
at org.bytedeco.javacpp.helper.opencv_core$AbstractArray.copyFrom(opencv_core.java:599)
at org.bytedeco.javacpp.helper.opencv_core$AbstractArray.copyFrom(opencv_core.java:594)
at org.bytedeco.javacpp.helper.opencv_core$AbstractArray.copyFrom(opencv_core.java:586)
at org.bytedeco.javacpp.helper.opencv_core$AbstractArray.copyFrom(opencv_core.java:583)
at com.foo.face.FaceClassifier.detect(FaceClassifier.java:190)
        ...
at java.lang.Thread.run(Thread.java:745)


Darrel Riekhof

unread,
May 2, 2014, 12:07:10 PM5/2/14
to jav...@googlegroups.com
Well, I guess nevermind.  Looks like it works if I give the opencv_core.Mat constructor the width, height, and type:

    opencv_core.Mat mm = new opencv_core.Mat(matFace.rows(), matFace.cols(), matFace.type());

It would be nice if I didn't need to provide those--pretty sure the OpenCV java bindings don't require that when using copyTo().  But not a big deal.

Samuel Audet

unread,
May 2, 2014, 8:25:06 PM5/2/14
to jav...@googlegroups.com
On 05/03/2014 01:07 AM, Darrel Riekhof wrote:
> Well, I guess nevermind. Looks like it works if I give the opencv_core.Mat constructor the width, height, and type:
>
> opencv_core.Mat mm = new opencv_core.Mat(matFace.rows(), matFace.cols(), matFace.type());
>
> It would be nice if I didn't need to provide those--pretty sure the OpenCV java bindings don't require that when using copyTo(). But not a big deal.

There's Mat.createFrom() that should do what you need:
https://github.com/bytedeco/javacpp-presets/blob/master/opencv/src/main/java/org/bytedeco/javacpp/helper/opencv_core.java#L2300
But whoops, I forgot to put them static. I'll be fixing that.

If possible, please open an issue here (just copy/paste), thanks!
https://github.com/bytedeco/javacpp-presets/issues

Samuel

Samuel Audet

unread,
May 6, 2014, 5:59:46 AM5/6/14
to jav...@googlegroups.com
Issue and fix reported here:
https://github.com/bytedeco/javacpp-presets/issues/1
Thanks for your input!

Samuel

Reply all
Reply to author
Forward
0 new messages