Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute distance length between pixel values in OpenCV

Tags:

c++

opencv

I need to create a difference image of two images and want to have the size/length of the difference of every pixel

I'm currently doing this:

cv::Mat diff = cv::abs(img1 - img2);
cv::Mat diffLen(diff.size(), CV_32FC1);
for(int x = 0; x < diff.size().width; ++x)
    for(int y = 0; y < diff.size().height; ++y)
    {
        float d = cv::norm(diff.at<Vec3f>(Point(x,y)));
        diffLen.at<float>(Point(x,y)) = d;
    }

Is there a more convenient way to do this?

like image 287
H4kor Avatar asked Dec 08 '25 16:12

H4kor


1 Answers

I was searching for a similar purpose, and here is what I've got now:

    cv::Mat diff = cv::abs(img1 - img2);
    diff = diff.mul(diff);
    cv::transform(diff, diff, cv::Mat::ones(1, diff.channels(), CV_32F));
    cv::sqrt(diff, diff);

It's not elegant. But seems that opencv doesn't provide "pixel-wise norm" operation.

like image 57
Maa Lee Avatar answered Dec 10 '25 13:12

Maa Lee