naveenKumar
naveenKumar

Reputation: 397

How to use the ImageMagick API to compare images in C++?

How to do the equivalent of the command line

compare bag_frame1.gif bag_frame2.gif  compare.gif

in C++ using the Magick++ APIs? I want to compare or find similarity of two images of same dimensions in C++ code rather using the command line.

Any sample code snippet would be appreciated.

Upvotes: 1

Views: 3128

Answers (2)

emcconville
emcconville

Reputation: 24419

I belive Magick::Image.compare is the method your looking for. There are three methods signatures available for your application.

  • Bool to evaluate if there's a difference.
  • Double distortion amount based on metric.
  • Image resulting difference as a new highlight image.

For example...

#include <Magick++.h>

int main(int argc, const char * argv[]) {

    Magick::InitializeMagick(argv[0]);

    Magick::Geometry canvas(150, 150);
    Magick::Color white("white");

    Magick::Image first(canvas, white);
    first.read("PATTERN:FISHSCALES");
    Magick::Image second(canvas, white);
    second.read("PATTERN:GRAY70");

    // Bool to evaluate if there's a difference.
    bool isIdentical = first.compare(second);

    // Double distortion amount based on metric.
    double metricDistortion = first.compare(second, Magick::AbsoluteErrorMetric);

    // Image resulting difference as a new highlight image.
    double distortion = 0.0;
    Magick::Image result = first.compare(second, Magick::AbsoluteErrorMetric, &distortion);

    return 0;
}

The third example would be the method needed to satisfy the command line

compare bag_frame1.gif bag_frame2.gif  compare.gif

Upvotes: 2

ShadowMitia
ShadowMitia

Reputation: 2533

I think you have your answer here: http://www.imagemagick.org/discourse-server/viewtopic.php?t=25191

Images are store in the Image class which has a compare member function. The first link has an example on how to use it, and the Image documentation has a nice example on how to use Image.

Upvotes: 0

Related Questions