I use JavaCV (not OpenCV). My goal is to obtain a Mat
object from an image that is stored as a Resource. Then I'm going to pass this Mat
into opencv_imgproc.matchTemplate
method. I've managed to write this bad code:
InputStream in = getClass().getResourceAsStream("Lenna32.png");
BufferedImage image = ImageIO.read(in);
Frame f = new Java2DFrameConverter().getFrame(image);
Mat mat = new OpenCVFrameConverter.ToMat().convert(f);
This works in some cases. The problems are:
For png images that has transparency channel (that is 32BPP), it shifts channels, so that
R=00 G=33 B=66 A=FF
turns toR=33 G=66 B=FF
On my target environment, I can't use
ImageIO
- There are too many object conversions
InputStream -> BufferedImage -> Frame -> Mat
. I feel like there should be a simple and effective way to do this.
What is the best way to create Mat from a Resource?