gpt4 book ai didi

c++ - 如何在opencv中找到图像中像素之间的欧氏距离

转载 作者:行者123 更新时间:2023-11-28 05:24:56 31 4
gpt4 key购买 nike

int main(){
Mat cmp, Ref, Diff;
cmp = imread("image1.tif", CV_LOAD_IMAGE_UNCHANGED);
Ref = imread("image2.tif", CV_LOAD_IMAGE_UNCHANGED);
ShiftChk(cmp, Ref);
absdiff(cmp, Ref, Diff);
imshow("difference image", Diff);
waitKey(0);
double min, max;
minMaxLoc(Diff, &min, &max);
Point min_loc, max_loc;
minMaxLoc(Diff, &min, &max, &min_loc, &max_loc);
Size sz = Diff.size();
cout << "max val : " << max << endl;//5
cout << "max val: " << max_loc << endl; //[26,38]


vector<vector<double>>test;
for (int i = 0; i < Diff.cols; i++) {
for (int j = 0; j < Diff.rows; j++) {

Point difference = Diff.at<uchar>(26, 38) - Diff.at<uchar>(j, i);
double dist = sqrt(difference.x*difference.x + difference.y*difference.y);
test.push_back(dist);
}
}
}

我试图找到图像中单个点与所有其他像素之间的欧氏距离。距离值将存储在 vector 测试中,但它显示出一些错误。而且我也不知道我使用的逻辑是否正确以给出正确的答案(欧氏距离)。谁能帮我吗。提前致谢

错误信息是:

error C2664: 
'void std::vector<std::vector<double,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>::push_back(const std::vector<_Ty,std::allocator<_Ty>> &)' :
cannot convert argument 1 from 'double' to 'std::vector<double,std::allocator<_Ty>> &&'

最佳答案

主要有两个问题:

  1. 您错误地将值附加到 test vector 。您需要创建一个中间 vector 并将其push_back测试(如 @0X0nosugar answer 所示),或者更好地使用正确的维度初始化您的 vector 并将值放在正确的地方。

    vector<vector<double>> test(Diff.rows, vector<double>(Diff.cols));
    for (int i = 0; i < Diff.rows; i++) {
    for (int j = 0; j < Diff.cols; j++) {
    test[i][j] = ...
    }
    }

    如上面的代码片段所示,按行扫描更好(也更快),因为 OpenCV 按行存储图像。

  2. 您不是在计算两点之间的距离。实际上,您正在获取两个给定点的值的差异,并从中创建一个 Point 对象(这没有意义)。您也可以避免显式计算欧氏距离。您可以使用 cv::norm:

     test[i][j] = norm(Point(38, 26) - Point(j, i)); // Pay attention to i,j order!

放在一起:

Point ref(38, 26);
vector<vector<double>> test(Diff.rows, vector<double>(Diff.cols));
for (int i = 0; i < Diff.rows; i++) {
for (int j = 0; j < Diff.cols; j++) {
test[i][j] = norm(ref - Point(j,i));
}
}

关于c++ - 如何在opencv中找到图像中像素之间的欧氏距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40742011/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com