失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > fitLine

fitLine

时间:2024-07-24 02:08:28

相关推荐

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;}

如果觉得《fitLine》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。