def power_lines_approx_parallel_123(detected_lines): parallel_bool_12 = True parallel_bool_13 = True parallel_bool_23= True crossover_bool = False radians_allowed = np.pi/3 img_heigth = 1080 if detected_lines[0][4] == True: a1,b1,length1 = find_cart_line_eq(detected_lines[0][0],detected_lines[0][1],detected_lines[0][2],detected_lines[0][3]) rho1, theta1, length1 = perpendicular_polar_line(a1,b1,length1) if detected_lines[1][4] == True: a2,b2,length2 = find_cart_line_eq(detected_lines[1][0],detected_lines[1][1],detected_lines[1][2],detected_lines[1][3]) rho2, theta2, length2 = perpendicular_polar_line(a2,b2,length2) if detected_lines[2][4] == True: a3,b3,length3 = find_cart_line_eq(detected_lines[2][0],detected_lines[2][1],detected_lines[2][2],detected_lines[2][3]) rho3, theta3, length3 = perpendicular_polar_line(a3,b3,length3) if detected_lines[0][4] and detected_lines[1][4]: if not lines_result_approx_parallel(theta1,theta2,a1,b1,a2,b2,img_heigth,crossover_bool ,radians_allowed): parallel_bool_12 = False if detected_lines[0][4] and detected_lines[2][4]: if not (lines_result_approx_parallel(theta3,theta1,a3,b3,a1,b1,img_heigth,crossover_bool,radians_allowed)): parallel_bool_13 = False if detected_lines[1][4] and detected_lines[2][4]: if not lines_result_approx_parallel(theta3,theta2,a3,b3,a2,b2,img_heigth,crossover_bool ,radians_allowed): parallel_bool_23= False return parallel_bool_12, parallel_bool_13, parallel_bool_23
def label_lines_org(lines, ransac_lines,left_x_bound, right_x_bound): left_lines=[] mid_lines=[] right_lines=[] other_lines=[] power_mast_lines =[] print(ransac_lines) left_idx, mid_idx, right_idx = find_mid_line(ransac_lines) crossover_bool = True radians_allowed = np.pi/4 for i in range(0,len(lines)): x1 = lines[i][0][0] y1 = lines[i][0][1] x2 = lines[i][0][2] y2 = lines[i][0][3] a,b,length = find_cart_line_eq(x1,y1,x2,y2) rho, theta, length = perpendicular_polar_line(a,b,length) if (x1 < left_x_bound) or (x2 < left_x_bound) or (x1 > right_x_bound) or (x2 > right_x_bound): other_lines.append(lines[i][0]) #print("Other line") else: #print("går forbiii") a_l,b_l,length = find_cart_line_eq(ransac_lines[left_idx][0],ransac_lines[left_idx][1],ransac_lines[left_idx][2],ransac_lines[left_idx][3]) rho_l, theta_l, length = perpendicular_polar_line(a_l,b_l,length) close_bool = line_within_reach(a_l,b_l,x1,y1,x2,y2) parallel_bool = lines_approx_parallel(theta_l,theta,a_l,b_l,a,b,1920,crossover_bool,radians_allowed ) if close_bool and parallel_bool: left_lines.append(lines[i][0]) else: #print("not left because par,close : ", parallel_bool, close_bool) #a_m = ransac_lines[mid_idx][0] #b_m = ransac_lines[mid_idx][1] #theta_m = ransac_lines[mid_idx][3] a_m,b_m,length = find_cart_line_eq(ransac_lines[mid_idx][0],ransac_lines[mid_idx][1],ransac_lines[mid_idx][2],ransac_lines[mid_idx][3]) rho_m, theta_m, length = perpendicular_polar_line(a_m,b_m,length) close_bool = line_within_reach(a_m,b_m,x1,y1,x2,y2) parallel_bool = lines_approx_parallel(theta_m,theta,a_m,b_m,a,b,1920,crossover_bool,radians_allowed ) if close_bool and parallel_bool: mid_lines.append(lines[i][0]) #print("Accepted as mid line ",theta_m,theta,b_m,b) else: #print("not mid because par,close : ", parallel_bool, close_bool) #print("NOT accepted as mid line ",np.rad2deg(theta_m),np.rad2deg(theta),b_m,b) a_r,b_r,length = find_cart_line_eq(ransac_lines[right_idx][0],ransac_lines[right_idx][1],ransac_lines[right_idx][2],ransac_lines[right_idx][3]) rho_r, theta_r, length = perpendicular_polar_line(a_r,b_r,length) close_bool = line_within_reach(a_r,b_r,x1,y1,x2,y2) parallel_bool = lines_approx_parallel(theta_r,theta,a_r,b_r,a,b,1920,crossover_bool,radians_allowed ) if close_bool and parallel_bool: right_lines.append(lines[i][0]) else: #print("not right because par,close : ", parallel_bool, close_bool) power_mast_lines.append(lines[i][0]) #print("power mast line") print(len(power_mast_lines), " power mast lines ") return left_lines,mid_lines,right_lines, other_lines
def AMA_RANSAC_alg(img): edges = cv2.Canny(img, 350, 110) minLineLength = 10 maxLineGap = 8 lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength, maxLineGap) binary_img = np.zeros(np.shape(img)) binary_img = draw_hough_lines(lines, binary_img, 1) detected_lines_are_parallel = False times_tried = 0 while detected_lines_are_parallel == False: max_attempts = 50 + times_tried * 100 detected_lines = three_line_RANSAC(binary_img, max_attempts) if len(detected_lines) == 0: print("no detected edges") break a1, b1, length1 = find_cart_line_eq(detected_lines[0][0], detected_lines[0][1], detected_lines[0][2], detected_lines[0][3]) rho1, theta1, length1 = perpendicular_polar_line(a1, b1, length1) a2, b2, length2 = find_cart_line_eq(detected_lines[1][0], detected_lines[1][1], detected_lines[1][2], detected_lines[1][3]) rho2, theta2, length2 = perpendicular_polar_line(a2, b2, length2) crossover_bool = True radians_allowed = np.pi / 3 img_heigth = np.shape(img)[0] if lines_approx_parallel(theta1, theta2, a1, b1, a2, b2, img_heigth, crossover_bool, radians_allowed): a3, b3, length3 = find_cart_line_eq(detected_lines[2][0], detected_lines[2][1], detected_lines[2][2], detected_lines[2][3]) rho3, theta3, length3 = perpendicular_polar_line(a3, b3, length3) if (lines_approx_parallel( theta1, theta3, a1, b1, a3, b3, img_heigth, crossover_bool, radians_allowed) and lines_approx_parallel( theta2, theta3, a2, b2, a3, b3, img_heigth, crossover_bool, radians_allowed)): detected_lines_are_parallel = True times_tried += 1 print(times_tried) if times_tried == 5: detected_lines = RANSAC_alg(img) detected_lines_are_parallel = True return detected_lines
def label_lines(lines, ransac_lines): left_lines = [] mid_lines = [] right_lines = [] other_lines = [] power_mast_lines = [] accepted_angle = np.pi / 2 l_index, m_index, r_index = find_mid_line(ransac_lines) for i in range(0, len(lines)): x1 = lines[i][0][0] y1 = lines[i][0][1] x2 = lines[i][0][2] y2 = lines[i][0][3] a, b, length = find_cart_line_eq(x1, y1, x2, y2) left_x_bound = int( (y1 - ransac_lines[l_index][1]) / ransac_lines[l_index][0]) - 20 right_x_bound = int( (y2 - ransac_lines[r_index][1]) / ransac_lines[r_index][0]) + 20 rho, theta, length = perpendicular_polar_line(a, b, length) if (x1 < left_x_bound) or (x2 < left_x_bound) or ( x1 > right_x_bound) or (x2 > right_x_bound): other_lines.append(lines[i][0]) else: a_l = ransac_lines[l_index][0] b_l = ransac_lines[l_index][1] theta_l = ransac_lines[l_index][3] close_bool = line_within_reach(a_l, b_l, x1, y1, x2, y2) parallel_bool = lines_approx_parallel(theta_l, theta, a_l, b_l, a, b, 1920, False, accepted_angle) if close_bool and parallel_bool: left_lines.append(lines[i][0]) else: a_m = ransac_lines[m_index][0] b_m = ransac_lines[m_index][1] theta_m = ransac_lines[m_index][3] close_bool = line_within_reach(a_m, b_m, x1, y1, x2, y2) parallel_bool = lines_approx_parallel(theta_m, theta, a_m, b_m, a, b, 1920, False, accepted_angle) if close_bool and parallel_bool: mid_lines.append(lines[i][0]) #print("Accepted as mid line ",theta_m,theta,b_m,b) else: #print("NOT accepted as mid line ",np.rad2deg(theta_m),np.rad2deg(theta),b_m,b) a_r = ransac_lines[r_index][0] b_r = ransac_lines[r_index][1] theta_r = ransac_lines[r_index][3] close_bool = line_within_reach(a_r, b_r, x1, y1, x2, y2) parallel_bool = lines_approx_parallel( theta_r, theta, a_r, b_r, a, b, 1920, False, accepted_angle + 0.5) if close_bool and parallel_bool: right_lines.append(lines[i][0]) else: power_mast_lines.append(lines[i][0]) return left_lines, mid_lines, right_lines, other_lines, power_mast_lines
def ThreeLineRANSAC(binary_img,img_org,first_attempt): detected_lines_are_parallel = False times_tried = 0 while detected_lines_are_parallel == False: max_attempts = first_attempt+ times_tried*100 detected_lines= three_line_RANSAC(binary_img,max_attempts) if len(detected_lines) == 0: print("no detected edges") break print("RANSAC lines - ",detected_lines) a1,b1,length1 = find_cart_line_eq(detected_lines[0][0],detected_lines[0][1],detected_lines[0][2],detected_lines[0][3]) rho1, theta1, length1 = perpendicular_polar_line(a1,b1,length1) a2,b2,length2 = find_cart_line_eq(detected_lines[1][0],detected_lines[1][1],detected_lines[1][2],detected_lines[1][3]) rho2, theta2, length2 = perpendicular_polar_line(a2,b2,length2) if lines_approx_parallel(theta1,theta2,a1,b1,a2,b2,1920,True,np.pi/6): a3,b3,length3 = find_cart_line_eq(detected_lines[2][0],detected_lines[2][1],detected_lines[2][2],detected_lines[2][3]) rho3, theta3, length3 = perpendicular_polar_line(a3,b3,length3) if (lines_approx_parallel(theta3,theta1,a3,b3,a1,b1,1920,True,np.pi/6) and lines_approx_parallel(theta3,theta2,a3,b3,a2,b2,1920,True,np.pi/6)): detected_lines_are_parallel = True else: print("lines are not parallel") times_tried += 1 print("Times tried - ", times_tried) #print(times_tried) if times_tried == 5 : print("PROBLEM PROBLEM PROBLEM!!!") a3,b3,length3 = find_cart_line_eq(detected_lines[2][0],detected_lines[2][1],detected_lines[2][2],detected_lines[2][3]) rho3, theta3, length3 = perpendicular_polar_line(a3,b3,length3) img_lined = img_org detected_lines_are_parallel = True power_lines = np.array([[a1,b1,rho1,theta1,length1],[a2,b2,rho2,theta2,length2],[a3,b3,rho3,theta3,length3]]) indx = find_mid_line(power_lines) for i in range(0,len(detected_lines)): if i == indx: img_lined = cv2.line(img_org,(detected_lines[i][0],detected_lines[i][1]),(detected_lines[i][2],detected_lines[i][3]),(0,255,0),2) else: img_lined = cv2.line(img_org,(detected_lines[i][0],detected_lines[i][1]),(detected_lines[i][2],detected_lines[i][3]),(0,0,255),2) return img_lined,power_lines
def draw_AMA_RANSAC_HTIVS_results(img,power_lines): img_lined = img if power_lines[0][4] == True: a2,b2,length2 = find_cart_line_eq(power_lines[0][0],power_lines[0][1],power_lines[0][2],power_lines[0][3]) rho2, theta2, length2 = perpendicular_polar_line(a2,b2,length2) print("LEFT LINE : ", np.rad2deg(theta2)) img_lined = draw_result_line(power_lines[0][0],power_lines[0][1],img,0,0,255) if power_lines[1][4] == True: a2,b2,length2 = find_cart_line_eq(power_lines[1][0],power_lines[1][1],power_lines[1][2],power_lines[1][3]) rho2, theta2, length2 = perpendicular_polar_line(a2,b2,length2) print("MID LINE : ", np.rad2deg(theta2)) img_lined = draw_result_line(power_lines[1][0],power_lines[1][1],img,0,255,0) if power_lines[2][4] == True: a2,b2,length2 = find_cart_line_eq(power_lines[2][0],power_lines[2][1],power_lines[2][2],power_lines[2][3]) rho2, theta2, length2 = perpendicular_polar_line(a2,b2,length2) print("RIGHT LINE : ", np.rad2deg(theta2)) img_lined = draw_result_line(power_lines[2][0],power_lines[2][1],img,255,0,0) return img_lined
def power_lines_approx_parallel(detected_lines): parallel_bool = True if detected_lines[0][4] == True: a1, b1, length1 = find_cart_line_eq(detected_lines[0][0], detected_lines[0][1], detected_lines[0][2], detected_lines[0][3]) rho1, theta1, length1 = perpendicular_polar_line(a1, b1, length1) if detected_lines[1][4] == True: a2, b2, length2 = find_cart_line_eq(detected_lines[1][0], detected_lines[1][1], detected_lines[1][2], detected_lines[1][3]) rho2, theta2, length2 = perpendicular_polar_line(a2, b2, length2) if detected_lines[2][4] == True: a3, b3, length3 = find_cart_line_eq(detected_lines[2][0], detected_lines[2][1], detected_lines[2][2], detected_lines[2][3]) rho3, theta3, length3 = perpendicular_polar_line(a3, b3, length3) if detected_lines[0][4] and detected_lines[1][4]: if not lines_approx_parallel(theta1, theta2, a1, b1, a2, b2, 1920, True, np.pi / 2): parallel_bool = False if detected_lines[0][4] and detected_lines[2][4]: if not (lines_approx_parallel(theta3, theta1, a3, b3, a1, b1, 1920, True, np.pi / 2)): parallel_bool = False if detected_lines[1][4] and detected_lines[2][4]: if not lines_approx_parallel(theta3, theta2, a3, b3, a2, b2, 1920, True, np.pi / 2): parallel_bool = False #print("Power Lines Parallel : " ,parallel_bool) return parallel_bool
def power_mast_detector(power_mast_lines, theta_l, a_l, b_l, theta_m, a_m, b_m, theta_r, a_r, b_r, l_bool, m_bool, r_bool): accepted_lines = [] other_lines = [] for i in range(0, len(power_mast_lines)): a, b, length = find_cart_line_eq(power_mast_lines[i][0], power_mast_lines[i][1], power_mast_lines[i][2], power_mast_lines[i][3]) rho, theta, length = perpendicular_polar_line(a, b, length) if lines_approx_orthogonal(theta, a, b, theta_l, a_l, b_l, theta_m, a_m, b_m, theta_r, a_r, b_r, l_bool, m_bool, r_bool): accepted_lines.append(power_mast_lines[i]) else: other_lines.append(power_mast_lines[i]) if len(accepted_lines) > 1: score = np.zeros(len(accepted_lines)) for i in range(0, len(accepted_lines)): for j in range(0, len(accepted_lines)): if i != j: if is_neighbour_line(accepted_lines[i][1], accepted_lines[i][3], accepted_lines[j][1], accepted_lines[j][3], 5): score[i] += length_of_line(accepted_lines[j][0], accepted_lines[j][1], accepted_lines[j][2], accepted_lines[j][3]) core_line_indx = np.argmax(score) max_score = np.max(score) if max_score > 100: return True, accepted_lines, accepted_lines[core_line_indx] else: return False, accepted_lines, accepted_lines[core_line_indx] else: return False, accepted_lines, accepted_lines
def KMeans_alg(img, power_lines): edges = cv2.Canny(img,350,110) minLineLength = 10 maxLineGap = 8 lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap) polar_lines=[] cartesian_lines=[] lengths = [] max_rho = 0 min_rho= 0 for i in range(0,len(lines)): a,b,length = find_cart_line_eq(lines[i][0][0],lines[i][0][1],lines[i][0][2],lines[i][0][3]) rho, theta, length = perpendicular_polar_line(a,b,length) if rho < min_rho: min_rho = rho if rho > max_rho : max_rho = rho polar_lines.append([rho,theta]) cartesian_lines.append([lines[i][0][0],lines[i][0][1],lines[i][0][2],lines[i][0][3]]) lengths.append(length) scaled_polar_lines = polar_lines for i in range(0,len(scaled_polar_lines)): scaled_polar_lines[i][0] = 180*(scaled_polar_lines[i][0] - min_rho)/(max_rho - min_rho) #scaled_polar_lines[i][0] = 0 scaled_polar_lines[i][1] = scaled_polar_lines[i][1] n_clusters = 3 kmeans = KMeans(n_clusters=3, random_state=0).fit(scaled_polar_lines) clustering = kmeans.predict(scaled_polar_lines) votes = np.zeros(n_clusters) for i in range(0,len(lines)): #img_hough = cv2.line(img,(lines[i][0],lines[i][1]),(lines[i][2],lines[i][3]),(a*255,255,0),2) for x1,y1,x2,y2 in lines[i]: a = clustering[i] if a == 0: votes[0] += lengths[i] elif a == 1: votes[1] +=lengths[i] else: votes[2] += lengths[i] ix = np.argmax(votes) sorted_lines,number_added = sort_lines(cartesian_lines,clustering) a,b,rho,theta = kmeans_voting_scheme(sorted_lines[ix],180, int(number_added[ix])) if lines_approx_parallel(power_lines[ix][3],theta,power_lines[ix][0],power_lines[ix][1],a,b,np.shape(img)[0],True,np.pi/2): #img_lined = draw_result_line(a,b,img_org,255,255,0) return a,b,False else: votes[ix] = 0 ix = np.argmax(votes) a,b,rho,theta = kmeans_voting_scheme(sorted_lines[ix],180, int(number_added[ix])) if lines_approx_parallel(power_lines[ix][3],theta,power_lines[ix][0],power_lines[ix][1],a,b,np.shape(img)[0],True,np.pi/2): return a,b,False #img_lined = draw_result_line(a,b,img_org,255,255,0) else: #img_lined = draw_result_line(a,b,img_org,255,255,0) return 0,0, True