# -*- coding: utf-8 -*-
"""
Created on Mon Mar 22 13:22:57 2021

@author: shsbnu
"""
import cv2
img = cv2.imread("ball.jpg",1)
#色彩空间变换：由BGR->HSV
imghsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
#指定色彩范围
lcval=(35,43,46)	
hcval=(77,255,255)	
#获得只含指定色彩范围的图像
newimg = cv2.inRange(imghsv, lcval,hcval)
#在相应图像中寻找轮廓
contours,hier= cv2.findContours(newimg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#画指定位置的轮廓
#cv2.drawContours(img, contours, 2, (255, 0, 0), 3)
#寻找最大面积的轮廓
mi=-1
ma=0
c=len(contours)
for i in range(c):
    m=cv2.contourArea(contours[i])
    if m>ma:
        ma=m
        mi=i
#求最大面积轮廓的最小闭包圆
(x, y), radius = cv2.minEnclosingCircle(contours[mi])

center = (int(x), int(y))
radius = int(radius)
#在原图中画出这个闭包圆
img = cv2.circle(img, center, radius, (0, 255, 0), 2)

cv2.imshow("imgnew",img)
cv2.waitKey(0)	
cv2.destroyAllWindows()	
