使用Java将图像保存到Mat?

时间:2021-10-04 16:34:23

Some bmp and tif image files cannot be read using the following method, except for the jpeg files. And I want to save it in opencv's Mat structure. What should I do? And I want to convert it into BufferedImage for further processing.

除了jpeg文件之外,使用以下方法无法读取某些bmp和tif图像文件。我想将它保存在opencv的Mat结构中。我该怎么办?我想将其转换为BufferedImage以进行进一步处理。

File input = new File("C:\\File\\1.tif");
BufferedImage image = ImageIO.read(input);         
byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();            
Mat img = new Mat(image.getHeight(),image.getWidth(), CvType.CV_8UC3);
img.put(0, 0, data);            
Imgcodecs.imwrite("C:\\File\\input.jpg", img);

1 个解决方案

#1


3  

It is all because the data type of the different images differ. For one you have DataBufferByte, for other you may have DataBufferInt.

这是因为不同图像的数据类型不同。对于一个你有DataBufferByte,对于其他你可能有DataBufferInt。

You can create an new BufferedImage of same size with type 3BYTE_BGR, and then draw the original image into it, then you can construct a Mat from this new one.

您可以使用3BYTE_BGR类型创建相同大小的新BufferedImage,然后将原始图像绘制到其中,然后您可以从这个新的构建Mat。

You can also use different supported Mat image type instead of CvType.CV_8UC3, but that depends if there are equivalent types for java ones.

您还可以使用不同的受支持的Mat图像类型而不是CvType.CV_8UC3,但这取决于是否存在Java等效类型。

This is for the approach with conversion:

这是针对转换的方法:

File input = new File("C:\\File\\1.tif");

BufferedImage image = ImageIO.read(input);
// Here we convert into *supported* format
BufferedImage imageCopy =
    new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
imageCopy.getGraphics().drawImage(image, 0, 0, null);

byte[] data = ((DataBufferByte) imageCopy.getRaster().getDataBuffer()).getData();  
Mat img = new Mat(image.getHeight(),image.getWidth(), CvType.CV_8UC3);
img.put(0, 0, data);           
Imgcodecs.imwrite("C:\\File\\input.jpg", img);

In the approach presented above you are delegating all the "conversion stuff" to the java BufferedImage and Graphics implementations. It is the easiest approach to have some standardized image format for any image. There is also another approach to tell java to directly load image as concrete type, but I don't remember the code right now, and it is far more complicated than this.

在上面介绍的方法中,您将所有“转换内容”委托给java BufferedImage和Graphics实现。这是为任何图像提供一些标准化图像格式的最简单方法。还有另一种方法告诉java直接加载图像作为具体类型,但我现在不记得代码,它比这复杂得多。

#1


3  

It is all because the data type of the different images differ. For one you have DataBufferByte, for other you may have DataBufferInt.

这是因为不同图像的数据类型不同。对于一个你有DataBufferByte,对于其他你可能有DataBufferInt。

You can create an new BufferedImage of same size with type 3BYTE_BGR, and then draw the original image into it, then you can construct a Mat from this new one.

您可以使用3BYTE_BGR类型创建相同大小的新BufferedImage,然后将原始图像绘制到其中,然后您可以从这个新的构建Mat。

You can also use different supported Mat image type instead of CvType.CV_8UC3, but that depends if there are equivalent types for java ones.

您还可以使用不同的受支持的Mat图像类型而不是CvType.CV_8UC3,但这取决于是否存在Java等效类型。

This is for the approach with conversion:

这是针对转换的方法:

File input = new File("C:\\File\\1.tif");

BufferedImage image = ImageIO.read(input);
// Here we convert into *supported* format
BufferedImage imageCopy =
    new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
imageCopy.getGraphics().drawImage(image, 0, 0, null);

byte[] data = ((DataBufferByte) imageCopy.getRaster().getDataBuffer()).getData();  
Mat img = new Mat(image.getHeight(),image.getWidth(), CvType.CV_8UC3);
img.put(0, 0, data);           
Imgcodecs.imwrite("C:\\File\\input.jpg", img);

In the approach presented above you are delegating all the "conversion stuff" to the java BufferedImage and Graphics implementations. It is the easiest approach to have some standardized image format for any image. There is also another approach to tell java to directly load image as concrete type, but I don't remember the code right now, and it is far more complicated than this.

在上面介绍的方法中,您将所有“转换内容”委托给java BufferedImage和Graphics实现。这是为任何图像提供一些标准化图像格式的最简单方法。还有另一种方法告诉java直接加载图像作为具体类型,但我现在不记得代码,它比这复杂得多。