Ask Your Question
1

distance between images, pixel by pixel

asked 2015-08-16 10:52:44 -0500

twentylemon gravatar image

updated 2015-11-09 15:33:15 -0500

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 flag offensive close merge delete

Comments

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

berak gravatar imageberak ( 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);
berak gravatar imageberak ( 2015-08-16 11:14:05 -0500 )edit

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

twentylemon gravatar imagetwentylemon ( 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)

berak gravatar imageberak ( 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.

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

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-08-17 09:33:44 -0500

Guanta gravatar image
//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
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-08-16 10:52:44 -0500

Seen: 2,304 times

Last updated: Aug 17 '15