|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +def show_img(img): |
| 5 | + cv2.imshow('image', img) |
| 6 | + cv2.waitKey(0) |
| 7 | + |
| 8 | +def get_dis_to_line(point, line_pt_1, line_pt_2, img): |
| 9 | + line_pt_1.append(0) |
| 10 | + line_pt_2.append(0) |
| 11 | + point.append(0) |
| 12 | + line_vector = np.array(line_pt_2) - np.array(line_pt_1) |
| 13 | + perp_line = np.cross(line_vector, [0, 0, 1]) |
| 14 | + unit_perp_line = perp_line / np.linalg.norm(perp_line) |
| 15 | + dis_to_line = np.dot(unit_perp_line, np.array(line_pt_2) - np.array(point)) |
| 16 | + |
| 17 | + perp_line_with_dis = unit_perp_line * dis_to_line |
| 18 | + cv2.line(img, (point[0], point[1]), (point[0] + int(perp_line_with_dis[0]), point[1] + int(perp_line_with_dis[1])), (255, 0, 0), 2) |
| 19 | + return abs(dis_to_line) |
| 20 | + |
| 21 | +def get_grouped_lines(lines): |
| 22 | + if not(lines is None): |
| 23 | + # Group the nearby lines together (execpecting 3 groups) |
| 24 | + criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) |
| 25 | + ret, label, grouped_lines = cv2.kmeans(lines, 3,None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) |
| 26 | + return grouped_lines |
| 27 | + else: |
| 28 | + print "There were no lines found" |
| 29 | + return [] |
| 30 | + |
| 31 | +cap = cv2.VideoCapture('./sonar_vid.avi') |
| 32 | + |
| 33 | +while(cap.isOpened()): |
| 34 | + |
| 35 | + ret, raw_img = cap.read() |
| 36 | + |
| 37 | + # Threshold the image |
| 38 | + gray_img = cv2.cvtColor(raw_img, cv2.COLOR_BGR2GRAY) |
| 39 | + ret, thresh_img = cv2.threshold(gray_img, 30, 255, cv2.THRESH_BINARY) |
| 40 | + |
| 41 | + |
| 42 | + # Find Hough lines |
| 43 | + |
| 44 | + lines = cv2.HoughLines(thresh_img, 1, np.pi/180, threshold = 30) |
| 45 | + |
| 46 | + # Draw the submarine on the image |
| 47 | + sub_pos = (int(len(raw_img[0])/2), len(raw_img) - 20) |
| 48 | + cv2.circle(raw_img, sub_pos, 5, (100, 255, 0), -1) |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + # Draw the lines on the original image and find the distance to the lines from the submarine |
| 53 | + |
| 54 | + dis_to_lines = [] |
| 55 | + text_pos_counter = 0 |
| 56 | + |
| 57 | + for line in lines: |
| 58 | + line = line[0] |
| 59 | + theta = line[1] |
| 60 | + rho = line[0] |
| 61 | + print "theta: ", theta, " rho: ", rho |
| 62 | + a = np.cos(theta) |
| 63 | + b = np.sin(theta) |
| 64 | + x0 = a*rho |
| 65 | + y0 = b*rho |
| 66 | + length = 2000 |
| 67 | + x1 = int(x0 + length*(-b)) |
| 68 | + y1 = int(y0 + length*(a)) |
| 69 | + x2 = int(x0 - length*(-b)) |
| 70 | + y2 = int(y0 - length*(a)) |
| 71 | + |
| 72 | + cv2.line(raw_img, (x1, y1), (x2, y2), (0, 0, 255), 2) |
| 73 | + |
| 74 | + dis_to_line = get_dis_to_line([sub_pos[0], sub_pos[1]], [x1, y1], [x2, y2], raw_img) |
| 75 | + print "dis_to_line: ", dis_to_line |
| 76 | + # cv2.putText(raw_img, "Line " + str(text_pos_counter) + " dis: " + str(dis_to_line), (100 , 200 + 100 * text_pos_counter), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255)) |
| 77 | + text_pos_counter += 1 |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + # cv2.imshow('video',cv2.resize(raw_img, ( int(len(raw_img[0]) * 0.5), int(len(raw_img) * 0.5)) )) |
| 82 | + cv2.imshow('video', raw_img) |
| 83 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 84 | + print "Breaking loop" |
| 85 | + break |
| 86 | +cap.release() |
| 87 | +cv2.destroyAllWindows() |
| 88 | + |
| 89 | + |
| 90 | +print "End of program" |
0 commit comments