scikit-image库-- 圆形和椭圆形霍夫变换(十七)

最简单形式的霍夫变换是一种检测直线的方法,但它也可用于检测圆或椭圆。该算法假设检测到边缘并且它对噪声或缺失点具有鲁棒性。

圆检测

在以下示例中,Hough变换用于检测硬币位置并匹配其边缘。我们提供一系列合理的半径。对于每个半径,提取两个圆圈,我们最终保留五个最突出的候选者。结果表明,硬币位置被很好地检测到。

算法概述

给定白色背景上的黑色圆圈,我们首先猜测其半径(或半径范围)以构建新圆。该圆圈应用于原始图像的每个黑色像素,并且该圆圈的坐标在累加器中进行投票。从这种几何结构,原始圆心位置获得最高分。请注意,累加器大小构建为大于原始图像,以便检测帧外的中心。它的尺寸扩大了半径更大的两倍。

import numpy as np
import matplotlib.pyplot as plt

from skimage import data, color
from skimage.transform import hough_circle, hough_circle_peaks
from skimage.feature import canny
from skimage.draw import circle_perimeter
from skimage.util import img_as_ubyte
%matplotlib inline

# Load picture and detect edges
image = img_as_ubyte(data.coins()[160:230, 70:270]) #将图像转换为8位无符号整数格式。

edges = canny(image, sigma=3, low_threshold=10, high_threshold=50)


# Detect two radii
hough_radii = np.arange(20, 35, 2)
hough_res = hough_circle(edges, hough_radii)

# Select the most prominent 5 circles
accums, cx, cy, radii = hough_circle_peaks(hough_res, hough_radii,
                                           total_num_peaks=3)

# Draw them
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(20, 10))
image = color.gray2rgb(image)
for center_y, center_x, radius in zip(cy, cx, radii):
    circy, circx = circle_perimeter(center_y, center_x, radius)
    image[circy, circx] = (220, 20, 20)

ax.imshow(image, cmap=plt.cm.gray)
plt.show()

在这里插入图片描述

data.coins()[160:230, 70:270]改为data.coins(),total_num_peaks取15

在这里插入图片描述


椭圆检测

在该第二示例中,目的是检测咖啡杯的边缘。 基本上,这是圆的投影,即椭圆。 要解决的问题要困难得多,因为必须确定五个参数,而不是三个参数。

算法概述

该算法采用属于椭圆的两个不同点。 它假定它是主轴。 所有其他点上的循环确定椭圆传递给它们的程度。 良好的匹配对应于高累加器值。

import matplotlib.pyplot as plt

from skimage import data, color, img_as_ubyte
from skimage.feature import canny
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter

# Load picture, convert to grayscale and detect edges
image_rgb = data.coffee()[0:220, 160:420]
image_gray = color.rgb2gray(image_rgb)
edges = canny(image_gray, sigma=2.0,
              low_threshold=0.55, high_threshold=0.8)

# Perform a Hough Transform
# The accuracy corresponds to the bin size of a major axis.
# The value is chosen in order to get a single high accumulator.
# The threshold eliminates low accumulators
result = hough_ellipse(edges, accuracy=20, threshold=250,
                       min_size=100, max_size=120)
result.sort(order='accumulator')

# Estimated parameters for the ellipse
best = list(result[-1])
yc, xc, a, b = [int(round(x)) for x in best[1:5]]
orientation = best[5]

# Draw the ellipse on the original image
cy, cx = ellipse_perimeter(yc, xc, a, b, orientation) #生成椭圆周长坐标。返回属于椭圆周长的像素指数。 可用于直接索引到数组
image_rgb[cy, cx] = (0, 0, 255) #蓝色
# Draw the edge (white) and the resulting ellipse (red)
edges = color.gray2rgb(img_as_ubyte(edges))
edges[cy, cx] = (250, 0, 0)  #红色

fig2, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(8, 4),
                                sharex=True, sharey=True)

ax1.set_title('Original picture')
ax1.imshow(image_rgb)

ax2.set_title('Edge (white) and result (red)')
ax2.imshow(edges)

plt.show()

在这里插入图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值