def _calc_gradient_matrix(self):
# TODO: find a better method to calc the gradient
height, width = self.working_image.shape[:2]
grey_image = rgb2grey(self.working_image)
grey_image[self.working_mask == 1] = None
gradient = np.nan_to_num(np.array(np.gradient(grey_image)))
gradient_val = np.sqrt(gradient[0]**2 + gradient[1]**2)
max_gradient = np.zeros([height, width, 2])
front_positions = np.argwhere(self.front == 1)
for point in front_positions:
patch = self._get_patch(point)
patch_y_gradient = self._patch_data(gradient[0], patch)
patch_x_gradient = self._patch_data(gradient[1], patch)
patch_gradient_val = self._patch_data(gradient_val, patch)
patch_max_pos = np.unravel_index(
patch_gradient_val.argmax(),
patch_gradient_val.shape
)
max_gradient[point[0], point[1], 0] = \
patch_y_gradient[patch_max_pos]
max_gradient[point[0], point[1], 1] = \
patch_x_gradient[patch_max_pos]
return max_gradient
评论列表
文章目录