# distance between images, pixel by pixel

I'm need to calculate the euclidean distance between two cv::Mat images in a pixel by pixel manner. Is there a function that does this? Specifically, I need the result to be of the form

diff[i] = sqrt( (r1-r2)^2 + (g1-g2)^2 + (b1-b2)^2 )


where diff is a Mat. If there is no function, is there a simple way to calculate this?

Thanks!

edit retag close merge delete

just saying: ^ is the 'binary or' operator in c++, not pow !

( 2015-08-16 11:11:24 -0500 )edit
1

you can use cv::norm for the L2 distance:

Vec3b a(1,2,3);
Vec3b b(4,5,6);

double dist = cv::norm(a,b);

( 2015-08-16 11:14:05 -0500 )edit

How can I store the result in a Mat? Sorry - I justed started using opencv yesterday.

( 2015-08-16 11:18:51 -0500 )edit

don't ever feel sorry for asking ;)

why do you want to do that ? (and there's no easy way)

( 2015-08-16 11:21:07 -0500 )edit

I'm implemented painterly rendering for a project. One of the steps is the calculate the distance between the source and a blurred image in a pixel by pixel manner, then sum square regions of the distance map to make decisions. I found the sum of the regions in a Mat using operator() and sum -- I'm hoping that the distance map can be found as a Mat then.

( 2015-08-16 11:30:03 -0500 )edit

Sort by ยป oldest newest most voted
//convert your uchar images to float matrices:
cv::Mat3f i1 = img1;
cv::Mat3f i2 = img2;

cv::Mat3f t = i1-i2;
cv::Mat3f res;
cv::sqrt(t.mul(t), res);
// now res contains the result

more