首页 > 编程 > Python > 正文

python+opencv实现霍夫变换检测直线

2020-01-04 13:42:28
字体:
来源:转载
供稿:网友

本文实例为大家分享了python+opencv实现霍夫变换检测直线的具体代码,供大家参考,具体内容如下

功能:

创建一个滑动条来控制检测直线的长度阈值,即大于该阈值的检测出来,小于该阈值的忽略 
注意:这里用的函数是HoughLinesP而不是HoughLines,因为HoughLinesP直接给出了直线的断点,在画出线段的时候可以偷懒

代码:

# -*- coding: utf-8 -*- import cv2#两个回调函数def HoughLinesP(minLineLength): global minLINELENGTH  minLINELENGTH = minLineLength + 1 print "minLINELENGTH:",minLineLength + 1 tempIamge = scr.copy() lines = cv2.HoughLinesP( edges, 1, cv2.cv.CV_PI/180, minLINELENGTH, 0 ) for x1,y1,x2,y2 in lines[0]:  cv2.line(tempIamge,(x1,y1),(x2,y2),(0,255,0),1) cv2.imshow(window_name,tempIamge)#临时变量minLineLength = 20#全局变量minLINELENGTH = 20max_value = 100window_name = "HoughLines Demo"trackbar_value = "minLineLength"#读入图片,模式为灰度图,创建窗口scr = cv2.imread("G://homework//building.bmp")gray = cv2.cvtColor(scr,cv2.COLOR_BGR2GRAY)img = cv2.GaussianBlur(gray,(3,3),0)edges = cv2.Canny(img, 50, 150, apertureSize = 3)cv2.namedWindow(window_name)#创建滑动条cv2.createTrackbar( trackbar_value, window_name, /     minLineLength, max_value, HoughLinesP)#初始化HoughLinesP(20)if cv2.waitKey(0) == 27:  cv2.destroyAllWindows()

调用:

>>> import os>>> os.chdir("g:/homework")>>> >>> import HoughLinesminLINELENGTH: 20minLINELENGTH: 21minLINELENGTH: 22minLINELENGTH: 23minLINELENGTH: 25minLINELENGTH: 26minLINELENGTH: 27minLINELENGTH: 28

效果图:

python,opencv,霍夫变换,检测直线

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。


注:相关教程知识阅读请移步到python教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表