【问题标题】:how to find euclidean distance between pixels within a image in opencv如何在opencv中找到图像内像素之间的欧几里得距离
【发布时间】:2017-04-06 03:17:44
【问题描述】:
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);
        }
    }
}

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

错误信息是:

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 ​​错误 C2664: 'void std::vector<:vector>>,std::allocator<:vector :allocator>>>>::push_back(const std::vector<_ty>> &)' : 无法将参数 1 从 'double' 转换为 'std::vector> &&' d\Visual Studio 2013\Projects\Source.cpp 39 1
  • 您的意思是vector&lt;double&gt; test; 而不是vector&lt;vector&lt;double&gt;&gt; test;
  • 2 IntelliSense:没有重载函数实例“std::vector<_ty _alloc>::push_back [with _Ty=std::vector>, _Alloc =std::allocator<:vector std::allocator>>>]" 匹配参数列表参数类型是:(double)对象类型是:std::vector<:vector std::allocator>>, std::allocator<:vector std::allocator>>>> d:\Visual Studio 2013\Projects\Source.cpp 39 11
  • 我正在尝试使用二维向量来存储 dist 的答案

标签: c++ image opencv matrix


【解决方案1】:

主要有两个问题:

  1. 您将值附加到 test 向量错误。您需要创建一个中间向量并将其 push_backtest(如 @0X0nosugar answer 所示),或者更好地使用正确的尺寸初始化向量并将值放在正确的位置。

    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] = ...       
        }
    }
    

    如上面的 sn-p 所示,按行扫描更好(更快),因为 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));      
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 2016-09-11
    • 2013-01-09
    相关资源
    最近更新 更多