如何使用 magnitude 和 absdiff OpenCV 函数来计算距离?
How to use magnitude and absdiff OpenCV functions to compute distances?
如何使用 magnitude
和 absdiff
?我看了the documentation里面的解释,但是每次都报错,因为我不明白到底应该怎么输入数组和输出。应该是 vector
、Mat
还是 Scalar
?我尝试了一些但我失败了,与 cartToPolar
一样。谁能给我一小段,因为我没有在文档中找到任何示例?
更准确地说,我有包含 30 条线的终点和起点的矢量 vector<Vec4f> lines;
,所以我想使用幅度来计算每条线的长度。我学会了如何通过 for 循环使用 norm
但我想使用 magnitude
所以我这样做了:
double x;
length=magnitude(lines[i][2]-lines[i][0],lines[i][3]-lines[i][1],x)
但它不起作用。我试图将 x 定义为 1 个数组向量,但我不能。
您已经了解了如何使用 norm
计算距离:
Point2f a = ...
Point2f b = ..
double length = norm(a - b); // NORM_L2, NORM_L1
您也可以一次处理所有点。您首先需要将坐标从矢量转换为矩阵形式,然后就是简单的数学运算:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
vector<Vec4f> lines{ { 1, 2, 4, 6 }, { 5, 7, 1, 3 }, { 11, 12, 12, 11 } };
Mat1f coordinates = Mat4f(lines).reshape(1);
Mat1f diff_x = coordinates.col(0) - coordinates.col(2);
Mat1f diff_y = coordinates.col(1) - coordinates.col(3);
cout << "coordinates: \n" << coordinates << "\n\n";
cout << "diff_x: \n" << diff_x << "\n\n";
cout << "diff_y: \n" << diff_y << "\n\n";
cout << endl;
// sqrt((x2 - x1)^2 + (y2 - y1)^2)
Mat1f euclidean_distance;
magnitude(diff_x, diff_y, euclidean_distance);
cout << "euclidean_distance: \n" << euclidean_distance << "\n\n";
// abs(x2 - x1) + abs(y2 - y1)
Mat1f manhattan_distance = abs(diff_x) + abs(diff_y);
cout << "manhattan_distance: \n" << manhattan_distance << "\n\n";
// Another way to compute L1 distance, with absdiff
// abs(x2 - x1) + abs(y2 - y1)
Mat1f points1 = coordinates(Range::all(), Range(0, 2));
Mat1f points2 = coordinates(Range::all(), Range(2, 4));
Mat1f other_manhattan_distance;
absdiff(points1, points2, other_manhattan_distance);
other_manhattan_distance = other_manhattan_distance.col(0) + other_manhattan_distance.col(1);
cout << "other_manhattan_distance: \n" << other_manhattan_distance << "\n\n";
return 0;
}
如何使用 magnitude
和 absdiff
?我看了the documentation里面的解释,但是每次都报错,因为我不明白到底应该怎么输入数组和输出。应该是 vector
、Mat
还是 Scalar
?我尝试了一些但我失败了,与 cartToPolar
一样。谁能给我一小段,因为我没有在文档中找到任何示例?
更准确地说,我有包含 30 条线的终点和起点的矢量 vector<Vec4f> lines;
,所以我想使用幅度来计算每条线的长度。我学会了如何通过 for 循环使用 norm
但我想使用 magnitude
所以我这样做了:
double x;
length=magnitude(lines[i][2]-lines[i][0],lines[i][3]-lines[i][1],x)
但它不起作用。我试图将 x 定义为 1 个数组向量,但我不能。
您已经了解了如何使用 norm
计算距离:
Point2f a = ...
Point2f b = ..
double length = norm(a - b); // NORM_L2, NORM_L1
您也可以一次处理所有点。您首先需要将坐标从矢量转换为矩阵形式,然后就是简单的数学运算:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
vector<Vec4f> lines{ { 1, 2, 4, 6 }, { 5, 7, 1, 3 }, { 11, 12, 12, 11 } };
Mat1f coordinates = Mat4f(lines).reshape(1);
Mat1f diff_x = coordinates.col(0) - coordinates.col(2);
Mat1f diff_y = coordinates.col(1) - coordinates.col(3);
cout << "coordinates: \n" << coordinates << "\n\n";
cout << "diff_x: \n" << diff_x << "\n\n";
cout << "diff_y: \n" << diff_y << "\n\n";
cout << endl;
// sqrt((x2 - x1)^2 + (y2 - y1)^2)
Mat1f euclidean_distance;
magnitude(diff_x, diff_y, euclidean_distance);
cout << "euclidean_distance: \n" << euclidean_distance << "\n\n";
// abs(x2 - x1) + abs(y2 - y1)
Mat1f manhattan_distance = abs(diff_x) + abs(diff_y);
cout << "manhattan_distance: \n" << manhattan_distance << "\n\n";
// Another way to compute L1 distance, with absdiff
// abs(x2 - x1) + abs(y2 - y1)
Mat1f points1 = coordinates(Range::all(), Range(0, 2));
Mat1f points2 = coordinates(Range::all(), Range(2, 4));
Mat1f other_manhattan_distance;
absdiff(points1, points2, other_manhattan_distance);
other_manhattan_distance = other_manhattan_distance.col(0) + other_manhattan_distance.col(1);
cout << "other_manhattan_distance: \n" << other_manhattan_distance << "\n\n";
return 0;
}