OpenCV 直线检测
/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 7 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011. This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user. Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/ #if !defined LINEF
#define LINEF #include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define PI 3.1415926 class LineFinder { private: // original image
cv::Mat img; // vector containing the end points
// of the detected lines
std::vector<cv::Vec4i> lines; // accumulator resolution parameters
double deltaRho;
double deltaTheta; // minimum number of votes that a line
// must receive before being considered
int minVote; // min length for a line
double minLength; // max allowed gap along the line
double maxGap; public: // Default accumulator resolution is 1 pixel by 1 degree
// no gap, no mimimum length
LineFinder() : deltaRho(1), deltaTheta(PI/180), minVote(10), minLength(0.), maxGap(0.) {} // Set the resolution of the accumulator
void setAccResolution(double dRho, double dTheta)
{ deltaRho= dRho;
deltaTheta= dTheta;
} // Set the minimum number of votes
void setMinVote(int minv)
{ minVote= minv;
} // Set line length and gap
void setLineLengthAndGap(double length, double gap)
{ minLength= length;
maxGap= gap;
} // Apply probabilistic Hough Transform
std::vector<cv::Vec4i> findLines(cv::Mat& binary)
{ lines.clear();
cv::HoughLinesP(binary,lines,deltaRho,deltaTheta,minVote, minLength, maxGap); return lines;
} // Draw the detected lines on an image
void drawDetectedLines(cv::Mat &image, cv::Scalar color=cv::Scalar(255,255,255))
{ // Draw the lines
std::vector<cv::Vec4i>::const_iterator it2= lines.begin(); while (it2!=lines.end()) { cv::Point pt1((*it2)[0],(*it2)[1]);
cv::Point pt2((*it2)[2],(*it2)[3]); cv::line( image, pt1, pt2, color); ++it2;
}
} // Eliminates lines that do not have an orientation equals to
// the ones specified in the input matrix of orientations
// At least the given percentage of pixels on the line must
// be within plus or minus delta of the corresponding orientation
std::vector<cv::Vec4i> removeLinesOfInconsistentOrientations(
const cv::Mat &orientations, double percentage, double delta)
{ std::vector<cv::Vec4i>::iterator it= lines.begin(); // check all lines
while (it!=lines.end()) { // end points
int x1= (*it)[0];
int y1= (*it)[1];
int x2= (*it)[2];
int y2= (*it)[3]; // line orientation + 90o to get the parallel line
double ori1= atan2(static_cast<double>(y1-y2),static_cast<double>(x1-x2))+PI/2;
if (ori1>PI) ori1= ori1-2*PI; double ori2= atan2(static_cast<double>(y2-y1),static_cast<double>(x2-x1))+PI/2;
if (ori2>PI) ori2= ori2-2*PI; // for all points on the line
cv::LineIterator lit(orientations,cv::Point(x1,y1),cv::Point(x2,y2));
int i,count=0;
for(i = 0, count=0; i < lit.count; i++, ++lit) { float ori= *(reinterpret_cast<float *>(*lit)); // is line orientation similar to gradient orientation ?
if (std::min(fabs(ori-ori1),fabs(ori-ori2))<delta)
count++; } double consistency= count/static_cast<double>(i); // set to zero lines of inconsistent orientation
if (consistency < percentage) { (*it)[0]=(*it)[1]=(*it)[2]=(*it)[3]=0; } ++it;
} return lines;
}
}; #endif
// HoughLines.cpp : 定义控制台应用程序的入口点。
// // findContours.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" /*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 7 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011. This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user. Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/ #include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp> #include "HoughLines.h" #pragma comment(lib,"opencv_core2410d.lib")
#pragma comment(lib,"opencv_highgui2410d.lib")
#pragma comment(lib,"opencv_imgproc2410d.lib") #define PI 3.1415926 int main()
{
// Read input image
cv::Mat image= cv::imread("road.jpg",0);
if (!image.data)
return 0; // Display the image
cv::namedWindow("Original Image");
cv::imshow("Original Image",image); // Apply Canny algorithm
cv::Mat contours;
cv::Canny(image,contours,125,350);
cv::Mat contoursInv;
cv::threshold(contours,contoursInv,128,255,cv::THRESH_BINARY_INV); // Display the image of contours
cv::namedWindow("Canny Contours");
cv::imshow("Canny Contours",contoursInv); // Hough tranform for line detection
std::vector<cv::Vec2f> lines;
cv::HoughLines(contours,lines,1,PI/180,60); // Draw the lines
cv::Mat result(contours.rows,contours.cols,CV_8U,cv::Scalar(255));
image.copyTo(result); std::cout << "Lines detected: " << lines.size() << std::endl; std::vector<cv::Vec2f>::const_iterator it= lines.begin();
while (it!=lines.end())
{ float rho= (*it)[0]; // first element is distance rho
float theta= (*it)[1]; // second element is angle theta if (theta < PI/4. || theta > 3.*PI/4.) { // ~vertical line // point of intersection of the line with first row
cv::Point pt1(rho/cos(theta),0);
// point of intersection of the line with last row
cv::Point pt2((rho-result.rows*sin(theta))/cos(theta),result.rows);
// draw a white line
cv::line( result, pt1, pt2, cv::Scalar(255), 1); } else { // ~horizontal line // point of intersection of the line with first column
cv::Point pt1(0,rho/sin(theta));
// point of intersection of the line with last column
cv::Point pt2(result.cols,(rho-result.cols*cos(theta))/sin(theta));
// draw a white line
cv::line( result, pt1, pt2, cv::Scalar(255), 1);
} std::cout << "line: (" << rho << "," << theta << ")\n"; ++it;
} // Display the detected line image
cv::namedWindow("Detected Lines with Hough");
cv::imshow("Detected Lines with Hough",result); // Create LineFinder instance
LineFinder ld; // Set probabilistic Hough parameters
ld.setLineLengthAndGap(100,20);
ld.setMinVote(80); // Detect lines
std::vector<cv::Vec4i> li= ld.findLines(contours);
ld.drawDetectedLines(image);
cv::namedWindow("Detected Lines with HoughP");
cv::imshow("Detected Lines with HoughP",image); cv::waitKey();
return 0;
}
实现效果:
OpenCV 直线检测的更多相关文章
- opencv直线检测在c#、Android和ios下的实现方法
opencv直线检测在c#.Android和ios下的实现方法 本文为作者原创,未经允许,不得转载 :原文由作者发表在博客园:http://www.cnblogs.com/panxiaochun/p/ ...
- Python+OpenCV图像处理(十四)—— 直线检测
简介: 1.霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进算法.主要用来从图像中分离出具有某种相同特征的几何形状(如,直线 ...
- opencv学习笔记霍夫变换——直线检测
参考大佬博文:blog.csdn.net/jia20003/article/details/7724530 lps-683.iteye.com/blog/2254368 openCV里有两个函数(比较 ...
- 【python+opencv】直线检测+圆检测
Python+OpenCV图像处理—— 直线检测 直线检测理论知识: 1.霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进 ...
- opencv:霍夫直线检测
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- 【CImg】霍夫变换——直线检测
霍夫变换——直线检测 考古debug,其实很久之前就解决的bug......一直忘记过来改文章....欸 =============================原文================ ...
- OpenCV轮廓检测,计算物体旋转角度
效果还是有点问题的,希望大家共同探讨一下 // FindRotation-angle.cpp : 定义控制台应用程序的入口点. // // findContours.cpp : 定义控制台应用程序的入 ...
- python实现直线检测
目录: (一)原理 (二)代码(标准霍夫线变换,统计概率霍夫线变换) (一)原理 1.霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也 ...
- Matlab 霍夫变换 ( Hough Transform) 直线检测
PS:好久没更新,因为期末到了,拼命复习中.复习久了觉得枯燥,玩玩儿霍夫变换直线检测 霍夫变换的基本原理不难,即便是初中生也很容易理解(至少在直线检测上是这样子的). 霍夫变换直线检测的基本原理:(不 ...
随机推荐
- ROS机器人程序设计(原书第2版)补充资料 (捌) 第八章 导航功能包集入门 navigation
ROS机器人程序设计(原书第2版)补充资料 (捌) 第八章 导航功能包集入门 navigation 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中 ...
- chromium出现输入密码解锁登录密钥环
chromium出现输入密码解锁登录密钥环 在ubuntu 16.04上安装了Chromium出现对话框,如下所示: 因为密码框截图困难,这个是网上图片. 点取消就可以使用,但是每次都这样很烦,百度后 ...
- Ejb远程调用-jboss服务器调用服务器-Bean调用Bean
英文参考地址 https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+o ...
- shell 参数列表的获取&shell使用的一些总结
最近在修改公司的一些cron,自己也是第一次接触和学习shell.对于一些零散但是常用的知识点,做一点点的总结. 拿出一个方法说说吧,方法如下:(信息量挺大的,请耐心看下面的说明) trans_cou ...
- Android项目开发填坑记-9patchPng报错
如果阅读体验不佳,请使用–> Github版 背景 之前写了一篇文章Android必知必会–NinePatch图片制作详细介绍了Android 9Patch图片的制作和一些Demo展示,这次说明 ...
- Android使用局和数据实现天气项目-android学习之旅(十二)
1.首先注册聚合数据账号,下载相应的sdk 2.导入jar包和 so文件 配置Application,初始化sdk <application //自己新建的application类 androi ...
- 11 PopupMenu菜单和代码例子
PopupMenu 弹出式菜单 API 11以上可用 1. 获取弹出菜单的对象 2. 在res里的menu添加菜单项 3. 将布局里的菜单项 给弹出菜单 4. 进行监听弹出菜单 5. 展示出弹出菜单 ...
- (一一五)利用NSKeyedArchiver实现任意对象转为二进制
[应用背景] 在数据库中存储数据时,如果对象过于复杂,又不必要创建复杂的表,可以直接把整个对象转化为二进制存入数据库字段,然后取出后再还原即可. [实现方法] 在PHP中,使用序列化和反序列化可以实现 ...
- 后端分布式系列:分布式存储-HDFS 架构解析
本文以 Hadoop 提供的分布式文件系统(HDFS)为例来进一步展开解析分布式存储服务架构设计的要点. 架构目标 任何一种软件框架或服务都是为了解决特定问题而产生的.还记得我们在 <分布式存储 ...
- SpriteBuilder实现2D精灵光影明暗反射效果(一)
其实不用3D建模,用2D的图像就可以模拟3D场景中光照反射的效果. 这里我们不得不提到一个normalMap(法线图)的概念,请各位童鞋自己度娘吧,简单来说它可以使得2D表面生成一定细节程度的光照方向 ...