def apply_erosion(image, pattern): image = image.copy() pixels = image.load() v_matr = Matrix(image.width, image.height) v_matr.apply(lambda x, y: int(rgb_to_v(pixels[x, y]))) new_v_matr = Matrix(image.width, image.height) for c_y in range(v_matr.height): for c_x in range(v_matr.width): if v_matr.raw[c_y][c_x] == 0: continue neighbours = v_matr.get_neighbours(c_x, c_y, pattern.width, pattern.height) new_v_matr.raw[c_y][c_x] = 1 for (x, y) in neighbours: p_x = pattern.width // 2 + c_x - x p_y = pattern.height // 2 + c_y - y if pattern.raw[p_y][p_x] == 0: continue if v_matr.raw[y][x] == 0: new_v_matr.raw[c_y][c_x] = 0 break for y in range(v_matr.height): for x in range(v_matr.width): pixels[x, y] = v_to_rgb(new_v_matr.raw[y][x]) return image
def otsu(image, levels): image = image.copy() pixels = image.load() height = image.height width = image.width v_matr = Matrix(width, height) v_matr.apply(lambda x, y: rgb_to_v(pixels[x, y])) p_vec = [0 for i in range(levels + 1)] for y in range(height): for x in range(width): p_vec[math.floor(v_matr.raw[y][x] * levels)] += 1 n_vec = [] n_t = 0 mu_t = 0 for i in range(levels): if p_vec[i] == 0: continue n_t += p_vec[i] n = p_vec[i] / (width * height) n_vec.append(n) mu_t += (i+1) * n levels = n_vec.__len__() sigma_vec = [0 for i in range(levels)] for t in range(1, levels): w1_t = 0 for i in range(t): w1_t += n_vec[i] w2_t = 1 - w1_t mu1_t = 0 for i in range(t): mu1_t += (i+1) * n_vec[i] mu1_t /= w1_t mu2_t = (mu_t - mu1_t * w1_t) / w2_t sigma_vec[t] = w1_t * w2_t * (mu1_t - mu2_t) ** 2 max_t = None max_sigma = 0 for t in range(levels): if max_t is None or sigma_vec[t] > max_sigma: max_t = t threshold = t / levels for y in range(height): for x in range(width): pixels[x, y] = v_to_rgb( int(1) if v_matr.raw[y][x] < threshold else int(0)) return image
def sobel_vectors(image): pixels = image.copy().load() x_max = image.width y_max = image.height vectors = Matrix(x_max, y_max) v_matr = Matrix(x_max, y_max) v_matr.apply(lambda x, y: rgb_to_v(pixels[x, y])) for y in range(y_max): for x in range(x_max): vectors.raw[y][x] = filter_vector(v_matr, x, y) return vectors
def init_filter(gamma, lam, theta, phi, sigma): # size_x = int(6 * sigma) # size_x = size_x - size_x // 2 + 1 # size_y = int(6 * sigma / gamma) # size_y = size_y - size_y % 2 + 1 size_x = 3 size_y = 3 center_x = size_x // 2 center_y = size_x // 2 filter = Matrix(size_x, size_y) filter.apply(lambda x, y: gabor_filter_func(x - center_x, y - center_y, gamma, lam, theta, phi, sigma)) return filter
def feedforword(self, inputs): if len(inputs) == self.sizes[0]: output = Matrix(1, len(inputs), [inputs]).transpose() for i in range(len(self.sizes) - 1): output = self.weights[i] * output + self.biases[i] output = output.apply(sigmoid) return output raise Exception("Invalid input length")
def gabor(image, gamma, lam, theta, phi, sigma): image = image.copy() pixels = image.load() height = image.height width = image.width v_matr = Matrix(width, height) v_matr.apply(lambda x, y: rgb_to_v(pixels[x, y])) filter = init_filter(gamma, lam, theta, phi, sigma) v_matr_filtered = Matrix(width, height) v_matr_filtered.apply(lambda x, y: apply_filter(v_matr, filter, x, y)) for y in range(height): for x in range(width): pixels[x, y] = hsv_to_rgb((0, 0, v_matr_filtered.raw[y][x])) return image
def dispatch_colors(image): image = image.copy() pixels = image.load() v_matr = Matrix(image.width, image.height) v_matr.apply(lambda x, y: int(rgb_to_v(pixels[x, y]))) colors = Matrix(v_matr.width, v_matr.height) curr_color = 1 for y in range(colors.height): for x in range(colors.width): if (v_matr.raw[y][x] == 0): continue neighbours = ((x - 1, y - 1), (x, y - 1), (x + 1, y - 1), (x - 1, y)) color = None for (n_x, n_y) in neighbours: neighbour_color = colors.get(n_x, n_y) if neighbour_color is not None and neighbour_color != 0: color = neighbour_color break if color is None: colors.raw[y][x] = curr_color curr_color += 1 else: colors.raw[y][x] = color color_mapping = [cl_n for cl_n in range(curr_color)] def get_mapping(cl): while color_mapping[cl] != cl: cl = color_mapping[cl] return cl for y in range(colors.height): for x in range(colors.width): if (v_matr.raw[y][x] == 0): continue neighbours = colors.get_neighbours(x, y) min_color = get_mapping(colors.raw[y][x]) for (n_x, n_y) in neighbours: if v_matr.raw[n_y][n_x] != 0: min_color = min(get_mapping(colors.raw[n_y][n_x]), min_color) neighbours.append((x, y)) for (n_x, n_y) in neighbours: if v_matr.raw[n_y][n_x] != 0: color_mapping[get_mapping( colors.raw[n_y][n_x])] = get_mapping(min_color) changed = 0 for cl in range(curr_color): if color_mapping[cl] != cl: changed += 1 print(color_mapping.__len__()) print(changed) color_scale_coef = 359 / color_mapping.__len__() - changed for y in range(image.height): for x in range(image.width): color = get_mapping(colors.raw[y][x]) * color_scale_coef pixels[x, y] = hsv_to_rgb((color, 1, v_matr.raw[y][x])) return image