1、打开IDLE软件,并且载入先关的函数库。import matplotlib.pyplot as pltfrom 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, thresh泠贾高框old=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)imag髫潋啜缅e_rgb[cy, cx] = (0, 0, 255)plt.imshow(image_rgb)plt.show()
7、我们检测的结果如下。