fitline拟合函数:

CV_EXPORTS_W void fitLine(
InputArray points, //待输入点集
OutputArray line, //输出点集(一个是方向向量,另一个是拟合直线上的点)
int distType,//拟合方法
double param, //以下参数使用默认值
double reps,
double aeps );

拟合方法总结:

CV_DIST_USER    =-1,  /* User defined distance */
CV_DIST_L1      =1,   /* distance = |x1-x2| + |y1-y2| */
CV_DIST_L2      =2,   /* the simple euclidean distance */
CV_DIST_C       =3,   /* distance = max(|x1-x2|,|y1-y2|) */
CV_DIST_L12     =4,   /* L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1)) */
CV_DIST_FAIR    =5,   /* distance = c^2(|x|/c-log(1+|x|/c)), c = 1.3998 */
CV_DIST_WELSCH  =6,   /* distance = c^2/2(1-exp(-(x/c)^2)), c = 2.9846 */
CV_DIST_HUBER   =7    /* distance = |x|<c ? x^2/2 : c(|x|-c/2), c=1.345 */

在这里不讨论最小二乘法原理和直线方面的知识。
通过寻找图片中灰度为0的像素(直线)进行拟合。

// fitLine.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include<iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Mat image,gray;
const double PI = 3.1415926535897;
vector<Point> getPoints(Mat &image, int value)//we find all the pixels that are equal to zero
{int nl = image.rows; // number of linesint nc = image.cols;vector<Point> points;for (int j = 0; j < nl; j++){uchar* data = image.ptr<uchar>(j);for (int i = 0; i < nc; i++){if(data[i] == value){points.push_back(Point(i, j));}}}return points;
}
void drawLine(Mat&image, double theta, double rho, Scalar color)
{if (theta < PI/4. || theta > 3.*PI/4.)// ~vertical line{Point pt1(rho/cos(theta), 0);Point pt2((rho - image.rows * sin(theta))/cos(theta), image.rows);line( image, pt1, pt2, color, 1);}else{Point pt1(0, rho/sin(theta));Point pt2(image.cols, (rho - image.cols * cos(theta))/sin(theta));line(image, pt1, pt2, Scalar(255,0,0), 1);}
}
int main()
{image=imread("D://vvoo//liness.jpg");image.copyTo(gray);cvtColor(gray,gray,CV_BGR2GRAY);vector<Point> points;//get all the intresting pointsVec4f line;points=getPoints(gray, 0);fitLine(points,line, CV_DIST_L2,0,0.01,0.01);cout<<"line[0]="<<line[0]<<endl<<"line[1]="<<line[1]<<endl<<"line[2]="<<line[2]<<endl<<"line[3]="<<line[3]<<endl;Point2f Point1[2]={Point2f(100,200),Point2f(int(line[2]),int(line[3]))};circle(image, Point1[1], 2, Scalar(0, 0, 255), 5);  double cos_theta = line[0];double sin_theta = line[1];double x0 = line[2], y0 = line[3];double tho = atan2(sin_theta, cos_theta) + PI / 2.0;//angledouble rho = y0 * cos_theta - x0 * sin_theta;cout << "tho = " << tho / PI * 180 << endl;cout << "rho = " << rho << endl;drawLine(image,tho, rho, Scalar(0,0,255));double k = sin_theta / cos_theta;double b = y0 - k * x0;double x = 0;double y = k * x + b;cout <<"k="<< k << endl;//the gradient pf "k" is equal to line[1]/line[0]cout <<"b="<< b << endl;imshow("", image);imwrite("D://vvoo3//iamge.jpg",image);waitKey(0);return 0;
}

对简单直线的识别结果:

参考:1.http://blog.csdn.net/liyuanbhu/article/details/50193947
2.http://blog.csdn.net/leonid112/article/details/6109277

opencv之fitline直线拟合相关推荐

  1. OpenCV 学习(直线拟合)

    Hough 变换可以提取图像中的直线.但是提取的直线的精度不高.而很多场合下,我们需要精确的估计直线的参数,这时就需要进行直线拟合. 直线拟合的方法很多,比如一元线性回归就是一种最简单的直线拟合方法. ...

  2. OpenCV fitline直线拟合函数学习

    下图是OpenCV官方文档中,对直线拟合函数的详细介绍: fitLine()函数用于,对二维或三维空间中的点集进行直线拟合.共有六个参数: param 1:输入的点集,可以是Mat或者vector&l ...

  3. opencv+hough直线检测+fitline直线拟合

    #include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/cor ...

  4. FitLine+直线拟合+C++

    做毕业论文中我用Opencv中的void cvFitLine( const CvArr* points, int dist_type, double param, double reps, doubl ...

  5. 关于opencv fitLine直线拟合得斜率及截距

    函数接口:C++: void fitLine(InputArray points, OutputArray line, int distType, double param, double reps, ...

  6. OpenCV—直线拟合fitLine

    本文的主要参考为官方文档OpenCV249-fitLine和博客-OpenCV 学习(直线拟合) 以及<Learning OpenCV 3>page425-426 OpenCV中提供的直线 ...

  7. [OpenCV]直线拟合

    OpenCV实现了直线的拟合. CV_IMPL void cvFitLine( const CvArr* array, int dist, double param,double reps, doub ...

  8. OpenCV | 直线拟合fitline函数(Python)

    简介 之前做直线拟合时,自己写了一个利用最小二乘做直线拟合的程序,但是由于直线检测的误差比较大,拟合的效果并不好.个人不知道是什么原因,因此想尝试更改一下直线拟合的算法,后来找到了OpenCV中的fi ...

  9. c++ opencv fitLine函数拟合直线

    c++ opencv fitLine函数拟合直线 fitLine 函数 void fitLine( InputArray points, OutputArray line, int distType, ...

最新文章

  1. 工作经验:Java 系统记录调用日志,并且记录错误堆栈
  2. 如何实现一个连接池?一文带你深入浅出,彻底搞懂!
  3. svn教程----eclipse的SVN插件使用
  4. c语言double root,C语言修仙
  5. python中文本文件r_Python如何读写文本文件
  6. 从《网管员必读》系列丛书获奖看读者的真正需求——成功原因分析(一)
  7. 极客大学架构师训练营 编程的本质与未来 第三课 听课总结
  8. cad插件_CAD插件乱刀去教育戳记安装教程
  9. Carson带你学Android:RxJava、Retrofit联合使用汇总(含实例教程)
  10. 系统架构设计师教程学习笔记
  11. 不一样的视角,不一样的VR
  12. C语言解一元二次方程
  13. 【51单片机实验笔记】3. LED点阵的基本控制
  14. 【详解】Linux面试详解
  15. zotero 使用方法
  16. AD单独设置单个或多个焊盘敷铜规则
  17. 网络市场调研的步骤与方法
  18. 【程序设计与算法(三)第7周测验(2019秋季)】005:山寨版istream_iterator
  19. 蓝桥杯 棋盘放麦子<最简单写法>
  20. 机械臂——六轴机械臂逆解

热门文章

  1. Android程序猿基本功
  2. 系统集成项目管理工程师(中级)
  3. 核酸多样性(pi)计算公式
  4. poj 1840 Eqs
  5. 看表情读情绪:AI“察言观色”背后的表情识别数据
  6. Visual Studio Code 代码显示空格等空白符的方法
  7. 京东评论情感分类器(基于bag-of-words模型)
  8. 索尼天气 v1.0.2 魅族定制版,纯净无广告的天气预报应用
  9. Java8 通关攻略
  10. 一张图看懂手机CPU性能——手机CPU性能天梯图