0

I'm using ImageMagick compare to calculate the difference between two images every five minutes on a Raspberry Pi. This takes almost a minute, and I suspect much of the CPU load is due to the generation of the difference image. All I need is the result, not the image. But if I don't supply the name of the result image, I get an error. Isn't this possible?

2

I was struggling with the same issue right now and found the answer: Yes!

TL; DR: You specify NULL: as the filename for the diff, i.e.

compare -metric rmse foo.png bar.png NULL:

ImageMagick's documentation

ImageMagick has support for specifying a number of different output formats. One of those are x: which opens a window for the user to immediately view the difference between the images.

You can read more about this in the documentation of the compare program:

Rather than saving the 'compare' image, you can of course view it directly, which I find more convenient, by output to the special "x:" output format, or using the "display" program. For example..

compare bag_frame1.gif bag_frame2.gif x:

Digging deeper into ImageMagicks documentation for image formats brought me to the list of "Image formats" (look under the header "Pseudo-image Formats"), which specifies the NULL image format as

Useful for creating blank tiles with montage (use NULL:). Also useful as an output format when evaluating image read performance.

Performance boost

As a sidenote, I also compared the time for rendering to PNG, GIF, TGA, and NULL: 100 times to really verify that the NULL format is quicker. Note that I'm piping stderr (2) to /dev/null just to suppress terminal output from compare.

Outputting to a PNG (~0.085 seconds):

time for n in {1..100}; do compare -metric rmse foo.png bar.png out.png 2>/dev/null ; done

real    0m3,036s
user    0m7,582s
sys     0m0,911s

Outputting to a GIF (~0.147 seconds):

time for n in {1..100}; do compare -metric rmse foo.png bar.png out.gif 2>/dev/null ; done

real    0m8,951s
user    0m11,760s
sys     0m2,918s

Outputting to a TGA (~0.094 seconds):

$ time for n in {1..100}; do compare -metric rmse foo.png bar.png out.tga 2>/dev/null ; done

real    0m4,623s
user    0m8,466s
sys     0m0,981s

Outputting to NULL: (~0.067 seconds):

time for n in {1..100}; do compare -metric rmse foo.png bar.png NULL: 2>/dev/null ; done

real    0m1,967s
user    0m5,797s
sys     0m0,920s

Conclusion

Outputting to NULL: is decisively faster than other output formats, though marginally faster than creating a PNG. This is probably because ImageMagick needs to create an internal diff image for the comparison regardless if it is written to a file or not, and the overhead for encoding it into a PNG file and writing it to disk seems to not be very demanding in my small test case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.