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, ...
最新文章
- ROS学习笔记—-- catkin
- 盘点我跳过的科研天坑,进坑就是半年白干
- python【力扣LeetCode算法题库】1013-将数组分成和相等的三个部分(贪心)
- WINCE补丁包下载地址
- JSP第四篇【EL表达式介绍、获取各类数据、11个内置对象、执行运算、回显数据、自定义函数、fn方法库】...
- 如何将SAP UI5应用配置到SAP云平台Fiori Launchpad里
- [翻译]Adobe Flash Player 11新特性
- 计算一棵树的深度和宽度[Treeview的深度和宽度](多题头的表格问题解决的基础)...
- webworker应用场景_初始WebWorker
- [转载]幂等和高并发在电商系统中的使用
- python3.5 3.6_centos7安装较高版本python3.5/3.6
- 自创RTSP 服务器 用多款客户端软件测试接入可以,唯独VLC接入不了
- 从程序员到技术总监,分享10年开发经验
- XS128 中断向量表
- 如何看懂财务报表:(二)财务报表目录解释
- 一个管理系统实体联系图
- element upload 上传文件报错status of undefined
- 2022金三银四,面试求生指南
- java.lang.ClassNotFoundException(通俗易懂)
- Oracle-SQL语句的逻辑读怎么计算
热门文章
- 重磅:银保监发布消金公司监管评级办法,评级为5级恐被退市(全文重点已标出)
- 设置服务器上的redis数据库共享
- 2022版Maven教程 - 第六章 单一架构案例
- 前端——用div画菜鸟网站首页导航条“小三角”
- mac添加Chrome插件的方法
- 华为ENSP之出口网关设备故障vrrp快速切换
- iOS 仿百度外卖,饿了么-商品列表页
- iOS开发者,金九银十,这样写简历才能让大厂面试官看重你!
- Electron:WARNING Too many active WebGL contexts. Oldest context will be lost.
- 卸载 npm 软件包