def define_lines(image): mask_to_use = "prewittdg" gradients = edge.find_edges(image, mask_to_use, False) edge_image = edge.define_edges(image, mask_to_use, False) xs, ys = edge_image.size central_point = (ys / 2.0), (xs / 2.0) angles = gradients_angles.define_angles(gradients, 1) counter_angles = structures.dict_to_list(angles, False, False) average_counter = statistics.average(counter_angles) median_counter = statistics.median(counter_angles) new_angles = define_line_angles(angles, average_counter) lines_gradients = define_lines_gradients(gradients, new_angles) image = define_equation_line(edge_image, central_point, lines_gradients, new_angles) xs, ys = image.size pixels = image.load() colors_list = pix.colors_image(image) white_color, black_color = (255, 255, 255), (0, 0, 0) visited_pixels = {} image.show() for y in xrange(ys): for x in xrange(xs): pixel = (y, x) if pixel not in visited_pixels: pixel_value = pixels[x, y] if pixel_value != white_color: newcolor = colors.color_generator(colors_list) colors_list.append(newcolor) visited_pixels[(y, x)] = 1 queue_neighbors = list() queue_neighbors.append(pixel) while len(queue_neighbors) > 0: bfs.bfs(image, queue_neighbors, pixel_value, newcolor, visited_pixels) return image
def voting_process(voting_pixels_dict, pixels, ys, xs): change_number_possible = True axis_limits = (ys, xs) while change_number_possible: number_before = len(voting_pixels_dict) frec_voting_pixels = structures.dict_to_list(voting_pixels_dict, False, False) average_voting_pixels = statistics.average(frec_voting_pixels) possible_centers_list = possible_centers(voting_pixels_dict, average_voting_pixels) for center in possible_centers_list: center_value = voting_pixels_dict[center] neighbor_pixels = neighbors.find_neighbors(pixels, center, None, axis_limits) for neighbor in neighbor_pixels: pixel_neighbor = neighbor[0] if pixel_neighbor in voting_pixels_dict: if pixel_neighbor != center: neighbor_value = voting_pixels_dict[pixel_neighbor] if neighbor_value < center_value: voting_pixels_dict[center] += voting_pixels_dict[ pixel_neighbor] remove_not_possible_centers(voting_pixels_dict, possible_centers_list) number_after = len(voting_pixels_dict) if number_after == number_before: change_number_possible = False print voting_pixels_dict champion = random.choice(voting_pixels_dict.keys()) return champion
def define_equation_line(image, central_point, gradients, angles): black_color, white_color = (0, 0, 0), (255, 255, 255) equation_line = {} pixels = image.load() for gradient in gradients: #gradient -> y, x oldy, oldx = gradient angle = gradients[gradient][1] y, x = oldy - central_point[0], oldx - central_point[1] rho = abs(int(x * math.cos(angle) + y * math.sin(angle))) aux = angle, rho if aux not in equation_line: equation_line[aux] = 1 else: equation_line[aux] += 1 pixels[oldx, oldy] = angles[angle] if pixels[oldx, oldy] == black_color: pixels[oldx, oldy] = white_color print equation_line suma = 0 miau = [] for i in equation_line: miau.append(equation_line[i]) suma += equation_line[i] print suma print miau, "hola" prom = statistics.average(miau) print prom nuevos = [] for i in equation_line: if equation_line[i] >= prom: if i not in nuevos: nuevos.append(i) print nuevos print len(nuevos) for i in nuevos: print i parameters = 5, 5 image.show() white_black = ( True, True ) #ignore white and black pixels in the neighborhood, only update white and black pixels image = pix.filter_pixels(image, white_black, True, "mode", parameters) image.show() white_black = (True, False) image = pix.filter_pixels(image, white_black, True, "mode", parameters) image.show() white_black = (True, True) image = pix.filter_pixels(image, white_black, True, "mode", parameters) image.show() image = pix.enhance_pixels(image, True) image.show() return image
def find_possible_holes(histogram, bool_plane): #the developer must be aware of the plane of the histogram possible_holes = [] for main_pos in histogram: histo = histogram[main_pos] average = statistics.average(histo) deviation = statistics.standard_deviation(histo) threshold = deviation * 3 for other_pos in xrange(len(histo)): y, x = 0, 0 value = histo[other_pos] if value < threshold: if bool_plane: y = main_pos x = other_pos else: y = other_pos x = main_pos possible_holes.append((y, x)) return possible_holes
def check_size_holes(shapes_info): new_shapes_Info = {} list_of_sizes = [] print list_of_sizes shapes_to_delete = [] for color in shapes_info: size_shape = shapes_info[color][2] if size_shape not in list_of_sizes: list_of_sizes.append(size_shape) average_size = statistics.average(list_of_sizes) deviation_size = statistics.standard_deviation(list_of_sizes) threshold = int(average_size - (deviation_size)) for color in shapes_info: size_shape = size_shape = shapes_info[color][2] if size_shape < threshold: shapes_to_delete.append(color) for shape_color in shapes_to_delete: if shape_color in shapes_info: del shapes_info[shape_color] return None
def find_edges(image, mask_to_use, bool_normalize): gradients = find_gradients(image, mask_to_use, bool_normalize) white_color, black_color = (255, 255, 255), (0, 0, 0) xs, ys = image.size #adaptative_threshold(ys, xs, gradients) gradients_values = [] for gradient_key in gradients: gradient_info = gradients[gradient_key] gradient_value = gradient_info[0] gradients_values.append(gradient_value) average_gradients = statistics.average(gradients_values) deviation_gradients = statistics.standard_deviation(gradients_values) / 2.0 mode = statistics.mode(gradients_values) threshold = average_gradients edge_gradients = {} for gradient_key in gradients: gradient_info = gradients[gradient_key] gradient_value = gradient_info[0] gradient_angle = gradient_info[1] if gradient_value >= threshold: edge_gradients[gradient_key] = gradient_info return edge_gradients
def draw_circle(center, pixels, ys, xs, image): white, red = (255, 255, 255), (255, 0, 0) yc, xc = center pixels[xc, yc] = (255, 0, 255) point_a = () point_b = () point_c = () point_d = () for y in xrange(yc, 0, -1): if pixels[xc, y] == white: point_a = (y + 1, xc) #pix.draw_point(point_a, pixels) break for x in xrange(xc, xs): if pixels[x, yc] == white: point_b = (yc, x - 1) #pix.draw_point(point_b, pixels) break for y in xrange(yc, ys): if pixels[xc, y] == white: point_c = (y - 1, xc) #pix.draw_point(point_c, pixels) break for x in xrange(xc, 0, - 1): if pixels[x, yc] == white: point_d = (yc, x + 1) #pix.draw_point(point_d, pixels) break distances = [] if point_a is not None: distance = statistics.euclidean_distance(center, point_a) distances.append(distance) if point_b is not None: distance = statistics.euclidean_distance(center, point_b) distances.append(distance) if point_c is not None: distance = statistics.euclidean_distance(center, point_c) distances.append(distance) if point_d is not None: distance = statistics.euclidean_distance(center, point_d) distances.append(distance) radius = statistics.average(distances) limit = math.pow(radius, 2) x = (limit * (-1.0)) while x <= limit: #x = pix.getXCoordinate(xpi, xc * 2.0) xpi = pix.getXPixel(x, xc * 2.0) aux1 = math.pow(radius, 2) aux2 = math.pow(x, 2) aux3 = aux1 - aux2 if aux3 >= 0.0: y1 = math.sqrt(aux3) y2 = (-1.0) * y1 ypix1 = pix.getYPixel(y1, yc * 2.0) ypix2 = pix.getYPixel(y2, yc * 2.0) if ypix1 >= 0.0 and ypix1 < ys and xpi >= 0 and xpi < xs: pixels[xpi, ypix1] = red if ypix2 >= 0.0 and ypix2 < ys and xpi >= 0 and xpi < xs: pixels[xpi, ypix2] = red x += 0.01 return None