Unable to save BufferedImage to file and compare it to original source

Viewed 50

I'm creating method which get frames from MP4 video, crop them and compare certain areas with presaved sample set.

How I've got frame:

import org.jcodec.api.FrameGrab;

Picture picture = null;
BufferedImage wholePicture;
    try {
        picture = FrameGrab.getFrameFromFile(new File(filename), i * FRAMERATE);
    } catch (JCodecException e) {
    // log messages
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    wholePicture = AWTUtil.toBufferedImage(picture);

then crop it

BufferedImage rightSample = wholePicture.getSubimage(1190, 10, 143, 99);

and how save it for future usage (to sample set)

ImageIO.write(rightSample, "png", new File("heavy_06_01.png"))

Saved sample loading:

HEAVY_06_01 = ImageIO.read(new File("heavy_06_01.png"));

My problem is this saved and reopened BufferedImage is not the same as his in-memory origin. img.getRaster().getDataBuffer()).getData(); for loaded from disk and in-memory will be totally different (so zero chance to have adequate similarity for images are the same for human)

For ImageIO.write I've also tried jpg and bmp but it doesn't matter

UPD:
Cropped BufferedImage on the left, saved sample on the right
Don't pay attention to 24/32bit - when sample file is loaded back - new BufferedImage will be 32bit too.

Main issue is that BufferedImage.getSubimage() doesn't affect BufferedImage.raster.dataBuffer.data bytearray size. It is 1920 * 1080 * 3 bytes before and after cropping. And only "save&load" will crop bytearray to the real "heigth * width" size 143 * 99 * 3 bytes

0 Answers
Related