def convert_to_negative(image): new_pixels = [] for channel in image.get_pixels(): result = [] for i in range(len(channel)): result.append(255 - channel[i]) new_pixels.append(result) return Image(new_pixels, image.get_width(), image.get_height(), image.get_mode())
def get_binorized_image(image, division): new_channels = list() for channel in image.get_pixels(): new_channel = [] for pixel in channel: new_channel.append(0 if pixel < division else 255) new_channels.append(new_channel) return Image(new_channels, image.get_width(), image.get_height(), "1")
def convert_to_greyscale(image): if image.get_mode() == "RGB": result = [] for i in range(len(image.get_pixels()[0])): result.append( int(image.get_pixels()[0][i] * 0.3 + image.get_pixels()[1][i] * 0.59 + image.get_pixels()[2][i] * 0.11)) else: result = copy.copy(image.get_pixels()[0]) return Image([result], image.get_width(), image.get_height(), "L")
def main(): sys.setrecursionlimit(1048576) im = Image() im.open("./images/P0001460.jpg") greyscale = ImageHandler.convert_to_greyscale(im) greyscale.save_image("./result/greyscale") div = ImageHandler.devide_classes(greyscale) print("devision: ", div) bin_im = ImageHandler.get_binorized_image(greyscale, div) after_erosion = ImageHandler.image_erosion(bin_im, 2) after_building = ImageHandler.image_building(after_erosion, 2) result_im = ImageHandler.median_filter(after_building) objects = ObjectHandler.find_object(after_building) vectors = [] for i in range(len(objects)): vectors.append((i, objects[i].get_vector())) #normalization normalize_vectors = [] for v in vectors: normalize_vectors.append((v[0], [])) for i in range(len(vectors[0][1])): array = [] for v in vectors: array.append(v[1][i]) min_value = min(array) max_value = max(array) for j in range(len(vectors)): normalize_vectors[j][1].append( (vectors[j][1][i] - min_value) / (max_value - min_value))
def median_filter(image): new_image = [] for channel in image.get_pixels(): result = np.zeros(image.get_width() * image.get_height()) for i in range(1, image.get_width() - 1): for j in range(1, image.get_height() - 1): neighbors = ImageHandler.__get_neighbors( channel, i, j, image.get_width()) neighbors.sort() result[image.get_width() * j + i] = neighbors[int( len(neighbors) / 2)] new_image.append(result) return Image(new_image, image.get_width(), image.get_height(), image.get_mode())
def erosion(image): if len(image.get_pixels()) != 1: return flags_matrix = [[0] * image.get_width() for _ in range(image.get_height())] chanel = copy.copy(image.get_pixels()[0]) for row in range(image.get_height()): for column in range(image.get_width()): if chanel[row * image.get_width() + column] == 255: flag = False if row != 0: if chanel[(row - 1) * image.get_width() + column] == 0: if flags_matrix[row - 1][column] != 1: flag = True if not flag: if row != image.get_height() - 1: if chanel[(row + 1) * image.get_width() + column] == 0: if flags_matrix[row + 1][column] != 1: flag = True if not flag: if column != 0: if chanel[row * image.get_width() + column - 1] == 0: if flags_matrix[row][column - 1] != 1: flag = True if not flag: if column != image.get_width() - 1: if chanel[row * image.get_width() + column + 1] == 0: if flags_matrix[row][column + 1] != 1: flag = True if flag: flags_matrix[row][column] = 1 chanel[row * image.get_width() + column] = 0 return Image([chanel], image.get_width(), image.get_height(), image.get_mode())
def building(image): if len(image.get_pixels()) != 1: return flags_matrix = [[0] * image.get_width() for _ in range(image.get_height())] chanel = copy.copy(image.get_pixels()[0]) for row in range(image.get_height()): for column in range(image.get_width()): if flags_matrix[row][column] == 0: if chanel[row * image.get_width() + column] == 255: if row != 0: if chanel[(row - 1) * image.get_width() + column] == 0: chanel[(row - 1) * image.get_width() + column] = 255 flags_matrix[row - 1][column] = 1 if row != image.get_height() - 1: if chanel[(row + 1) * image.get_width() + column] == 0: chanel[(row + 1) * image.get_width() + column] = 255 flags_matrix[row + 1][column] = 1 if column != 0: if chanel[row * image.get_width() + column - 1] == 0: chanel[row * image.get_width() + column - 1] = 255 flags_matrix[row][column - 1] = 1 if column != image.get_width() - 1: if chanel[row * image.get_width() + column + 1] == 0: chanel[row * image.get_width() + column + 1] = 255 flags_matrix[row][column + 1] = 1 return Image([chanel], image.get_width(), image.get_height(), image.get_mode())