1. 程式人生 > >opencv學習(二十一):圓檢測

opencv學習(二十一):圓檢測

檢測原理:

參考連結:https://www.cnblogs.com/ssyfj/p/9275977.html#一houghcircles方法

                    https://blog.csdn.net/zhaocj/article/details/50454847

例項演示:

程式碼如下:

#匯入cv模組
import cv2 as cv
import numpy as np

#霍夫圓檢測
def detect_circle_demo(image):
    # dst = cv.bilateralFilter(image, 0, 150, 5)  #高斯雙邊模糊,不太好調節,霍夫噪聲敏感,所以要先消除噪聲
    # cv.imshow("1",dst)
    # dst = cv.pyrMeanShiftFiltering(image,5,100)  #均值遷移,EPT邊緣保留濾波,霍夫噪聲敏感,所以要先消除噪聲
    # cv.imshow("2", dst)
    dst = cv.GaussianBlur(image,(13,15),15) #使用高斯模糊,修改卷積核ksize也可以檢測出來
    # cv.imshow("3", dst)
    gray = cv.cvtColor(dst,cv.COLOR_BGR2GRAY)
    circles = cv.HoughCircles(gray,cv.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)
    circles = np.uint16(np.around(circles))#around對資料四捨五入,為整數
    for i in circles[0,:]:
        cv.circle(image,(i[0],i[1]),i[2],(0,0,255),2)
        cv.circle(image,(i[0],i[1]),2,(255,0,0),2)   #圓心
    cv.imshow("detect_circle_demo",image)

print("------------Python Opencv Tutorial!-------------")
# 讀取影象,支援 bmp、jpg、png、tiff 等常用格式
src = cv.imread("F:/Projects/images/coins.png")
#建立視窗並顯示影象
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)   #顯示原圖
detect_circle_demo(src)
cv.waitKey(0)
#釋放視窗
cv.destroyAllWindows()

執行效果: