def process_img(img):
original_image=img
processed_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
processed_img = cv2.Canny(processed_img, threshold1=200, threshold2=300)
processed_img = cv2.GaussianBlur(processed_img, (3,3), 0 )
copy=processed_img
vertices = np.array([[30, 240], [30, 100], [195, 100], [195, 240]])
processed_img = roi(processed_img, np.int32([vertices]))
verticesP = np.array([[30, 270], [30, 230], [197, 230], [197, 270]])
platform = roi(copy, np.int32([verticesP]))
# edges
#lines = cv2.HoughLinesP(platform, 1, np.pi/180, 180,np.array([]), 3, 2)
#draw_lines(processed_img,lines)
#draw_lines(original_image,lines)
#Platform lines
#imgray = cv2.cvtColor(platform,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(platform,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(original_image, contours, -1, (0,255,0), 3)
try:
platformpos=contours[0][0][0]
except:
platformpos=[[0]]
circles = cv2.HoughCircles(processed_img, cv2.HOUGH_GRADIENT, 1, 20,
param1=90, param2=5, minRadius=1, maxRadius=3)
ballpos=draw_circles(original_image,circles=circles)
return processed_img,original_image,platform,platformpos,ballpos
python类HOUGH_GRADIENT的实例源码
def read_captured_circles(self):
img = cv2.cvtColor(self.query, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(img, 7)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 30,
param1=50, param2=30, minRadius=20, maxRadius=50)
if circles is None:
return
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
if i[1] < 400:
continue
self.circlePoints.append((i[0], i[1]))
if self._debug:
self.draw_circles(circles, cimg)
def __findCircles(mask):
"""
Finds circles from mask and returns HoughCircles
"""
circles = cv2.HoughCircles(mask, cv2.HOUGH_GRADIENT, 1, 100, param1=30, param2=50)
return circles
def find_hough_circles(img):
circles = cv2.HoughCircles(pupil_img,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=80)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
def capture_white_circles(self):
self.prep_for_white_circles()
img = cv2.cvtColor(self.white_query, cv2.COLOR_BGR2GRAY)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 40,
param1=50, param2=30, minRadius=5, maxRadius=60)
if circles is None:
return
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
self.circlePoints.append((i[0], i[1]))
if self._debug:
self.draw_circles(circles, cimg)
def get_center_scale(self, calibration_image):
"""
:param calibration_image: The HSV-image to use for calculation
:return: Position of center point in image (tuple), ratio px per cm (reproduction scale)
"""
gray = cv2.cvtColor(calibration_image, cv2.COLOR_HSV2BGR)
gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 1)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 100, param1=50, param2=30, minRadius=50, maxRadius=300)
center_circle = (0, 0, 0)
min_dist = 0xFFFFFFFFFFF
for circle in circles[0]:
dist_x = abs(circle[0] - calibration_image.shape[1] / 2)
dist_y = abs(circle[1] - calibration_image.shape[0] / 2)
if(dist_x + dist_y) < min_dist:
min_dist = dist_x + dist_y
center_circle = circle
rgb = cv2.cvtColor(calibration_image, cv2.COLOR_HSV2RGB)
cv2.circle(rgb, (center_circle[0], center_circle[1]), center_circle[2], (0, 255, 0), 1)
center = center_circle[0], center_circle[1]
radius = center_circle[2]
ratio_pxcm = radius / 10.25
self.center = center
self.ratio_pxcm = ratio_pxcm
return [center, ratio_pxcm]
def findCircles():
# read image - 0 is greyscale, 1 - color
table_img = cv2.imread('training_sets/tables/extable6.png', 1)
table_img_col = table_img.copy()
table_img_grey = cv2.cvtColor(table_img, cv2.COLOR_BGR2GRAY)
table_orig = table_img_grey.copy()
# smooth
table_img_grey = cv2.blur(table_img_grey, (3,3))
# perform canny edge detection
table_canny = cv2.Canny(table_img_grey, 15, 30)
t_c_copy = table_canny.copy()
# Perform Hough circle transform
circles = cv2.HoughCircles(table_canny, cv2.HOUGH_GRADIENT, 1, 25, param1=90, param2=30, maxRadius=50, minRadius=14)
avgObjRadius = 0
stripes = []
solids = []
cueBall = (0,0)
pockets = []
if circles is not None:
print("Found circles")
circles = np.round(circles[0, :]).astype("int")
totAvgRadius = sum(i[2] for i in circles) // len(circles)
objBallCounter = 0
for x, y, r in circles:
if r <= totAvgRadius:
objBallCounter += 1
avgObjRadius += r
avgObjRadius = avgObjRadius // objBallCounter
for x, y, r in circles:
if r > 30:
pockets.append([x, y, r])
cv2.circle(table_img, (x, y), r, (0, 210, 30), 3)
else:
# store pixels within circle below
ball = isolateBall(x, y, avgObjRadius, table_img)
ballType = classifyBall(ball)
if ballType == "stripe":
stripes.append((x, y))
elif ballType == "solid":
solids.append((x, y))
elif ballType == "cue":
cueBall = (x, y)
else:
raise Exception("Ball can not be classified. X= " + x + " Y= " + y)
cv2.circle(table_img, (x, y), avgObjRadius, (150, 100, 255), 4)
#concatenate before+after images
img = np.concatenate((table_img_col, cv2.cvtColor(t_c_copy, cv2.COLOR_GRAY2BGR), table_img), axis=0)
filename = 'img.png'
cv2.imwrite(filename, img)
return filename