1. 程式人生 > >【opencv自學筆記】14:霍夫圓檢測

【opencv自學筆記】14:霍夫圓檢測

霍夫圓檢測
邊緣保留濾波——轉灰度圖——霍夫圓檢測

# -*- coding: utf-8 -*-
"""
霍夫圓檢測
邊緣保留濾波————轉灰度圖————霍夫圓檢測
@author: LNP
"""
import cv2 as cv
import numpy as np

def detect_circle_demo(image):
    #hough圓檢測對噪聲敏感,必須濾波
    blur=cv.pyrMeanShiftFiltering(image,10,100)
    cv.imshow('blur',blur)
    gray=cv.cvtColor(blur,cv.COLOR_BGR2GRAY)
    circle=cv.HoughCircles(gray,cv.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)
    circle=np.uint16(np.around(circle))
    print(circle.shape)
    for i in circle[0,:]:
        cv.circle(image,(i[0],i[1]),i[2],(0,0,255),2)
        cv.circle(image,(i[0],i[1]),2,(0,0,255),2)
    cv.imshow('detect_circle_demo',image)

src=cv.imread("D:/Study/opencv/code/circle.png")
cv.imshow('src',src)
detect_circle_demo(src)

cv.waitKey(0)
cv.destroyAllWindows()