Schnodderbalken
Schnodderbalken

Reputation: 3467

Compare images in node.js (using e.g. opencv)

What I want to do is to get an image diff using node.js. Ultimately I want to have a method expecting e.g. two filepaths or image data outputting the subtraction of both. Somehow like the compare function in imagemagick.

Something like:

var comparison_result = compareModule.compare('./image1.png', './image2.png');

Also, I would like to get the position of the spots in the resulting image that mark the differences.

Like this:

comparison_result.forEach(function(difference) {
    console.log("A difference occurred at " + difference.x + "|" + difference.y);
}); 

I installed node-opencv, however I can not find a documentation that maps basic opencv c++ functions to node.js. The function I would like to use is cvSub.

I would like to avoid js-imagediff as it works with canvas, has a dependency to "cairo" and I am not sure whether I can access the spots because in the documentation it rather seems like it just returns the difference as an image.

Upvotes: 2

Views: 4130

Answers (1)

Kornel
Kornel

Reputation: 5354

I have never tried to calculate per-element difference by cv::addWeighted() but it may work in practice:

var diff = new cv.Matrix(first.width(), first.height());
diff.addWeighted(first, 1.0, second, -1.0);

In native code (C++), this function can be replaced with the expression below:

diff = first*1.0 + second*(-1.0) + 0.0;

p.s.: node-opencv's authors published a sample code for measuring similarity:
node-opencv / examples / dissimilarity.js

Upvotes: 1

Related Questions