How to convert BufferedImage to IplImage?

570 views
Skip to first unread message

Stephan Beuze

unread,
Jun 28, 2015, 2:59:33 AM6/28/15
to jav...@googlegroups.com
I'm trying to detect some images on the screen. Here is my code:

// Get screen size
Rectangle fullScreenRectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

// Capture screen 
BufferedImage screenshot = new Robot().createScreenCapture(fullScreenRectangle);

// Convert BufferedImage into IplImage
Java2DFrameConverter frameBufferedImageConverter = new Java2DFrameConverter();
OpenCVFrameConverter.ToIplImage frameIplImageConverter = new OpenCVFrameConverter.ToIplImage(); 
 
Frame screenshotFrame = frameBufferedImageConverter.convert(screenshot);
IplImage screenshotIplImage = frameIplImageConverter.convert(screenshotFrame);

          // Use JavaCV to match template with captured screen ...

I'm getting this error:

java.lang.ClassCastException: java.nio.DirectByteBuffer cannot be cast to java.nio.IntBuffer


So I started using the Java2DFrameConverter patched version and I still can't capture the screen.
Here is what I expect...


... and this is what I get:


What am I doing wrong?



Samuel Audet

unread,
Jul 3, 2015, 7:41:35 PM7/3/15
to jav...@googlegroups.com
On 06/28/2015 03:59 PM, Stephan Beuze wrote:
> What am I doing wrong?

Probably nothing, there's some more fixes here:
https://github.com/bytedeco/javacv/commit/487954e1cb1e4dabfa74f7ff698dc2331259365d

Samuel

Samuel Audet

unread,
Jul 6, 2015, 8:48:37 AM7/6/15
to jav...@googlegroups.com
Actually, I've tried your code, and Robot returns images in ARGB format:

BufferedImage@179d3b25: type = 1 DirectColorModel: rmask=ff0000
gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width = 3200
height = 1080 #Bands = 3 xOff = 0 yOff = 0 dataOffset[0] 0

That's not going to work very well with OpenCV. We can convert that to,
for example, BGRA, which works much better with OpenCV, this way:

IplImage bgraImage = IplImage.create(screenshotIplImage.width(),
screenshotIplImage.height(), IPL_DEPTH_8U, 4);
cvMixChannels(screenshotIplImage, 1, bgraImage, 1, new int[] {0,3,
1,2, 2,1, 3,0}, 4);

Samuel

Stephan Beuze

unread,
Jul 8, 2015, 5:21:56 PM7/8/15
to jav...@googlegroups.com
Thanks for your replies Samuel.

Is it planned to add the additions below to Java2DFrameConverter ?

Samuel Audet

unread,
Jul 8, 2015, 8:15:12 PM7/8/15
to javacv

Not really, it would cause inconsistencies with other image formats. Besides, it's more efficient to use FFmpeg to capture from the screen these days...

Samuel

2015/07/09 6:21 "Stephan Beuze" <sbe...@gmail.com>:
--

---
You received this message because you are subscribed to the Google Groups "javacv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javacv+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Samuel Audet

unread,
Jul 9, 2015, 9:24:42 AM7/9/15
to javacv
Actually, there is already functionality for that. Calling
Java2DFrameConverter.getBufferedImage(frame, 1.0, true, null) should
produce the same result.

Samuel
Reply all
Reply to author
Forward
0 new messages