我需要从之前拼接在一起的一张图像中提取单个图像并对其进行处理。例如,将四个独立的图像拼接成一个图像,如下所示:
作为图像处理的新手,我做了一些研究,发现Java Image IO适合我的要求。
这是我到目前为止写的:
图像提取与处理
ImageInputStream oldImage = ImageIO
.createImageInputStream(new FileInputStream("Four_Image_Stitched.jpeg"));
ImageReader imageReader = getImageReader(imageFormat);
imageReader.setInput(oldImage);
int nosOfImages = imageReader.getNumImages(true);
System.out.println("Number of Images = " + nosOfImages);
if (nosOfImages > 1) {
for (int i = 0; i < nosOfImages; i++) {
BufferedImage image = imageReader.read(i);
// do some processing
}
}
获取图像阅读器实例
private ImageReader getImageReader(String imageFormat) {
if ("PNG".equalsIgnoreCase(imageFormat)) {
return new PNGImageReader(new PNGImageReaderSpi());
}
if ("JPEG".equalsIgnoreCase(imageFormat)) {
return new JPEGImageReader(new JPEGImageReaderSpi());
}
if ("GIF".equalsIgnoreCase(imageFormat)) {
return new GIFImageReader(new GIFImageReaderSpi());
}
if ("BMP".equalsIgnoreCase(imageFormat)) {
return new BMPImageReader(new BMPImageReaderSpi());
}
return null;
}
这里的问题imageReader.getNumImages(true)
总是1
针对每种格式返回,而与拼接图像的数量无关;我想这应该是实际缝合的图像数量,特别4
是在提到的例子中。
I am not sure what exactly I am missing here!
Any help or pointers in this regard would be highly appreciated.
P.S.:
Number of images stitched into single are not know before hand i.e. a single image might contain n
number of stitched images, each with different dimensions.