Reputation: 4024
I need to extract individual images, and process them, from one single image which were stitched together previously. For e.g. four independent images are stitched to produce one image as shown below:
Being new to image processing I did some research and found Java Image IO suitable for my requirement.
This is what I wrote so far:
Image Extraction and Processing
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
}
}
Fetch Image Reader Instance
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;
}
The problem here is imageReader.getNumImages(true)
always return 1
for every format and irrespective of number of stitched images; which I suppose should be actual number of images stitched and specifically 4
in the mentioned example.
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.
Upvotes: 1
Views: 183
Reputation: 75986
You seem to believe that the stiched images are somehow stored separately inside the full image? That's not (almost certainly) the case, once the (say) four images are stiched into one image and saved, you have a single image -a matrix of pixels- and you cannot tell that it was composed of N images, unless you make some image processing heuristic algorithm to detect abrupt changes in colors, etc. getNumImages()
if only for some special image formats that allow several subimages inside, like "pages" in a text-document; normally they're frames in an animation.
In such a simple case like the example, a simple edge detector could suffice.
Upvotes: 1