|
| 1 | + |
| 2 | +#------------------------------------# |
| 3 | +# Author: Yueh-Lin Tsou # |
| 4 | +# Update: 7/25/2019 # |
| 5 | + |
| 6 | +#------------------------------------# |
| 7 | + |
| 8 | +"""----------------------------------------- |
| 9 | +Improve hough line transform on pool tabke |
| 10 | +------------------------------------------""" |
| 11 | +""" |
| 12 | +Improvement: Use target mask to select the target, function table_selection(), largest_contours() |
| 13 | +""" |
| 14 | + |
| 15 | +import numpy as np # use numpy library as np for array object |
| 16 | +import cv2 # opencv-python |
| 17 | + |
| 18 | +# segment the pool table used color information |
| 19 | +def table_selection(ts_image): |
| 20 | + boundaries = [ ([60, 60, 0], [190, 150, 35])] # color boundary for the pool table |
| 21 | + |
| 22 | + # create NumPy arrays from the boundary |
| 23 | + for (lower, upper) in boundaries: |
| 24 | + lower = np.array(lower, dtype = "uint8") |
| 25 | + upper = np.array(upper, dtype = "uint8") |
| 26 | + |
| 27 | + ts_mask = cv2.inRange(ts_image, lower, upper) # create the mask for the selected region |
| 28 | + result = cv2.bitwise_and(ts_image, ts_image, mask = ts_mask) # remain only the pool table |
| 29 | + |
| 30 | + return result, ts_mask # return table selection and mask |
| 31 | + |
| 32 | +# Canny Edge Detection |
| 33 | +def edge_detection(image): |
| 34 | + gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert image in to grayscale |
| 35 | + gray = cv2.GaussianBlur(gray,(3, 3),0) # use gaussian blur |
| 36 | + edged = cv2.Canny(gray,50,150,apertureSize = 3) # Canny Edge Detection |
| 37 | + return edged # return edge image |
| 38 | + |
| 39 | +# Hough Lines Detection (with "cv2.HoughLines") |
| 40 | +def hough_line(image, edge_img): |
| 41 | + # Call function "cv2.HoughLines" and adjust the parameter for the function |
| 42 | + line = cv2.HoughLines(edge_img,rho=2,theta=np.pi/180, threshold=150) |
| 43 | + p, q, r = line.shape |
| 44 | + # calsulate the line point use "theta" and "rho" value |
| 45 | + |
| 46 | + # Parameters ---------------------------------------------# |
| 47 | + # rho – Distance resolution of the accumulator in pixels. # |
| 48 | + # theta – Angle resolution of the accumulator in radians. # |
| 49 | + #---------------------------------------------------------# |
| 50 | + |
| 51 | + for i in range(p): # calculate points for each line and draw the four lines |
| 52 | + for rho,theta in line[i]: |
| 53 | + a, b = np.cos(theta), np.sin(theta) # calculate slop scale |
| 54 | + x, y = a*rho, b*rho # calculate one point (x0, y0) on the line |
| 55 | + x1, y1 = int(x + 600*(-b)), int(y + 600*(a)) # calculate two end points of the line |
| 56 | + x2, y2 = int(x - 1500*(-b)), int(y - 1500*(a)) # calculate two end points of the line |
| 57 | + |
| 58 | + cv2.line(image,(x1,y1),(x2,y2),(0,255,255),1) # draw line on the image |
| 59 | + |
| 60 | + return image # return image with four lines for the pool table |
| 61 | + |
| 62 | +# select the largest pool table |
| 63 | +def largest_contours(ori_image, lc_image, lc_mask): |
| 64 | + ret,thresh = cv2.threshold(lc_mask, 40, 255, 0) # convert the mask image into black and white |
| 65 | + |
| 66 | + # find contours |
| 67 | + im,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) |
| 68 | + |
| 69 | + if len(contours) != 0: # if has contour |
| 70 | + c = max(contours, key = cv2.contourArea) # find the largest contour area |
| 71 | + |
| 72 | + mask = np.zeros(ori_image.shape, dtype='uint8') # create a empty mask |
| 73 | + mask = cv2.drawContours(mask, [c], -1, (225,225,225), -1) # create a mask for the largest pool table |
| 74 | + |
| 75 | + gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) # convert the mask into grayscale |
| 76 | + output = cv2.bitwise_and(ori_image, ori_image, mask=gray) #create the image only the largest pool table |
| 77 | + |
| 78 | + return output, mask # return the largest pool table image and it's mask |
| 79 | + |
| 80 | +# -------------------------- main -------------------------- # |
| 81 | +if __name__ == '__main__': |
| 82 | + # command >> python table_mask.py |
| 83 | + |
| 84 | + ori_image = cv2.imread('pool.jpg') # read image |
| 85 | + table_img, table_img_mask = table_selection(ori_image) # segment the pool table used color information |
| 86 | + seg_obj, obj_mask = largest_contours(ori_image, table_img, table_img_mask) # slect the largest pool table |
| 87 | + edge_img = edge_detection(seg_obj) # Canny Edge Detection |
| 88 | + line_img = hough_line(ori_image, edge_img) # Hough Lines Detection |
| 89 | + |
| 90 | + # show result image |
| 91 | + cv2.imshow("images", line_img) |
| 92 | + cv2.waitKey(0) # system pause, press esc to exit |
0 commit comments