在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → 霍夫变换直线检测源代码.py

霍夫变换直线检测源代码.py

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:3.92KB
  • 下载次数:9
  • 浏览次数:113
  • 发布时间:2022-01-09
  • 实例类别:Python语言基础
  • 发 布 人:执行者
  • 文件格式:.py
  • 所需积分:2
 相关标签: 霍夫变换 源代码 代码 检测

实例介绍

【实例简介】霍夫变换直线检测源代码.py

【实例截图】

from clipboard

【核心代码】


#coding:utf-8  import numpy as np import matplotlib.pyplot as plt import os import cv2 "霍夫变换直线检测算法" def lines_detector_hough(edge,ThetaDim = None,DistStep = None,threshold = None,halfThetaWindowSize = 2,halfDistWindowSize = None): '''  :param edge: 经过边缘检测得到的二值图  :param ThetaDim: hough空间中theta轴的刻度数量([0,pi)均分为多少份),反应theta轴的粒度,越大粒度越细  :param DistStep: hough空间中dist轴的划分粒度,dist轴的最小单位长度  :param threshold: 投票表决认定存在直线的起始阈值  :return: 返回检测出的所有直线的参数(theta,dist)  '''  imgsize = edge.shape if ThetaDim == None:
        ThetaDim = 90 #90个区间,相当于4度一区间  if DistStep == None:
        DistStep = 1  MaxDist = np.sqrt(imgsize[0]**2  imgsize[1]**2) #对角线的长度  DistDim = int(np.ceil(MaxDist/DistStep)) #r的刻度数量   if halfDistWindowSize == None:
        halfDistWindowSize = int(DistDim/50)
    accumulator = np.zeros((ThetaDim,DistDim)) # theta的范围是[0,pi). 在这里将[0,pi)进行了线性映射.类似的,也对Dist轴进行了线性映射   sinTheta = [np.sin(t*np.pi/ThetaDim) for t in range(ThetaDim)]
    cosTheta = [np.cos(t*np.pi/ThetaDim) for t in range(ThetaDim)] for i in range(imgsize[0]): for j in range(imgsize[1]): if not edge[i,j] == 0: for k in range(ThetaDim):
                    accumulator[k][int(round((i*cosTheta[k] j*sinTheta[k])*DistDim/MaxDist))]  = 1   M = accumulator.max() if threshold == None:
        threshold = int(M*2.3875/10) #大于投票数的都是直线  result = np.array(np.where(accumulator > threshold)) # 阈值化  temp = [[],[]] for i in range(result.shape[1]):
        eight_neiborhood = accumulator[max(0, result[0,i] - halfThetaWindowSize 1):min(result[0,i]   halfThetaWindowSize, accumulator.shape[0]), max(0, result[1,i] - halfDistWindowSize 1):min(result[1,i]   halfDistWindowSize, accumulator.shape[1])] if (accumulator[result[0,i],result[1,i]] >= eight_neiborhood).all():
            temp[0].append(result[0,i])
            temp[1].append(result[1,i])
    result = np.array(temp) # 非极大值抑制   result = result.astype(np.float64)
    result[0] = result[0]*np.pi/ThetaDim #最终角度0  result[1] = result[1]*MaxDist/DistDim #最终距离r   return result #划红线的函数 def drawLines(lines,edge,color = (255,0,0),err = 3): if len(edge.shape) == 2:
        result = np.dstack((edge,edge,edge)) else:
        result = edge
    Cos = np.cos(lines[0])
    Sin = np.sin(lines[0]) for i in range(edge.shape[0]): for j in range(edge.shape[1]):
            e = np.abs(lines[1] - i*Cos - j*Sin) if (e < err).any():
                result[i,j] = color return result #读取图片 if __name__=='__main__':
    pic_path = 'C:/Users/lixin/Desktop/科研/论文资料/霍夫变换/000/'  pics = os.listdir(pic_path) for i in pics: if i[-5:] == '.jpeg' or i[-4:] == '.jpg':
            img = plt.imread(pic_path i)
            blurred = cv2.GaussianBlur(img, (3, 3), 0) #卷积高斯去噪  plt.imshow(blurred,cmap='gray')
            plt.axis('off')
            plt.show() #灰度化  生成二值图  if not len(blurred.shape) == 2:
                gray = cv2.cvtColor(blurred, cv2.COLOR_RGB2GRAY) else:
                gray = blurred
            edge = cv2.Canny(gray, 50, 150) # 二值图 (0  255) 得到 canny边缘检测的结果  #返回直线的参数  根据原始图像上画红线  lines = lines_detector_hough(edge)
            final_img = drawLines(lines,blurred)
            
            plt.imshow(final_img,cmap='gray')
            plt.axis('off')
            plt.show()


实例下载地址

霍夫变换直线检测源代码.py

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警