当前位置:   article > 正文

opencv 曲线拟合_opencv拟合曲线

opencv拟合曲线

本文首发地址:opencv 曲线拟合 - 无左无右 - 博客园最小二乘法多项式曲线拟合原理与实现 https://blog.csdn.net/jairuschan/article/details/7517773/ 算法+OpenCV】基于opencv的直线和曲线https://www.cnblogs.com/yanghailin/p/15724647.html

最小二乘法多项式曲线拟合原理与实现 最小二乘法多项式曲线拟合原理与实现_JairusChan的技术博客-CSDN博客_曲线拟合的最小二乘法
算法+OpenCV】基于opencv的直线和曲线拟合与绘制(最小二乘法) 算法+OpenCV】基于opencv的直线和曲线拟合与绘制(最小二乘法) - feng..liu - 博客园

基于opencv c++代码如下:

  1. #include <iostream>
  2. #include <opencv.hpp>
  3. #include<opencv2/opencv.hpp>
  4. using namespace std;
  5. using namespace cv;
  6. void FitPolynomialCurve(const std::vector<cv::Point>& points, int n, cv::Mat& A){
  7. //最小二乘法多项式曲线拟合原理与实现 https://blog.csdn.net/jairuschan/article/details/7517773/
  8. //https://www.cnblogs.com/fengliu-/p/8031406.html
  9. int N = points.size();
  10. cv::Mat X = cv::Mat::zeros(n + 1, n + 1, CV_64FC1);
  11. for (int i = 0; i < n + 1; i++){
  12. for (int j = 0; j < n + 1; j++){
  13. for (int k = 0; k < N; k++){
  14. X.at<double>(i, j) = X.at<double>(i, j) +
  15. std::pow(points[k].x, i + j);
  16. }
  17. }
  18. }
  19. cv::Mat Y = cv::Mat::zeros(n + 1, 1, CV_64FC1);
  20. for (int i = 0; i < n + 1; i++){
  21. for (int k = 0; k < N; k++){
  22. Y.at<double>(i, 0) = Y.at<double>(i, 0) +
  23. std::pow(points[k].x, i) * points[k].y;
  24. }
  25. }
  26. A = cv::Mat::zeros(n + 1, 1, CV_64FC1);
  27. cv::solve(X, Y, A, cv::DECOMP_LU);
  28. }
  29. int main(int argc, char **argv)
  30. {
  31. string path = "/data_1/everyday/1224/2.jpeg";
  32. Mat img = imread(path);
  33. Mat img_gray,img_bi;
  34. cvtColor(img,img_gray,CV_BGR2GRAY);
  35. threshold(img_gray,img_bi,80,255,THRESH_BINARY_INV);
  36. vector<vector<Point> > contours;
  37. vector<Vec4i> hierarchy;
  38. findContours( img_bi, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE , Point(0, 0) );
  39. std::cout<<contours[0].size()<<std::endl;
  40. cv::Mat img_draw = cv::Mat(img.rows,img.cols,CV_8UC3,Scalar(0,0,255));
  41. drawContours(img_draw,contours,-1,Scalar(255,255,255));
  42. int n = 3;
  43. cv::Mat A;
  44. FitPolynomialCurve(contours[0], n, A);
  45. std::vector<cv::Point> points_fitted;
  46. for (int x = 0; x < 800; x++)
  47. {
  48. double y = A.at<double>(0, 0) + A.at<double>(1, 0) * x +
  49. A.at<double>(2, 0)*std::pow(x, 2) + A.at<double>(3, 0)*std::pow(x, 3);
  50. points_fitted.push_back(cv::Point(x, y));
  51. }
  52. cv::polylines(img_draw, points_fitted, false, cv::Scalar(0, 0, 0), 1, 8, 0);
  53. imshow("img_src",img);
  54. imshow("img_draw",img_draw);
  55. imshow("img_bi",img_bi);
  56. waitKey(0);
  57. return 0;
  58. }

效果图如下:

但是我后面又整了个S形状的图像,找不到能够很好拟合的函数阶数。

  1. #include <iostream>
  2. #include <opencv.hpp>
  3. #include<opencv2/opencv.hpp>
  4. using namespace std;
  5. using namespace cv;
  6. void FitPolynomialCurve(const std::vector<cv::Point>& points, int n, cv::Mat& A){
  7. //最小二乘法多项式曲线拟合原理与实现 https://blog.csdn.net/jairuschan/article/details/7517773/
  8. //https://www.cnblogs.com/fengliu-/p/8031406.html
  9. int N = points.size();
  10. cv::Mat X = cv::Mat::zeros(n + 1, n + 1, CV_64FC1);
  11. for (int i = 0; i < n + 1; i++){
  12. for (int j = 0; j < n + 1; j++){
  13. for (int k = 0; k < N; k++){
  14. X.at<double>(i, j) = X.at<double>(i, j) +
  15. std::pow(points[k].x, i + j);
  16. }
  17. }
  18. }
  19. cv::Mat Y = cv::Mat::zeros(n + 1, 1, CV_64FC1);
  20. for (int i = 0; i < n + 1; i++){
  21. for (int k = 0; k < N; k++){
  22. Y.at<double>(i, 0) = Y.at<double>(i, 0) +
  23. std::pow(points[k].x, i) * points[k].y;
  24. }
  25. }
  26. A = cv::Mat::zeros(n + 1, 1, CV_64FC1);
  27. cv::solve(X, Y, A, cv::DECOMP_LU);
  28. }
  29. int main(int argc, char **argv)
  30. {
  31. string path = "/data_1/everyday/1224/3.jpeg";
  32. Mat img = imread(path);
  33. Mat img_gray,img_bi;
  34. cvtColor(img,img_gray,CV_BGR2GRAY);
  35. threshold(img_gray,img_bi,80,255,THRESH_BINARY_INV);
  36. vector<vector<Point> > contours;
  37. vector<Vec4i> hierarchy;
  38. findContours( img_bi, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE , Point(0, 0) );
  39. std::cout<<contours[0].size()<<std::endl;
  40. cv::Mat img_draw = cv::Mat(img.rows,img.cols,CV_8UC3,Scalar(0,0,255));
  41. drawContours(img_draw,contours,-1,Scalar(255,255,255));
  42. int n = 9;
  43. cv::Mat A;
  44. FitPolynomialCurve(contours[0], n, A);
  45. std::vector<cv::Point> points_fitted;
  46. for (int x = 0; x < 800; x++)
  47. {
  48. double y = A.at<double>(0, 0) + A.at<double>(1, 0) * x +
  49. A.at<double>(2, 0)*std::pow(x, 2) + A.at<double>(3, 0)*std::pow(x, 3) + A.at<double>(4, 0)*std::pow(x, 4) + A.at<double>(5, 0)*std::pow(x, 5)
  50. + A.at<double>(6, 0)*std::pow(x, 6) + A.at<double>(7, 0)*std::pow(x, 7) + A.at<double>(8, 0)*std::pow(x, 8) + A.at<double>(9, 0)*std::pow(x, 9);
  51. //+ A.at<double>(10, 0)*std::pow(x, 10) + A.at<double>(11, 0)*std::pow(x, 11) + A.at<double>(12, 0)*std::pow(x, 12);
  52. points_fitted.push_back(cv::Point(x, y));
  53. }
  54. cv::polylines(img_draw, points_fitted, false, cv::Scalar(0, 0, 0), 1, 8, 0);
  55. imshow("img_src",img);
  56. imshow("img_draw",img_draw);
  57. imshow("img_bi",img_bi);
  58. waitKey(0);
  59. return 0;
  60. }

 突然想明白,这个S形状曲线一个x对应好几个y,不行。需要一个x唯一对应一个y的曲线才能拟合。然后又顺手画了一个,果真可以拟合。

 

当然代码每次根据不同的阶数写好多A.at(6, 0)*std::pow(x, 6),可以用如下函数自动根据x得到y:

  1. double CurveY(double x, cv::Mat& A){
  2. double y = 0.0;
  3. double *a = A.ptr<double>();
  4. for (int i = 0; i < A.rows; i++){
  5. y += a[i] * pow(x, i);
  6. }
  7. return y;
  8. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/264023
推荐阅读
相关标签
  

闽ICP备14008679号