[quote] Converting Java BufferedImage to OpenCV Mat and vice versa

http://enfanote.blogspot.com/2013/06/converting-java-bufferedimage-to-opencv.html

BufferedImage to Mat

Note: this only works if the image is of type BufferedImage.TYPE_3BYTE_BGR

BufferedImage image = myBufferedImage;
byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
Mat mat = new Mat(image.getHeight(), image.getWidth, CvType.CV_8UC3);
mat.put(0, 0, data);

Mat to BufferedImage

Mat mat = myMat;
byte[] data = new byte[mat.rows()*mat.cols()*(int)(mat.elemSize())];
mat.get(0, 0, data);
if (mat.channels() == 3) {
for (int i = 0; i < data.length; i += 3) {
byte temp = data[i];
data[i] = data[i + 2];
data[i + 2] = temp;
}
}
BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), BufferedImage.TYPE_3BYTE_BGR);
image.getRaster().setDataElements(0, 0, mat.cols(), mat.rows(), data);

此条目发表在 Android 分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>