Python采用霍夫椭圆变换检测图片中的椭圆物体
1、打开IDLE软件,并且载入先关的函数库。
import matplotlib.pyplot as plt
from skimage import data,draw,color,transform,feature
2、读取图片,并对图片进行相应的剪切,且需要进行灰度化处理。这读取的是skimage库内的文件。
image_rgb = data.coffee()[0:220, 160:420]
image_gray = color.rgb2gray(image_rgb)
3、在灰度图的基础上,进行边缘检测,代码如下。
edges = feature.canny(image_gray, sigma=2.0, low_threshold=0.55, high_threshold=0.8)
4、对边缘图像进行霍夫椭圆变换处理,代码如下,执行比较费时间。
result =transform.hough_ellipse(edges, accuracy=20, threshold=250,min_size=100, max_size=120)
result.sort(order='accumulator')
5、获取我们的处理得到的椭圆。
best = list(result[-1])
yc, xc, a, b = [int(round(x)) for x in best[1:5]]
orientation = best[5]
6、查看效果,采用以下代码,在原图上画出我们检测得到的椭圆。
cy, cx =draw.ellipse_perimeter(yc, xc, a, b, orientation)
image_rgb[cy, cx] = (0, 0, 255)
plt.imshow(image_rgb)
plt.show()
7、我们检测的结果如下。