# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import  cv2
import numpy as np

#load the picture
img=cv2.imread('T1.jpg')
cv2.namedWindow('1',cv2.WINDOW_NORMAL)
cv2.namedWindow('2',cv2.WINDOW_NORMAL)
#rgb2gray
graytemp=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray1 = cv2.GaussianBlur(graytemp,(5,5),1,1)
res = cv2.threshold(gray1,15,255,cv2.THRESH_BINARY)
#vtitch = np.vstack((gray1, res[1]))
cv2.imshow('2',res[1])


#霍夫变换圆检测
#1 para：gray image
#2 para: detect method
#3 para: 1(use default value for me)
#4 para: the minist distance between two circles
#5 para: canny() higher threshold,the higher detect the obvious edge
#6 para: the hiher ,detect the more obvious circle
#7 8 para: min R,max R
circles= cv2.HoughCircles(res[1],cv2.HOUGH_GRADIENT,1,100,param1=1000,param2=30,minRadius=80,maxRadius=300)

if np.any(circles):
    #根据检测到圆的信息，画出每一个圆
    for circle in circles[0]:
        #圆的基本信息
        x=int(circle[0])
        y=int(circle[1])
        r=int(circle[2])
        #在原图用指定颜色标记出圆的位置
        img=cv2.circle(img,(x,y),r,(0,0,255),10)
        #cv.rectangle(src,(29,195),(46,206),(0,0,255),1);#画方框
#显示新图像
cv2.imshow('1',img)

#按任意键退出
cv2.waitKey(0)
cv2.destroyAllWindows()
# -







StarcoderdataJupyterScriptsDedupFiltered