fitLine

#include<iostream>
#include<opencv.hpp>

using namespace std;
using namespace cv;
int main()
{
	//创建一个用于绘制图像的空白图
	cv::Mat image = cv::Mat::zeros(480, 640, CV_8UC3);

	//输入拟合点
	std::vector<cv::Point> points;
	//points.push_back(cv::Point(110, 58));
	//points.push_back(cv::Point(125, 98));
	//points.push_back(cv::Point(148, 160));
	//points.push_back(cv::Point(173, 220));
	//points.push_back(cv::Point(188, 260));
	//points.push_back(cv::Point(204, 300));
	//points.push_back(cv::Point(227, 360));
	//points.push_back(cv::Point(242, 400));
	points.push_back(cv::Point(242, 58));
	points.push_back(cv::Point(227, 98));
	points.push_back(cv::Point(204, 160));
	points.push_back(cv::Point(188, 220));
	points.push_back(cv::Point(173, 260));
	points.push_back(cv::Point(148, 300));
	points.push_back(cv::Point(125, 360));
	points.push_back(cv::Point(110, 400));
	//将拟合点绘制到空白图上
	for (int i = 0; i < points.size(); i++)
	{
		cv::circle(image, points[i], 5, cv::Scalar(0, 0, 255), 2, 8, 0);
	}

	cv::Vec4f line_para;
	cv::fitLine(points, line_para, cv::DIST_L2, 0, 1e-2, 1e-2);

	std::cout << "line_para = " << line_para << std::endl;

	//获取点斜式的点和斜率
	cv::Point point0;
	point0.x = line_para[2];
	point0.y = line_para[3];

	double k = line_para[1] / line_para[0];
	//斜率下的角度
	double angle = atan2(line_para[1], line_para[0]) * 180.0 / CV_PI;//最后为-68.826026756655949

		//计算直线的端点(y = k(x - x0) + y0)
	cv::Point point1, point2;
	point1.x = 0;
	point1.y = k * (0 - point0.x) + point0.y;
	point2.x = 640;
	point2.y = k * (640 - point0.x) + point0.y;

	cv::line(image, point1, point2, cv::Scalar(0, 255, 0), 2, 8, 0);

	//cv::imshow("image", image);
	cv::waitKey(0);
	return 0;
}

请添加图片描述

#include<iostream>
#include<opencv.hpp>

using namespace std;
using namespace cv;
int main()
{
	//创建一个用于绘制图像的空白图
	cv::Mat image = cv::Mat::zeros(480, 640, CV_8UC3);

	//输入拟合点
	std::vector<cv::Point> points;
	points.push_back(cv::Point(110, 58));
	points.push_back(cv::Point(125, 98));
	points.push_back(cv::Point(148, 160));
	points.push_back(cv::Point(173, 220));
	points.push_back(cv::Point(188, 260));
	points.push_back(cv::Point(204, 300));
	points.push_back(cv::Point(227, 360));
	points.push_back(cv::Point(242, 400));
	//points.push_back(cv::Point(242, 58));
	//points.push_back(cv::Point(227, 98));
	//points.push_back(cv::Point(204, 160));
	//points.push_back(cv::Point(188, 220));
	//points.push_back(cv::Point(173, 260));
	//points.push_back(cv::Point(148, 300));
	//points.push_back(cv::Point(125, 360));
	//points.push_back(cv::Point(110, 400));
	//将拟合点绘制到空白图上
	for (int i = 0; i < points.size(); i++)
	{
		cv::circle(image, points[i], 5, cv::Scalar(0, 0, 255), 2, 8, 0);
	}

	cv::Vec4f line_para;
	cv::fitLine(points, line_para, cv::DIST_L2, 0, 1e-2, 1e-2);

	std::cout << "line_para = " << line_para << std::endl;

	//获取点斜式的点和斜率
	cv::Point point0;
	point0.x = line_para[2];
	point0.y = line_para[3];

	double k = line_para[1] / line_para[0];
	//斜率下的角度
	double angle = atan2(line_para[1], line_para[0]) * 180.0 / CV_PI;//最后为68.774868639772549

		//计算直线的端点(y = k(x - x0) + y0)
	cv::Point point1, point2;
	point1.x = 0;
	point1.y = k * (0 - point0.x) + point0.y;
	point2.x = 640;
	point2.y = k * (640 - point0.x) + point0.y;

	cv::line(image, point1, point2, cv::Scalar(0, 255, 0), 2, 8, 0);

	//cv::imshow("image", image);
	cv::waitKey(0);
	return 0;
}

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值