opencv之fitline直线拟合
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直线拟合相关推荐
- OpenCV 学习(直线拟合)
Hough 变换可以提取图像中的直线.但是提取的直线的精度不高.而很多场合下,我们需要精确的估计直线的参数,这时就需要进行直线拟合. 直线拟合的方法很多,比如一元线性回归就是一种最简单的直线拟合方法. ...
- OpenCV fitline直线拟合函数学习
下图是OpenCV官方文档中,对直线拟合函数的详细介绍: fitLine()函数用于,对二维或三维空间中的点集进行直线拟合.共有六个参数: param 1:输入的点集,可以是Mat或者vector&l ...
- opencv+hough直线检测+fitline直线拟合
#include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/cor ...
- FitLine+直线拟合+C++
做毕业论文中我用Opencv中的void cvFitLine( const CvArr* points, int dist_type, double param, double reps, doubl ...
- 关于opencv fitLine直线拟合得斜率及截距
函数接口:C++: void fitLine(InputArray points, OutputArray line, int distType, double param, double reps, ...
- OpenCV—直线拟合fitLine
本文的主要参考为官方文档OpenCV249-fitLine和博客-OpenCV 学习(直线拟合) 以及<Learning OpenCV 3>page425-426 OpenCV中提供的直线 ...
- [OpenCV]直线拟合
OpenCV实现了直线的拟合. CV_IMPL void cvFitLine( const CvArr* array, int dist, double param,double reps, doub ...
- OpenCV | 直线拟合fitline函数(Python)
简介 之前做直线拟合时,自己写了一个利用最小二乘做直线拟合的程序,但是由于直线检测的误差比较大,拟合的效果并不好.个人不知道是什么原因,因此想尝试更改一下直线拟合的算法,后来找到了OpenCV中的fi ...
- c++ opencv fitLine函数拟合直线
c++ opencv fitLine函数拟合直线 fitLine 函数 void fitLine( InputArray points, OutputArray line, int distType, ...
最新文章
- 工作经验:Java 系统记录调用日志,并且记录错误堆栈
- 如何实现一个连接池?一文带你深入浅出,彻底搞懂!
- svn教程----eclipse的SVN插件使用
- c语言double root,C语言修仙
- python中文本文件r_Python如何读写文本文件
- 从《网管员必读》系列丛书获奖看读者的真正需求——成功原因分析(一)
- 极客大学架构师训练营 编程的本质与未来 第三课 听课总结
- cad插件_CAD插件乱刀去教育戳记安装教程
- Carson带你学Android:RxJava、Retrofit联合使用汇总(含实例教程)
- 系统架构设计师教程学习笔记
- 不一样的视角,不一样的VR
- C语言解一元二次方程
- 【51单片机实验笔记】3. LED点阵的基本控制
- 【详解】Linux面试详解
- zotero 使用方法
- AD单独设置单个或多个焊盘敷铜规则
- 网络市场调研的步骤与方法
- 【程序设计与算法(三)第7周测验(2019秋季)】005:山寨版istream_iterator
- 蓝桥杯 棋盘放麦子<最简单写法>
- 机械臂——六轴机械臂逆解