Python采用霍夫椭圆变换检测图片中的椭圆物体

2025-10-19 13:46:28

1、打开IDLE软件,并且载入先关的函数库。

import matplotlib.pyplot as plt

from skimage import data,draw,color,transform,feature

Python采用霍夫椭圆变换检测图片中的椭圆物体

2、读取图片,并对图片进行相应的剪切,且需要进行灰度化处理。这读取的是skimage库内的文件。

image_rgb = data.coffee()[0:220, 160:420]

image_gray = color.rgb2gray(image_rgb)

Python采用霍夫椭圆变换检测图片中的椭圆物体

3、在灰度图的基础上,进行边缘检测,代码如下。

edges = feature.canny(image_gray, sigma=2.0, low_threshold=0.55, high_threshold=0.8)

Python采用霍夫椭圆变换检测图片中的椭圆物体

4、对边缘图像进行霍夫椭圆变换处理,代码如下,执行比较费时间。

result =transform.hough_ellipse(edges, accuracy=20, threshold=250,min_size=100, max_size=120)

result.sort(order='accumulator')

Python采用霍夫椭圆变换检测图片中的椭圆物体

5、获取我们的处理得到的椭圆。

best = list(result[-1])

yc, xc, a, b = [int(round(x)) for x in best[1:5]] 

orientation = best[5]

Python采用霍夫椭圆变换检测图片中的椭圆物体

6、查看效果,采用以下代码,在原图上画出我们检测得到的椭圆。

cy, cx =draw.ellipse_perimeter(yc, xc, a, b, orientation) 

image_rgb[cy, cx] = (0, 0, 255)

plt.imshow(image_rgb)

plt.show()

Python采用霍夫椭圆变换检测图片中的椭圆物体

7、我们检测的结果如下。

Python采用霍夫椭圆变换检测图片中的椭圆物体

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢