def getneighnumslist( coords: tuple, kernelsize: int = 3 ) -> list: """ Gets list with…
- User
def get_neigh_nums_list( coords: tuple, kernel_size: int = 3 ) -> list: """ Gets list with number of neighborhoods by kernel. Returns list like [[x1, y1, N1], [x2, y2, N2]], Where N - neighborhood's number. """ neigh_nums = [] offset = (kernel_size - 1) // 2
for x, y in zip(coords[0], coords[1]): if x < 0 + offset or x > image.shape[0] - offset: continue if y < 0 + offset or y > image.shape[1] - offset: continue kernel = image[x-offset:x+offset+1, y-offset:y+offset+1] step_neigh_num = np.count_nonzero(kernel) - 1 neigh_nums.append([x, y, step_neigh_num]) return neigh_numsdef get_neigh_coeffs_list( coords: tuple, kernel_array: np.array ) -> list: """ Gets list with number of neighborhoods by kernel. Returns list like [[x1, y1, N1], [x2, y2, N2]], Where N - neighborhood's coeffs. """ neigh_nums = []
offset = (kernel_array.shape[0] - 1) // 2 for x, y in zip(coords[0], coords[1]): if x < 0 + offset or x > image.shape[0] - offset: continue if y < 0 + offset or y > image.shape[1] - offset: continue kernel = image[x-offset:x+offset+1, y-offset:y+offset+1] step_neigh_num = np.count_nonzero(kernel) - 1 neigh_nums.append([x, y, step_neigh_num]) return neigh_numsПерепиши метод get_neigh_coeffs_list(), чтобы он заносил в результат не количество соседей, а сумму произведения интенсивности пикселей умноженную на коэффицент матрицы kernel_array kernel_array = np.array([[-1, -1, -1, -1, -1], [-1, 4, 4, 4, -1], [-1, 4, 0, 4, -1], [-1, 4, 4, 4, -1], [-1, -1, -1, -1, -1]])
- Assistant
import numpy as np
def get_neigh_nums_list(coords: tuple, kernel_size: int = 3) -> list: """ Gets list with number of neighborhoods by kernel. Returns list like [[x1, y1, N1], [x2, y2, N2]], Where N - neighborhood's number. """ neigh_nums = [] offset = (kernel_size - 1) // 2
for x, y in zip(coords[0], coords[1]): if x < 0 + offset or x > image.shape[0] - offset: continue if y < 0 + offset or y > image.shape[1] - offset: continue kernel = image[x-offset:x+offset+1, y-offset:y+offset+1] step_neigh_num = np.count_nonzero(kernel) - 1 neigh_nums.append([x, y, step_neigh_num]) return neigh_numsdef get_neigh_coeffs_list(coords: tuple, kernel_array: np.array) -> list: """ Gets list with number of neighborhoods by kernel. Returns list like [[x1, y1, N1], [x2, y2, N2]], Where N - neighborhood's coeffs. """ neigh_nums = []
offset = (kernel_array.shape[0] - 1) // 2 for x, y in zip(coords[0], coords[1]): if x < 0 + offset or x > image.shape[0] - offset: continue if y < 0 + offset or y > image.shape[1] - offset: continue kernel = image[x-offset:x+offset+1, y-offset:y+offset+1] step_neigh_sum = np.sum(kernel * kernel_array) neigh_nums.append([x, y, step_neigh_sum]) return neigh_nums - User
где табуляция?!
- Assistant
Извините за ошибку. Вот исправленный код с правильной табуляцией:
import numpy as np
def get_neigh_nums_list(coords: tuple, kernel_size: int = 3) -> list: """ Gets list with number of neighborhoods by kernel. Returns list like [[x1, y1, N1], [x2, y2, N2]], Where N - neighborhood's number. """ neigh_nums = [] offset = (kernel_size - 1) // 2
for x, y in zip(coords[0], coords[1]): if x < 0 + offset or x > image.shape[0] - offset: continue if y < 0 + offset or y > image.shape[1] - offset: continue kernel = image[x-offset:x+offset+1, y-offset:y+offset+1] step_neigh_num = np.count_nonzero(kernel) - 1 neigh_nums.append([x, y, step_neigh_num]) return neigh_numsdef get_neigh_coeffs_list(coords: tuple, kernel_array: np.array) -> list: """ Gets list with number of neighborhoods by kernel. Returns list like [[x1, y1, N1], [x2, y2, N2]], Where N - neighborhood's coeffs. """ neigh_nums = []
offset = (kernel_array.shape[0] - 1) // 2 for x, y in zip(coords[0], coords[1]): if x < 0 + offset or x > image.shape[0] - offset: continue if y < 0 + offset or y > image.shape[1] - offset: continue kernel = image[x-offset:x+offset+1, y-offset:y+offset+1] step_neigh_sum = np.sum(kernel * kernel_array) neigh_nums.append([x, y, step_neigh_sum]) return neigh_nums