问题
I am storing BufferedImages inside a MySQL database and retrieve them to a java application.
The BufferedImages are of type TYPE_INT_RGB.
How can i convert that image to a OpenCV Mat object?
I do always get a
java.lang.UnsupportedOperationException: Mat data type is not compatible:
Exception.
Can somebody help?
回答1:
Got it on my own.
int[] data = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(data);
Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
mat.put(0, 0, byteBuffer.array());
return mat;
image is the BufferedImage, you want to convert to a Mat-Object.
回答2:
A much more simplified version of tellob's orignal anwser.
public static Mat mat2(BufferedImage image)
{
byte[] data = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
mat.put(0, 0, data);
return mat;
}
来源:https://stackoverflow.com/questions/21175336/convert-bufferedimage-type-int-rgb-to-opencv-mat-object