I am trying to get frames from a gif using OpenCV. I found Convert each animated GIF frame to a separate BufferedImage and used the second suggestion. I modified it slightly to return an array of Mats instead of BufferedImages.
I tried two methods to get bufferedImages
from the gif. Each presented different problems.
With the previous thread's suggestion
BufferedImage fImage=ir.read(i);
The program calls a "ArrayIndexOutOfBoundsException: 4096"
With the original code from the previous thread.
BufferedImage fImage=ir.getRawImageType(i).createBufferedImage(ir.getWidth(i),ir.getHeight(i));
Each frame is a monotone color(not all black though) and the mat derived from the BufferedImage is empty.
System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); ArrayList<Mat> frames = new ArrayList<Mat>(); ImageReader ir = new GIFImageReader(new GIFImageReaderSpi()); ir.setInput(ImageIO.createImageInputStream(new File("ronPaulTestImage.gif"))); for(int i = 0; i < ir.getNumImages(true); i++){ BufferedImage fImage=ir.read(i); //BufferedImage fImage=ir.getRawImageType(i).createBufferedImage(ir.getWidth(i), ir.getHeight(i)); fImage = toBufferedImageOfType(fImage, BufferedImage.TYPE_3BYTE_BGR); //byte[] pixels = ((DataBufferByte) r.getRaster().getDataBuffer()).getData(); Mat m=new Mat(); //m.put(0,0,pixels); m.put(0, 0,((DataBufferByte) fImage.getRaster().getDataBuffer()).getData()); if(i==40){ //a test, writes the mat and the image at the specified frame to files, exits ImageIO.write(fImage,"jpg",new File("TestError.jpg")); Imgcodecs.imwrite("TestErrorMat.jpg",m); System.exit(0); }
Here is the gif I used