坐看云起时|

一枚码农

园龄:7年6个月粉丝:5关注:1

opencv-python 4.13. 霍夫线变换

前言

霍夫变换是一种特征检测(feature extraction),被广泛应用在图像分析(image analysis)、计算机视觉(computer vision)以及数位影像处理(digital image processing)。

霍夫变换(Hough Transform)是图像处理中的一种特征提取技术,它通过一种投票算法检测具有特定形状的物体。该过程在一个参数空间(parameter space)中,通过计算累计空间(accumulator space)中局部最大值(local maximum)得到一个符合该特定形状的集合作为霍夫变换结果。霍夫变换于1962年由Paul Hough 首次提出,后于1972年由Richard Duda和Peter Hart推广使用,经典霍夫变换用来检测图像中的直线,后来霍夫变换扩展到任意形状物体的识别,多为圆和椭圆。

什么是霍夫变换直线检测

Hough直线检测的基本原理在于利用点与线的对偶性,在我们的直线检测任务中,即图像空间中的直线与参数空间中的点是一一对应的,参数空间中的直线与图像空间中的点也是一一对应的。这意味着我们可以得出两个非常有用的结论:

  • 图像空间中的每条直线在参数空间中都对应着单独一个点来表示。
  • 图像空间中的直线上任何一部分线段在参数空间对应的是同一个点。

因此Hough直线检测算法就是把在图像空间中的直线检测问题转换到参数空间中对点的检测问题,通过在参数空间里寻找峰值来完成直线检测任务。

霍夫变换直线检测原理详解

对于直角坐标系中的任意一点A(x0 , y0),经过点A的直线满足y = mx + c(m是斜率,c是截距),那么在x−y平面过点A(x0,y0)的直线族可以用y0 = mx0 + c 表示,但对于垂直于x轴的直线斜率是无穷大的则无法表示。为此,将直角坐标系转换到极坐标系就能解决该特殊情况。

在极坐标系中,表示直线的方程为ρ = xcosθ + ysinθ(ρ为原点到直线的距离),如下图1所示:
image
图1 直线方程极坐标表示

现利用霍夫变换直线检测原理对上图进行分析,为方便接下来对上图的分析,将上图进行数据形象化表示如下图2所示
image
图2 直线数据形象化表示

image
表1 直线方程参数确定

image

OpenCV中的霍夫变换

在 OpenCV 中提供了两个霍夫直线检测的函数,一个是标准霍夫变换,另一个是概率霍夫变换。

1. 标准霍夫变换

先学习一下标准霍夫变换吧,该变化方式也叫做多尺度霍夫变换。该方法使用的函数是 cv2.HoughLines,函数原型如下
lines = cv2.HoughLines(image, rho, theta, threshold[, lines[, srn[, stn[, min_theta[, max_theta]]]]])
参数说明:

  • image:输入 8 位灰度图像;
  • rho:生成极坐标时像素扫描步长;
  • theta:生成极坐标时候的角度步长;
  • threshold:阈值;
  • lines:返回值,极坐标表示的直线;
  • sen:是否应用多尺度的霍夫变换,如果不是设置 0 表示经典霍夫变换;
  • stn:是否应用多尺度的霍夫变换,如果不是设置 0 表示经典霍夫变换;
  • min_theta:角度扫描范围最小值;
  • max_theta:角度扫描范围最大值。

该函数的返回值是( θ,ρ ),其中 ρ 的单位是像素, θ 的单位是弧度。

import cv2 as cv
import numpy as np

img = cv.imread(r'C:\Users\yuyalong\Pictures\Saved Pictures\build.jpg')
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# 直方图均衡
dst = cv.equalizeHist(gray_img)
# 高斯滤波降噪
# gaussian = cv.GaussianBlur(dst, (9, 9), 0)
# cv.imshow("gaussian", gaussian)

# 边缘检测
edges = cv.Canny(dst, 50, 200)
cv.imshow("edges", edges)

# Hough 直线检测
# 重点注意第四个参数 阈值,只有累加后的值高于阈值时才被认为是一条直线,也可以把它看成能检测到的直线的最短长度(以像素点为单位)
# 在霍夫空间理解为:至少有多少条正弦曲线交于一点才被认为是直线
lines = cv.HoughLines(edges, 1.0, np.pi / 180, 200)

for line in lines:
    # line[0]存储的是点到直线的极径和极角,其中极角是弧度表示的,theta是弧度
    rho, theta = line[0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a * rho
    y0 = b * rho

    # 下图 1000 的目的是为了将线段延长
    # 以 (x0,y0) 为基础,进行延长
    x1 = int(x0 + 1000 * (-b))
    y1 = int(y0 + 1000 * (a))
    x2 = int(x0 - 1000 * (-b))
    y2 = int(y0 - 1000 * (a))
    cv.line(img, (x1, y1), (x2, y2), (255, 255, 0), 2)

cv.imshow("src", img)
cv.waitKey()

image

2.概率霍夫变换(Probabilistic Hough Transform)

概率霍夫变换是一种概率直线检测,它是针对于上文标准霍夫检测的优化,核心点是采取概率挑选机制,选取一些点出来进行计算,相当于降采样。 请参见下图,其中比较霍夫空间中的霍夫变换和概率霍夫变换。(图片提供:Franck Bettinger的主页)
image
OpenCV实现基于使用Matas,J。和Galambos,C。和Kittler,J.V。[122]的渐进概率Hough变换的线的鲁棒检测。 使用的函数是cv.HoughLinesP()。
lines = cv2.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]])
参数说明:

  • src:输入 8-bit 的灰度图像;
  • rho:像素为单位的距离精度,double 类型的,推荐用 1.0;
  • theta:以弧度为单位的角度精度,推荐用 numpy.pi/180;
  • threshold:阈值;
  • lines:输出的极坐标来表示直线;
  • minLineLength:最短长度阈值,比这个长度短的线会被排除;
  • maxLineGap:最大间隔,如果小于此值,这两条直线就被看成是一条直线。

该函数还有一个优点,返回值是一条条线段,不用我们在进行转换了

import cv2 as cv
import numpy as np

img = cv.imread(r'C:\Users\yuyalong\Pictures\Saved Pictures\build.jpg')
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# 直方图均衡
dst = cv.equalizeHist(gray_img)
# 高斯滤波降噪
# gaussian = cv.GaussianBlur(dst, (9, 9), 0)
# cv.imshow("gaussian", gaussian)

# 边缘检测
edges = cv.Canny(dst, 50, 200)
cv.imshow("edges", edges)

# Hough 直线检测
# 重点注意第四个参数 阈值,只有累加后的值高于阈值时才被认为是一条直线,也可以把它看成能检测到的直线的最短长度(以像素点为单位)
# 在霍夫空间理解为:至少有多少条正弦曲线交于一点才被认为是直线
# lines = cv.HoughLines(edges, 1.0, np.pi / 180, 200)

# 概率霍夫变换
lines = cv.HoughLinesP(edges, 1, np.pi / 180, 200, minLineLength=200, maxLineGap=10)

for line in lines:
    # # line[0]存储的是点到直线的极径和极角,其中极角是弧度表示的,theta是弧度
    # rho, theta = line[0]
    # a = np.cos(theta)
    # b = np.sin(theta)
    # x0 = a * rho
    # y0 = b * rho
    #
    # # 下图 1000 的目的是为了将线段延长
    # # 以 (x0,y0) 为基础,进行延长
    # x1 = int(x0 + 1000 * (-b))
    # y1 = int(y0 + 1000 * (a))
    # x2 = int(x0 - 1000 * (-b))
    # y2 = int(y0 - 1000 * (a))
    # cv.line(img, (x1, y1), (x2, y2), (255, 255, 0), 2)

    x1, y1, x2, y2 = line[0]
    cv.line(img, (x1, y1), (x2, y2), (255, 255, 0), 2)

cv.imshow("src", img)
cv.waitKey()

image

参考博客:

本文作者:一枚码农

本文链接:https://www.cnblogs.com/yimeimanong/p/17293590.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   一枚码农  阅读(188)  评论(0编辑  收藏  举报
 
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Sold Out Hawk
  2. 2 光辉岁月 Beyond
Sold Out - Hawk
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Jon Steingard

作曲 : Jon Steingard

I ain't like no one you met before

I'm running for the front

When they're all running for the door

And I won't sit down won't back out

You can't ever shut me up

Cause I'm on a mission

And I won't quit now

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

Because the battle's already been won

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to show

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out

This ain't just some temporary phase

You can't face this kind of grace

And leave the way you came

This is permanent with intent

And there won't be no stopping it now

I'm on a mission and it's heaven sent

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

Cause my soul is like a stadium

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to shout

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out

No trials coming against me

Could put a dent in my passion

They're just an opportunity

To put my faith into action

In a world full of followers

I'll be a leader

In a world full of doubters

I'll be a believer

I'm stepping out without a hesitation

I ain't got nothing left to be afraid of

I'm sold out

I'm no longer living

Just for myself

Running after Jesus

With my whole heart

And now I'm ready to show

I am sold out

I'm sold out

With every single

Step that I take now

With every drop of blood

Left in my veins

I'm gonna be making it count

I am sold out