Python实现霍夫圆和椭圆变换代码详解


Posted in Python onJanuary 12, 2018

在极坐标中,圆的表示方式为:

x=x0+rcosθ

y=y0+rsinθ

圆心为(x0,y0),r为半径,θ为旋转度数,值范围为0-359

如果给定圆心点和半径,则其它点是否在圆上,我们就能检测出来了。在图像中,我们将每个非0像素点作为圆心点,以一定的半径进行检测,如果有一个点在圆上,我们就对这个圆心累加一次。如果检测到一个圆,那么这个圆心点就累加到最大,成为峰值。因此,在检测结果中,一个峰值点,就对应一个圆心点。

霍夫圆检测的函数:

skimage.transform.hough_circle(image, radius)

radius是一个数组,表示半径的集合,如[3,4,5,6]

返回一个3维的数组(radius index, M, N), 第一维表示半径的索引,后面两维表示图像的尺寸。

例1:绘制两个圆形,用霍夫圆变换将它们检测出来。

import numpy as np
import matplotlib.pyplot as plt
from skimage import draw,transform,feature

img = np.zeros((250, 250,3), dtype=np.uint8)
rr, cc = draw.circle_perimeter(60, 60, 50) #以半径50画一个圆
rr1, cc1 = draw.circle_perimeter(150, 150, 60) #以半径60画一个圆
img[cc, rr,:] =255
img[cc1, rr1,:] =255

fig, (ax0,ax1) = plt.subplots(1,2, figsize=(8, 5))

ax0.imshow(img) #显示原图
ax0.set_title('origin image')

hough_radii = np.arange(50, 80, 5) #半径范围
hough_res =transform.hough_circle(img[:,:,0], hough_radii) #圆变换 

centers = [] #保存所有圆心点坐标
accums = [] #累积值
radii = [] #半径

for radius, h in zip(hough_radii, hough_res):
 #每一个半径值,取出其中两个圆
 num_peaks = 2
 peaks =feature.peak_local_max(h, num_peaks=num_peaks) #取出峰值
 centers.extend(peaks)
 accums.extend(h[peaks[:, 0], peaks[:, 1]])
 radii.extend([radius] * num_peaks)

#画出最接近的圆
image =np.copy(img)
for idx in np.argsort(accums)[::-1][:2]:
 center_x, center_y = centers[idx]
 radius = radii[idx]
 cx, cy =draw.circle_perimeter(center_y, center_x, radius)
 image[cy, cx] =(255,0,0)

ax1.imshow(image)
ax1.set_title('detected image')

结果图如下:原图中的圆用白色绘制,检测出的圆用红色绘制。

Python实现霍夫圆和椭圆变换代码详解

例2,检测出下图中存在的硬币。

Python实现霍夫圆和椭圆变换代码详解

import numpy as np
import matplotlib.pyplot as plt
from skimage import data, color,draw,transform,feature,util

image = util.img_as_ubyte(data.coins()[0:95, 70:370]) #裁剪原图片
edges =feature.canny(image, sigma=3, low_threshold=10, high_threshold=50) #检测canny边缘

fig, (ax0,ax1) = plt.subplots(1,2, figsize=(8, 5))

ax0.imshow(edges, cmap=plt.cm.gray) #显示canny边缘
ax0.set_title('original iamge')

hough_radii = np.arange(15, 30, 2) #半径范围
hough_res =transform.hough_circle(edges, hough_radii) #圆变换 

centers = [] #保存中心点坐标
accums = [] #累积值
radii = [] #半径

for radius, h in zip(hough_radii, hough_res):
 #每一个半径值,取出其中两个圆
 num_peaks = 2
 peaks =feature.peak_local_max(h, num_peaks=num_peaks) #取出峰值
 centers.extend(peaks)
 accums.extend(h[peaks[:, 0], peaks[:, 1]])
 radii.extend([radius] * num_peaks)

#画出最接近的5个圆
image = color.gray2rgb(image)
for idx in np.argsort(accums)[::-1][:5]:
 center_x, center_y = centers[idx]
 radius = radii[idx]
 cx, cy =draw.circle_perimeter(center_y, center_x, radius)
 image[cy, cx] = (255,0,0)

ax1.imshow(image)
ax1.set_title('detected image')

Python实现霍夫圆和椭圆变换代码详解

椭圆变换是类似的,使用函数为:

skimage.transform.hough_ellipse(img,accuracy, threshold, min_size, max_size)

输入参数:

img: 待检测图像。

accuracy: 使用在累加器上的短轴二进制尺寸,是一个double型的值,默认为1

thresh: 累加器阈值,默认为4

min_size: 长轴最小长度,默认为4

max_size: 短轴最大长度,默认为None,表示图片最短边的一半。

返回一个 [(accumulator, y0, x0, a, b, orientation)] 数组,accumulator表示累加器,(y0,x0)表示椭圆中心点,(a,b)分别表示长短轴,orientation表示椭圆方向

例:检测出咖啡图片中的椭圆杯口

import matplotlib.pyplot as plt
from skimage import data,draw,color,transform,feature

#加载图片,转换成灰度图并检测边缘
image_rgb = data.coffee()[0:220, 160:420] #裁剪原图像,不然速度非常慢
image_gray = color.rgb2gray(image_rgb)
edges = feature.canny(image_gray, sigma=2.0, low_threshold=0.55, high_threshold=0.8)

#执行椭圆变换
result =transform.hough_ellipse(edges, accuracy=20, threshold=250,min_size=100, max_size=120)
result.sort(order='accumulator') #根据累加器排序

#估计椭圆参数
best = list(result[-1]) #排完序后取最后一个
yc, xc, a, b = [int(round(x)) for x in best[1:5]]
orientation = best[5]

#在原图上画出椭圆
cy, cx =draw.ellipse_perimeter(yc, xc, a, b, orientation)
image_rgb[cy, cx] = (0, 0, 255) #在原图中用蓝色表示检测出的椭圆

#分别用白色表示canny边缘,用红色表示检测出的椭圆,进行对比
edges = color.gray2rgb(edges)
edges[cy, cx] = (250, 0, 0) 

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

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

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

plt.show()

Python实现霍夫圆和椭圆变换代码详解

霍夫椭圆变换速度非常慢,应避免图像太大。

总结

以上就是本文关于Python实现霍夫圆和椭圆变换代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python回调函数的使用方法
Jan 23 Python
Python使用sftp实现上传和下载功能(实例代码)
Mar 14 Python
windows下添加Python环境变量的方法汇总
May 14 Python
对python中两种列表元素去重函数性能的比较方法
Jun 29 Python
Pycharm如何打断点的方法步骤
Jun 13 Python
Pycharm+Python+PyQt5使用详解
Sep 25 Python
Python OpenCV读取显示视频的方法示例
Feb 20 Python
什么是Python中的匿名函数
Jun 02 Python
Python调用shell cmd方法代码示例解析
Jun 18 Python
Python环境管理virtualenv&virtualenvwrapper的配置详解
Jul 01 Python
Python改变对象的字符串显示的方法
Aug 01 Python
拿来就用!Python批量合并PDF的示例代码
Aug 10 Python
微信跳一跳python自动代码解读1.0
Jan 12 #Python
Tornado 多进程实现分析详解
Jan 12 #Python
快速了解Python相对导入
Jan 12 #Python
Python实现翻转数组功能示例
Jan 12 #Python
Python实现求数列和的方法示例
Jan 12 #Python
python+matplotlib演示电偶极子实例代码
Jan 12 #Python
Python实现读取及写入csv文件的方法示例
Jan 12 #Python
You might like
基于ubuntu下nginx+php+mysql安装配置的具体操作步骤
2013/04/28 PHP
php-beanstalkd消息队列类实例分享
2017/07/19 PHP
Laravel5框架自定义错误页面配置操作示例
2019/04/17 PHP
推荐20家国外的脚本下载网站
2011/04/28 Javascript
也说JavaScript中String类的replace函数
2011/09/22 Javascript
浅析js封装和作用域
2013/07/09 Javascript
JavaScript自定义日期格式化函数详细解析
2014/01/14 Javascript
jquery中交替点击事件的实现代码
2014/02/14 Javascript
Javascript中的Array数组对象详谈
2014/03/03 Javascript
javascript常见用法总结
2014/05/22 Javascript
jQuery弹出框代码封装DialogHelper
2015/01/30 Javascript
JS实现往下不断流动网页背景的方法
2015/02/27 Javascript
快速学习JavaScript的6个思维技巧
2015/10/13 Javascript
详解用vue.js和laravel实现微信支付
2017/06/23 Javascript
bootstrap精简教程_动力节点Java学院整理
2017/07/14 Javascript
利用JS如何计算字符串所占字节数示例代码
2017/09/13 Javascript
vue学习之mintui picker选择器实现省市二级联动示例
2017/10/12 Javascript
关于HTML5的data-*自定义属性的总结
2018/05/05 Javascript
JavaScript 监听组合按键思路及代码实现
2020/07/28 Javascript
如何区分vue中的v-show 与 v-if
2020/09/08 Javascript
pytyon 带有重复的全排列
2013/08/13 Python
python如何实现远程控制电脑(结合微信)
2015/12/21 Python
python图书管理系统
2020/04/05 Python
pandas 对group进行聚合的例子
2019/12/27 Python
浅谈keras使用预训练模型vgg16分类,损失和准确度不变
2020/07/02 Python
python中return不返回值的问题解析
2020/07/22 Python
英国最大的高品质珠宝和手表专家:Goldsmiths
2017/03/11 全球购物
德国网上药房:Apotal
2017/04/04 全球购物
美体小铺瑞典官方网站:The Body Shop瑞典
2018/01/27 全球购物
工作表现自我评价
2014/02/08 职场文书
毕业生就业推荐表自我鉴定
2014/03/20 职场文书
电工实训报告总结
2014/11/05 职场文书
2015年幼儿园中班工作总结
2015/04/25 职场文书
《家》读后感:万惜拯救,冷暖自知
2019/09/25 职场文书
理解深度学习之深度学习简介
2021/04/14 Python
刚学完怎么用Python实现定时任务,转头就跑去撩妹!
2021/06/05 Python