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>> &&'
有两个主要问题:
您将值附加到test
向量错误。您需要创建一个中间矢量并将push_back
其创建test
(如@ 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] = ...
}
}
如上面的代码片段所示,按行扫描更好(更快),因为OpenCV按行存储图像。
您不是在计算两点之间的距离。实际上,您是在两个给定的点上取值之差,并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));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句