def current_rank(image, angle, distance, top): """Calculate the rank for a specific line""" line_index = ph.pixels_on_line(image, angle * math.pi / 180, distance, top) # calling the help func in order to get the specific pixels i need white_lst = [] temp_dist = 0 current_rank = 0 for pixel in line_index: # creating a renwed list with only white pixels in it if image[pixel[0]][pixel[1]] == WHITE: white_lst.append(pixel) for p in range(len(white_lst) - 1): # here we calculate the distance between every following white # pixels - as long as they are close enough to each other, we keep # adding the small distances to one large distance, of every segment # in separate. when we reach a 'space', we begin new segment d = math.sqrt( math.pow((white_lst[p][0] - white_lst[p + 1][0]), 2) + math.pow(white_lst[p][1] - white_lst[p + 1][1], 2)) if d <= 2: temp_dist += d else: current_rank += math.pow(temp_dist, 2) temp_dist = 0 # this command line is for the end of every line the function is working on current_rank += math.pow(temp_dist, 2) return current_rank
def rating_line(image, angle, distance, top): """ For a given line, giving the rate of this line :param image: image to work on :param angle: angle of the tested line :param distance: distance of the line :param top: true or false :return: the rate (the size of each blank line in the line power 2) """ # Getting the pixels of the line line = ex6_helper.pixels_on_line(image, angle, distance, top) blank_line = [] line_rate = 0 # Getting the first blank pixel in blank_line (to be able to compare in # any cases after that) for i in range(len(line)): if image[line[i][ROW_INDEX]][line[i][COLUMN_INDEX]] == WHITE: blank_line.append(line[i]) break # For each white pixel for index_line in range(len(line)): if image[line[index_line][ROW_INDEX]][ line[index_line][COLUMN_INDEX]] == WHITE: # If it's close in off to the last blank pixel if get_distance( blank_line[LAST_MEMBER_INDEX][ROW_INDEX], blank_line[LAST_MEMBER_INDEX][COLUMN_INDEX], line[index_line][ROW_INDEX], line[index_line][COLUMN_INDEX]) <= 2: # Adding it to the list blank_line.append(line[index_line]) else: # But if the distance is too big, that's mean we ended a # blank line # So, adding the rate of the actual blank segment to line_rate line_rate += (get_distance(blank_line[0][ROW_INDEX], blank_line[0][COLUMN_INDEX], blank_line[len(blank_line) - 1][ ROW_INDEX], blank_line[len(blank_line) - 1][ COLUMN_INDEX])) ** 2 blank_line = [] # Adding the actual pixel for the next segment blank_line.append(line[index_line]) if blank_line: # If something still in blank_line # Adding it's rate to line_rate line_rate += (get_distance(blank_line[0][ROW_INDEX], blank_line[0][COLUMN_INDEX], blank_line[len(blank_line) - 1][ROW_INDEX], blank_line[len(blank_line) - 1][ COLUMN_INDEX])) ** 2 return line_rate
def get_angle(image): pixel = threshold_filter(image) angle_list = [0 for a in range(0, 180)] diagonal = int(image_diagonal(pixel)) for t in range(0, diagonal + 1): for a in range(0, len(angle_list)): if 0 < a < 90: line_top = ex6_helper.pixels_on_line(pixel, math.radians(a), t, True) angle_list[a] += line_pixel_counter(line_top, pixel) line_bottom = ex6_helper.pixels_on_line(pixel, math.radians(a), t, False) angle_list[a] += line_pixel_counter(line_bottom, pixel) else: line = ex6_helper.pixels_on_line(pixel, math.radians(a), t) angle_list[a] += line_pixel_counter(line, pixel) largest_line = angle_list.index(max(angle_list)) return largest_line
def get_angle(image): '''A function that finds the dominate angle ,the function use the "line_grade" function to grade each \ distance for each degree and return the max garde and angle ''' pic = threshold_filter(image) diagonnal = int(pitaguras(len(image), len(image[0]))) degree_to_return = 0 max_digree = 0 for i in range(180): the_rad = math.radians(i) degree_grade = 0 for j in range(0, diagonnal, 1): the_line = HELPER.pixels_on_line(pic, the_rad, j) degree_grade += line_grade(the_line, pic) if 0 < i < 90: the_line = HELPER.pixels_on_line(pic, the_rad, j, False) degree_grade += line_grade(the_line, pic) if degree_grade > max_digree: max_digree = degree_grade degree_to_return = i return degree_to_return
def get_angle(image): """ this function receives a black and white image it finds the line in the image that has the greatest square lengths of white lines, and returns that line angle with the top of the image """ DEGREES = 180 max_distance = get_image_diagonal_size(image) max_distance = math.ceil(max_distance) dominant_angle = 0 rank_of_dominant_angle = 0 for angle in range(DEGREES): angle_in_radians = math.radians(angle) overall_angle_rank = 0 for distance in range(max_distance + 1): line_to_check = ex6_helper.pixels_on_line(image, angle_in_radians, distance) overall_angle_rank += rank_line(image, line_to_check) if 0 < angle < 90: line_to_check = ex6_helper.pixels_on_line(image, angle_in_radians, distance, top=False) overall_angle_rank += rank_line(image, line_to_check) if overall_angle_rank > rank_of_dominant_angle: rank_of_dominant_angle = overall_angle_rank dominant_angle = angle return dominant_angle
def downsample(image, max_diagonal_size): slant = calculate_diagonal(image) while slant > max_diagonal_size: image = downsample_by_3(image) slant = calculate_diagonal(image) if slant > max_diagonal_size: continue else: return image new_image = [[1 for i in range(9)] for j in range(9)] #print(downsample(new_image, 2**0.5)) img = ex6_helper.load_image("test20.jpg") # img2 = detect_edges(img) # result_checker = [] # for i in range(len(img)): # result_checker.append(img[i]+img2[i]) # ex6_helper.show(result_checker) dots = ex6_helper.pixels_on_line(img, 1, 17, False) new_img = [] for _ in range(len(img)): new_img.append([0] * len(img[0])) for dot in dots: new_img[dot[0]][dot[1]] = 255 ex6_helper.show(new_img) print(dots)