def draw_court_lines_from_detections(image, boxes, classes, scores, min_score_thresh=.5): # first we have to find the corners. in (x,y) format image_height, image_witdh, channels = image.shape corners_points = [] for i in range(boxes.shape[0]): if scores is None or scores[i] > min_score_thresh: box = tuple(boxes[i].tolist()) ymin, xmin, ymax, xmax = box ymin = ymin * image_height ymax = ymax * image_height xmax = xmax * image_witdh xmin = xmin * image_witdh corners_points.append((int( (xmax + xmin) / 2), int((ymax + ymin) / 2))) # now we are going to paint de points. image_pil = Image.fromarray(np.uint8(image)).convert('RGB') draw = ImageDraw.Draw(image_pil) for p in corners_points: r = 2 draw.ellipse((720 - r, 100 - r, 720 + r, 100 + r), fill=(255, 0, 0)) draw.ellipse((p[0] - r, p[1] - r, p[0] + r, p[1] + r), fill=(255, 0, 0)) draw.point(p, fill="yellow") def print_court_polylines(best_homography_matrix): for line in BadmintonCourt.court_lines(): pts = np.float32([line]).reshape(-1, 1, 2) dst = cv2.perspectiveTransform(pts, best_homography_matrix) draw.line([(dst[0][0][0], dst[0][0][1]), (dst[1][0][0], dst[1][0][1])], fill="red") if len(corners_points) == 4: # find homography matrix court = BadmintonCourt() ground_truth_corners = court.court_external_4_corners() src_pts = np.array(ground_truth_corners, np.float32) # puede que tengan que tener el mismo orden cp = corners_points # TODO test si puedo cambiar la lista directamente. homography_candidates = order_corner_points_for_homography( corners_points) dst_pts = np.array(homography_candidates, np.float32) M, mask = cv2.findHomography(src_pts, dst_pts) print_court_polylines(M) homography_ok_points = 0 np.copyto(image, np.array(image_pil))
def print_court_polylines(best_homography_matrix): for line in BadmintonCourt.court_lines(): pts = np.float32([line]).reshape(-1, 1, 2) dst = cv2.perspectiveTransform(pts, best_homography_matrix) draw.line([(dst[0][0][0], dst[0][0][1]), (dst[1][0][0], dst[1][0][1])], fill="red")
def print_court_polylines(best_homography_matrix, sufix): #h = 1340 #w = 610 img = cv2.imread('C:\\TFM\\imagenes\\finalRioWS-35880-897.jpeg') for line in BadmintonCourt.court_lines(): pts = np.float32([line]).reshape(-1, 1, 2) dst = cv2.perspectiveTransform(pts, best_homography_matrix) cv2.line(img, (dst[0][0][0], dst[0][0][1]), (dst[1][0][0], dst[1][0][1]), 255, 3) #img2 = cv2.polylines(img, [np.int32(dst)], True, 255, 3, cv2.LINE_AA) cv2.imwrite( "C:\\TFM\\ws1\\test_final_alg\\results\\final_court_finder_lines{}.jpg" .format(sufix), img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
from model.badminton.court_model import BadmintonCourt import numpy as np import cv2 if __name__ == "__main__": img = cv2.imread('C:\\TFM\\imagenes\\finalRioWS-35880-897.jpeg') c = BadmintonCourt() ground_truth_corners = c.court_external_4_corners() fixed_points_example = [(620, 498), (369, 1019), (1548, 1019), (1288, 498)] src_pts = np.array(ground_truth_corners, np.float32) dst_pts = np.array(fixed_points_example, np.float32) M, mask = cv2.findHomography(src_pts, dst_pts) matchesMask = mask.ravel().tolist() h = 1340 w = 610 pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2) dst = cv2.perspectiveTransform(pts, M) img2 = cv2.polylines(img, [np.int32(dst)], True, 255, 3, cv2.LINE_AA) cv2.imwrite("C:\\TFM\\ws1\\test_final_alg\\final_court_example_finder.jpg", img2, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
def main(): img = cv2.imread('C:\\TFM\\imagenes\\finalRioWS-35880-897.jpeg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh3 = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY) cv2.imwrite("C:\\TFM\\ws1\\test_final_alg\\threshold.jpg", thresh3, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) edges = cv2.Canny(thresh3, 100, 200, apertureSize=3) # cv2.imwrite("C:\\TFM\\ws1\\test_final_alg\\canny.jpg", edges, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) """ line ecuation: y = m*x +n polar: punto(3,6): indica que estamos a una distancia de 3 desde el origen de coordenadas con un angulo de 6 p = x * cos(o) + y* sin(o) p = """ lines = cv2.HoughLines(thresh3, 1, np.pi / 180, 400) line_equations = [] shape = img.shape img_rows = shape[0] img_cols = shape[1] for line in lines: for rho, theta in line: a = np.cos(theta) b = np.sin(theta) #line_equations.append([[a, b], rho]) line_equations.append(Line(a, b, rho)) # calculate intersection for all lines # se puede hacer también con dos bucles for. combinations = itertools.combinations(line_equations, 2) intersections = [] for combination in combinations: try: intersections.append( LineIntersection(combination[0], combination[1], img_cols, img_rows)) except: # parallel lines or out of image. pass #### HOMOGRAPHY homography_candidates_ini = itertools.combinations(intersections, 4) homography_candidates_filter = [] sum = 0 area_threshold = 150000 print(time.asctime(time.localtime(time.time()))) for candidates_set in homography_candidates_ini: sum += 1 if sum % 100000 == 0: print(sum) intersections_line_set = set() for intersecion in candidates_set: intersections_line_set.add(intersecion.line_1) intersections_line_set.add(intersecion.line_2) #print(len(intersections_line_set)) if len(intersections_line_set) != 4: continue valid_candidate = True candidates_set_points = [ candidates_set[0].intersection, candidates_set[1].intersection, candidates_set[2].intersection, candidates_set[3].intersection ] triangles = itertools.combinations(candidates_set_points, 3) for triangle in triangles: area = triangle_area(triangle[0], triangle[1], triangle[2]) if area < area_threshold: valid_candidate = False break if valid_candidate: homography_candidates_filter.append(candidates_set_points) print(time.asctime(time.localtime(time.time()))) print(sum) print(len(homography_candidates_filter)) # homography min_error = None best_homography_matrix = None c = BadmintonCourt() ground_truth_corners = c.court_external_4_corners() homography_checked = 0 intersection_points = [] for line_intersection in intersections: intersection_points.append(line_intersection.intersection) for homography_candidates in homography_candidates_filter: homography_checked += 1 print(homography_checked) src_pts = np.array(ground_truth_corners, np.float32) dst_pts = np.array(homography_candidates, np.float32) M, mask = cv2.findHomography(src_pts, dst_pts) pts = np.array(c.court_medium_corners(), np.float32).reshape(-1, 1, 2) dst = cv2.perspectiveTransform(pts, M) homography_err = 0 for dst_h in dst: min_dst = None for dst_r in intersection_points: d = euqlidean_distance(dst_h, dst_r) if min_dst == None or d < min_dst: min_dst = d homography_err += min_dst print_court_polylines(M, str(homography_checked)) if min_error == None or homography_err < min_error: min_error = homography_err print(min_error) best_homography_matrix = M print(time.asctime(time.localtime(time.time()))) print_court_polylines(best_homography_matrix, "final")