#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(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;
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::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));
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;
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::waitKey(0);
return 0;
}