首先通过鼠标操作在图片上随机点两个点,创建一条直线。然后点第三个点,该点到直线的距离就出来了。
由于鼠标点击有误差,所以测的距离有误差,还请大佬指点!
#include "opencv2/opencv.hpp"
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
//定义一个计算两点距离的函数
double getDistance(CvPoint pointO, CvPoint pointA)
{
double distance;
distance = powf((pointO.x - pointA.x), 2) + powf((pointO.y - pointA.y), 2);
distance = sqrtf(distance);
return distance;
}
//定义一个将长整形转换成字符串型的函数
string convertToString(long d)
{
ostringstream os;
if (os << d)
return os.str();
return "invalid conversion";
}
//定义全局变量
static Mat img_src;
static Point point1 = Point(0, 0);//第一个点(x1,y1)
static Point point2 = Point(0, 0);//第二个点(x2,y2)
static Point point3 = Point(0, 0);//第三个点(x3,y3)
static Point point4;//第四个点(x4,y4)
static Point point5;
static Point mouse_dot;
static string window_name = "image&#