Example Usage • Option Summary
Use the compare program to mathematically and visually annotate the difference between an image and its reconstruction. See Command Line Processing for advice on how to structure your compare command or see below for example usages of the command.
Example Usage
We list a few examples of the compare command here to illustrate its usefulness and ease of use. To get started, lets compare an image to one that's been sharpened:
$ magick rose.jpg -sharpen 0x1 reconstruct.jpg $ magick compare rose.jpg reconstruct.jpg difference.png $ magick compare -compose src rose.jpg reconstruct.jpg difference.png
The red areas of the difference image emphasizes (highlight) pixels that are affected by the image sharpening, whereas white de-emphasizes (lowlight) pixels that are untouched by the sharpening process.
In addition to the visual interpretation of the difference in an image and its reconstruction, we report a mathematical measure of the difference:
$ magick compare -verbose -metric rmse rose.jpg reconstruct.jpg difference.png Image: rose.jpg Channel distortion: RMSE red: 2833.91 (0.0432427) green: 2401.45 (0.0366438) blue: 2560.43 (0.0390696) all: 2604.73 (0.0397456)
Or, if you just want the red channel distortion, use this command:
$ magick compare -channel red -metric PSNR rose.jpg reconstruct.jpg difference.png 27.2817 (0.227348)
Or, if you just want the overall image distortion, use this command:
$ magick compare -metric PSNR rose.jpg reconstruct.jpg difference.png 28.0142 (0.233452)
If the reconstructed image is a subimage of the image, the compare program returns the best match offset. In addition, it returns a similarity image such that an exact match location is completely white and if none of the pixels match, black, otherwise some gray level in-between:
$ magick compare -metric NCC -subimage-search logo.png hat.png similarity.pfm 0 (0) @ 417,44 [0] $ magick identify -define identify:locate=maximum -define identify:limit=1 similarity.pfm Channel maximum locations: Gray: 65532.9 (0.999967) 417,44
The value in the brackets is the normalized simularity metric.
Note, similarity search respects the virtual page offset. To ensure expected results, make sure your image page offset is +0+0.
The subimage search feature in ImageMagick can be quite slow due to its iterative nature. However, if your release of ImageMagick includes FFTW (Fastest Fourier Transform in the West) support and your metric is DPC, MSE, NCC, RMSE, PHASE, or PSNR, you can expect an order of magnitude speed increase in processing time. For discussion, refer to Accelerated Subimage-search Compare.
If the -subimage-search option is not enabled, ImageMagick performs a direct pixel-by-pixel comparison between the two images, starting from their page offset, typically the top-left corners. In cases where the images differ in size, the smaller image is aligned with the larger one. The extra areas in the smaller image are treated as virtual pixels. These virtual pixels can be processed according to the chosen virtual pixel method, which influences how unmatched regions are handled in the comparison. Consequently, the comparison metrics may be affected by these additional areas, potentially impacting the overall similarity assessment. To only compare authentic pixels, use -define compare:virtual-pixels=false.
You can find additional examples of using compare in Examples of ImageMagick Usage.
Two images are considered similar if their difference according to the specified metric and fuzz value is 0, with the exception of the normalized cross correlation metric (NCC), where two images are considered similar when their normalized cross correlation is 1. The default metric is NCC.
The compare program returns 2 on error, 0 if the images are similar, or a value between 0 and 1 if they are not similar.
Option Summary
The compare command recognizes these options. Click on an option to get more details about how that option works.