def dual_gradient_energy(img): R = img[:, :, 0] G = img[:, :, 1] B = img[:, :, 2] w, h = img.shape[:2] ibh = filters.sobel_h(B) ibv = filters.sobel_v(B) irh = filters.sobel_h(R) irv = filters.sobel_v(R) igh = filters.sobel_h(G) igv = filters.sobel_v(G) energy = np.zeros((w, h)) for i in range(0, w): for j in range(0, h): energy[i][j] = (irh[i][j] * irh[i][j] + igh[i][j] * igh[i][j] + ibh[i][j] * ibh[i][j] + irv[i][j] * irv[i][j] + igv[i][j] * igv[i][j] + ibv[i][j] * ibv[i][j]) gray() imshow(energy) title("Energy of image") show() for i in range(0, w): energy[i][0] = energy[i][1] energy[i][h-1] = energy[i][h-2] for i in range(0, h): energy[0][i] = energy[2][i] energy[w-1][i] = energy[w-2][i] return energy pass
def dual_gradient_energy(img): """ calculates the gradient energy of each pixel of an image :param img: the image for which the gradient energy is to be calculated :return: the gradient energy of each pixel of the image. >>> img_test = np.array([[[ 0.77254903, 0.72941178, 0.79215688]]]) >>> dual_gradient_energy(img_test) array([[ 0.]]) """ r = img[:, :, 0] g = img[:, :, 1] b = img[:, :, 2] r_h = filters.sobel_h(r) g_h = filters.sobel_h(g) b_h = filters.sobel_h(b) r_v = filters.sobel_v(r) g_v = filters.sobel_v(g) b_v = filters.sobel_v(b) sum_rg_h = np.add(np.square(r_h), np.square(g_h)) sum_h = np.add(sum_rg_h, np.square(b_h)) sum_rg_v = np.add(np.square(r_v), np.square(g_v)) sum_v = np.add(sum_rg_v, np.square(b_v)) energy = np.add(sum_h, sum_v) return energy
def dual_gradient_energy(img): h, w = img.shape[:2] img = img_as_float(img) R = img[:, :, 0] G = img[:, :, 1] B = img[:, :, 2] rh_gradient = filters.sobel_h(R) gh_gradient = filters.sobel_h(G) bh_gradient = filters.sobel_h(B) rv_gradient = filters.sobel_v(R) gv_gradient = filters.sobel_v(G) bv_gradient = filters.sobel_v(B) # Calculating the energy Matrix energy = (rh_gradient*rh_gradient) + (gh_gradient*gh_gradient) +\ (bh_gradient*bh_gradient) + (rv_gradient*rv_gradient) +\ (gv_gradient*gv_gradient) + (bv_gradient*bv_gradient) # Copying the second column to first and second last column to last # as first and last column return 0 due to sobel for i in range(h): for j in range(w): if j == 0: energy[i][j] = energy[i][j+1] if j == w-1: energy[i][j] = energy[i][j-1] return energy
def dual_gradient_energy(img): """ Dual gradient energy is the sum of the square of a horizontal gradient and a vertical gradient. Use skimage.filter.hsobel and vsobel to calculate the gradients of each channel independently. The energy is the sum of the square the horizontal and vertical gradients over all channels. :param img: input image :return: dual gradient energy of input image """ red_channel = img[:, :, 0] # red channel of the image green_channel = img[:, :, 1] # green channel of the image blue_channel = img[:, :, 2] # blue channel of the image horizontal_gradient_red = filters.sobel_h(red_channel) # horizontal gradient of the red channel vertical_gradient_red = filters.sobel_v(red_channel) # vertical gradient of the red channel horizontal_gradient_green = filters.sobel_h(green_channel) # horizontal gradient of the green channel vertical_gradient_green = filters.sobel_v(green_channel) # vertical gradient of the green channel horizontal_gradient_blue = filters.sobel_h(blue_channel) # horizontal gradient of the blue channel vertical_gradient_blue = filters.sobel_v(blue_channel) # vertical gradient of the blue channel # dual gradient energy at each pixel energy = (horizontal_gradient_red * horizontal_gradient_red)\ + (vertical_gradient_red * vertical_gradient_red)\ + (horizontal_gradient_green * horizontal_gradient_green)\ + (vertical_gradient_green * vertical_gradient_green)\ + (horizontal_gradient_blue * horizontal_gradient_blue)\ + (vertical_gradient_blue * vertical_gradient_blue) return energy
def dual_gradient_energy(img1): w, h = img1.shape[:2] R = img1[:, :, 0] G = img1[:, :, 1] B = img1[:, :, 2] A = sobel_h(R) ** 2 + sobel_v(R) ** 2 + sobel_h(G) ** 2 + sobel_v(G) ** 2 + sobel_h(B) ** 2 + sobel_v(B) ** 2 r = len(range(len(A))) c = len(range(len(A[0]))) for i in range(len(A[0])): A[0][i] = A[1][i] A[r - 1][i] = A[r - 2][i] for i in range(len(A)): A[i][0] = A[i][1] A[i][c - 1] = A[i][c - 2] return A
def detect_edges(self, filename=None): """Edge filter an image using the Canny algorithm.""" if filename is None: filename = './output/phough_transform' low = self.canny_threshold[0] * (self.img.max() - self.img.min()) high = self.canny_threshold[1] * (self.img.max() - self.img.min()) if self.canny_edges == 'horizontal': print('Running One-Way Horizontal Edge Detector') magnitude = sobel_h(self.img).clip(min=0) elif self.canny_edges == 'vertical': print('Running One-Way Vertical Edge Detector') magnitude = sobel_v(self.img).clip(min=0) else: print('Running One-Way Multidirectional Edge Detector') magnitude = sobel(self.img).clip(min=0) self.edges = apply_hysteresis_threshold(magnitude, low, high) if self.show_figures: io.imshow(self.edges) plt.show(block=False) if self.save_figures: io.imsave(filename + '.tif', util.img_as_ubyte(self.edges))
def harris_corners(img, window_size=3, k=0.04): """ Compute Harris corner response map. Follow the math equation R=Det(M)-k(Trace(M)^2). Hint: You may use the function scipy.ndimage.filters.convolve, which is already imported above. If you use convolve(), remember to specify zero-padding to match our equations, for example: out_image = convolve(in_image, kernel, mode='constant', cval=0) Args: img: Grayscale image of shape (H, W) window_size: size of the window function k: sensitivity parameter Returns: response: Harris response image of shape (H, W) """ H, W = img.shape window = np.ones((window_size, window_size)) response = np.zeros((H, W)) # 1. Compute x and y derivatives (I_x, I_y) of an image dx = filters.sobel_v(img) dy = filters.sobel_h(img) ### YOUR CODE HERE pass ### END YOUR CODE return response
def harris_corners(img, window_size=3, k=0.04): """ Compute Harris corner response map. Following the equation R=Det(M)-k(Trace(M)^2). Args: img: Grayscale image of shape (H, W) window_size: size of the window function k: sensitivity parameter Returns: response: Harris response image of shape (H, W) """ window = np.ones((window_size, window_size)) dx = filters.sobel_v(img) dy = filters.sobel_h(img) # Calculate the elements in the matrix in the formula for M ixx = dx**2 ixy = dx * dy iyy = dy**2 # Calculate the sum using convolution sxx = convolve(ixx, window) sxy = convolve(ixy, window) syy = convolve(iyy, window) # Calculate determinant and trace det = (sxx * syy) - (sxy**2) trace = sxx + syy response = det - k * (trace**2) return response
def define_threshold(imagem): sobel_vertical = sobel_v(imagem) sobel_horizontal = sobel_h(imagem) h, c = imagem.shape return np.sum(np.abs(sobel_vertical + sobel_horizontal)) / (h * c)
def g(img): """ Computes gradient magnitude at a distance of n """ g_h = sobel_h(img) g_v = sobel_v(img) return g_v, g_h
def harris_corners(img, window_size=3, k=0.04): """ Compute Harris corner response map. Follow the math equation R=Det(M)-k(Trace(M)^2). Hint: You may use the function scipy.ndimage.filters.convolve, which is already imported above Args: img: Grayscale image of shape (H, W) window_size: size of the window function k: sensitivity parameter,usually between [0.04,0.06] Returns: response: Harris response image of shape (H, W) """ H, W = img.shape window = np.ones((window_size, window_size)) response = np.zeros((H, W)) dx = filters.sobel_v(img) #使用核为[[1,0,-1],[2,0,-2],[1,0,-1]]滤波 dy = filters.sobel_h(img) #使用核为[[1,2,1],[0,0,0],[-1,-2,-1]]滤波 ### YOUR CODE HERE A = convolve(dx**2, window) B = convolve(dx * dy, window) C = convolve(dy**2, window) for i in range(H): for j in range(W): M = np.array([[A[i, j], B[i, j]], [B[i, j], C[i, j]]]) response[i, j] = np.linalg.det(M) - k * np.trace(M)**2 ### END YOUR CODE return response
def calc_bkgfluctuation(filename, scale=0.003): from PIL import Image from skimage import filters bkg_fn = filename.split(".")[0] + "_bkg.fits" bkg_img = getdata(bkg_fn, 0) bkg_img = np.array(bkg_img).byteswap().newbyteorder() bkg_image = Image.fromarray(bkg_img) factor = scale width = int(bkg_image.size[0] * factor) height = int(bkg_image.size[1] * factor) bkg_image = bkg_image.resize((width, height), Image.ANTIALIAS) # best down-sizing filter bkg_image = np.array(bkg_image) edges_x = filters.sobel_h(bkg_image) edges_y = filters.sobel_v(bkg_image) # sum of gradient over entire background (log) grad = np.log10( np.sqrt(np.sum(edges_x)**2 + np.sum(edges_y)**2) / bkg_image.size) # sum of residual of subtraction between the background and the median level of the background (log) residual = np.log10( np.sum((bkg_image - np.median(bkg_image))**2) / bkg_image.size) return grad, residual
def harris_corners(img, window_size=3, k=0.04): """ Compute Harris corner response map. Follow the math equation R=Det(M)-k(Trace(M)^2). Args: img: Grayscale image of shape (H, W) window_size: size of the window function k: sensitivity parameter Returns: response: Harris response image of shape (H, W) """ H, W = img.shape window = np.ones((window_size, window_size)) response = np.zeros((H, W)) dx = filters.sobel_v(img) dy = filters.sobel_h(img) Sx2 = scipy.ndimage.filters.convolve(np.square(dx), window) Sxy = scipy.ndimage.filters.convolve(np.multiply(dx, dy), window) Sy2 = scipy.ndimage.filters.convolve(np.square(dy), window) response = np.multiply(Sx2, Sy2) - np.square(Sxy) - k * np.square(Sx2 + Sy2) return response
def y_coordinate_extractor(img_arr): middle_y_coor = 0.5 * img_arr.shape[0] image = rgb2gray(img_arr) image = np.asarray(image > 0.5, dtype=np.float32) edges = filters.sobel_h(image) h, theta, d = hough_line(edges) upper_y = img_arr.shape[0] lower_y = 0 thres_upp = img_arr.shape[0] thres_low = img_arr.shape[0] for _, angle, dist in zip(*hough_line_peaks(h, theta, d)): y0 = (dist - 0 * np.cos(angle)) / np.sin(angle) y1 = (dist - image.shape[1] * np.cos(angle)) / np.sin(angle) avg = (y0 + y1) / 2 if avg - middle_y_coor < thres_low and avg - middle_y_coor > 0: lower_y = avg thres_low = avg - middle_y_coor if middle_y_coor - avg < thres_upp and middle_y_coor - avg > 0: upper_y = avg thres_upp = middle_y_coor - avg return upper_y, lower_y
def image_features(images, block_size, orientations): N, R, C = images.shape thetas = np.linspace(0, np.pi, orientations, endpoint=False) edge_x = np.empty_like(images) edge_y = np.empty_like(images) for idx, image in enumerate(images): edge_x[idx] = sobel_h(image) edge_y[idx] = sobel_v(image) Cb = C // block_size if (C % block_size): Cb += 1 Rb = R // block_size if (C % block_size): Rb += 1 #print("CB,RB",Cb,Rb) block_features = np.empty((N, Cb, Rb, orientations)) for orientation, theta in enumerate(thetas): v_x = np.cos(theta) v_y = np.sin(theta) edges = edge_x * v_x + edge_y * v_y #print("edges",edges.shape) feature = np.maximum(edges, 0) block = (1, block_size, block_size) block_feature = block_reduce(feature, block, np.mean) #print("blocks",block_feature.shape) block_features[:, :, :, orientation] = block_feature return block_features.reshape(N, -1) print(block_features.shape) #print("block_features",block_features.shape) return block_features.reshape(len(images), -1)
def harris_corners(img, window_size=3, k=0.04): """ Compute Harris corner response map. Follow the math equation R=Det(M)-k(Trace(M)^2). Hint: You may use the function scipy.ndimage.filters.convolve, which is already imported above. Args: img: Grayscale image of shape (H, W) window_size: size of the window function k: sensitivity parameter Returns: response: Harris response image of shape (H, W) """ H, W = img.shape window = np.ones((window_size, window_size)) response = np.zeros((H, W)) dx = filters.sobel_v(img) dy = filters.sobel_h(img) ### YOUR CODE HERE pass ### END YOUR CODE return response
def project_h_edge(img, gauss_sigma=1, print_img=False, total_blocks=-1): """ Project horizontal edges vertically by processing an h_sobel operation followed by gaussian blur :param img: input gray image (0~255) :param gauss_sigma: sigma in gaussian distribution w.r.t. blurness :param print_img: print debug images :param total_blocks: number of blocks or intervals in image spreading vertically :return: histogram normalized to 0~1 and length is image height """ img_height, img_width = img.shape[0], img.shape[1] img = img_as_float(img) blurred = filters.gaussian(img, gauss_sigma) edge_map = filters.sobel_h(blurred) if total_blocks is -1: total_blocks = img_height if print_img is True: dump_images(img, blurred, edge_map) hist_len = min(total_blocks, img_height) ratio = (hist_len - 1) / (img_height - 1) hist = [0] * hist_len peak_val = 0 for i in range(img_height): hist_index = round(i * ratio) for j in range(img_width): hist[hist_index] += abs(edge_map[i][j]) if hist[hist_index] > peak_val: peak_val = hist[hist_index] for i in range(hist_len): hist[i] /= peak_val return hist
def hog_descriptor(patch, pixels_per_cell=(8, 8)): """ Generating hog descriptor by the following steps: 1. Compute the gradient image in x and y directions (already done for you) 2. Compute gradient histograms for each cell 3. Flatten block of histograms into a 1D feature vector Here, we treat the entire patch of histograms as our block 4. Normalize flattened block by L2 norm Normalization makes the descriptor more robust to lighting variations Args: patch: grayscale image patch of shape (H, W) pixels_per_cell: size of a cell with shape (M, N) Returns: block: 1D patch descriptor array of shape ((H*W*n_bins)/(M*N)) """ assert (patch.shape[0] % pixels_per_cell[0] == 0),\ 'Heights of patch and cell do not match' assert (patch.shape[1] % pixels_per_cell[1] == 0),\ 'Widths of patch and cell do not match' n_bins = 9 degrees_per_bin = 180 // n_bins Gx = filters.sobel_v(patch) Gy = filters.sobel_h(patch) # Unsigned gradients G = np.sqrt(Gx**2 + Gy**2) theta = (np.arctan2(Gy, Gx) * 180 / np.pi) % 180 # Group entries of G and theta into cells of shape pixels_per_cell, (M, N) # G_cells.shape = theta_cells.shape = (H//M, W//N) # G_cells[0, 0].shape = theta_cells[0, 0].shape = (M, N) G_cells = view_as_blocks(G, block_shape=pixels_per_cell) theta_cells = view_as_blocks(theta, block_shape=pixels_per_cell) rows = G_cells.shape[0] cols = G_cells.shape[1] # For each cell, keep track of gradient histrogram of size n_bins cells = np.zeros((rows, cols, n_bins)) # Compute histogram per cell ### YOUR CODE HERE # print(theta_cells.max()) for i in range(rows): for j in range(cols): theta_block = theta_cells[i][j].reshape(-1) G_block = G_cells[i][j].reshape(-1) for k in range(theta_block.shape[0]): # print(theta_block[k] // degrees_per_bin) cells[i][j][(int(theta_block[k] // degrees_per_bin) % 9)] += G_block[k] block = cells.reshape(-1) block = block / np.sqrt(np.dot(block.T, block)) ### YOUR CODE HERE return block
def harris_corners(img, window_size=3, k=0.04): H, W = img.shape window = np.ones((window_size, window_size)) response = np.zeros((H, W)) #第一步: 偏导数 dx = filters.sobel_v(img) dy = filters.sobel_h(img) # 第二步: 偏导数乘积 dxx = dx * dx dyy = dy * dy dxy = dx * dy # 第三步: 形成矩阵 mxx = convolve(dxx, window) mxy = convolve(dxy, window) myy = convolve(dyy, window) #加权计算 # 第四步: 计算response for i in range(H): for j in range(W): M = np.array([[mxx[i, j], mxy[i, j]], [mxy[i, j], myy[i, j]]]) response[i, j] = np.linalg.det(M) - k * np.trace(M)**2 return response
def generate_lighting_effect(img, light_pos, stroke): h, w, _ = img.shape # 高斯模糊 + 归一化 n = [img[:, :, 0], img[:, :, 1], img[:, :, 2]] for i in range(3): n[i] = filters.gaussian(n[i], sigma=21) n[i] = (n[i] - np.min(n[i])) / (np.max(n[i]) - np.min(n[i])) # 计算图像上每个位置的光源方向 coords = np.zeros(img.shape) coords[:, :, 0] = np.arange(h * w).reshape((h, w)) % w coords[:, :, 1] = np.arange(h * w).reshape((h, w)) // w light_dir = light_pos - coords light_dir /= np.sqrt(np.sum(light_dir**2, axis=2, keepdims=True)) # 生成光效 e = [] for i in range(3): dx = filters.sobel_v(n[i]) dy = filters.sobel_h(n[i]) normal = np.stack([-dx, -dy, np.ones((h, w)) * 0.2], axis=2) normal /= np.sqrt(np.sum(normal**2, axis=2, keepdims=True)) e.append(np.sum(normal * light_dir, axis=2).clip(0, 1) * stroke) return np.stack(e, axis=2)
def TENG(im): # Tenengrad (Krotkov86) gx = sobel_v(im) gy = sobel_h(im) fm = gx**2 + gy**2 fm = fm.mean() return fm
def my_features(img): """ Implement your own features Args: img - array of shape (H, W, C) Returns: features - array of (H * W, C) """ from skimage.filters import sobel_h, sobel_v from skimage.color import rgb2gray img = rgb2gray(img) H, W = np.shape(img) features = np.zeros((H, W, 3)) ### YOUR CODE HERE X, Y = sobel_h(img), sobel_v(img) features[:, :, 0] = X features[:, :, 1] = Y features[:, :, 2] = X**2 + Y**2 for i in range(3): mean, std = np.mean(features[:, :, i]), np.std(features[:, :, i]) features[:, :, i] = (features[:, :, i] - mean) / std features = features.reshape((H * W, 3)) ### END YOUR CODE return features
def extract_hog(img): image = 0.299 * img[:, :, 0] + 0.587 * img[:, :, 1] + 0.114 * img[:, :, 2] image = resize(image, (64, 64), mode='reflect') sobelx = sobel_v(image) sobely = sobel_h(image) modul_grad = (sobelx**2 + sobely**2)**(1 / 2) way_grad = abs(arctan2(sobely, sobelx)) gistogram_places = zeros((8, 8, 9)) for x in range(8): for y in range(8): for i in range(8): for j in range(8): pixelx = 8 * x + i pixely = 8 * y + j gistogram_places[x, y, way(way_grad[ pixelx, pixely])] += modul_grad[pixelx, pixely] eps = 0.0000000001 for x in range(7): for y in range(7): v = gistogram_places[x, y] v = append(v, gistogram_places[x + 1, y]) v = append(v, gistogram_places[x, y + 1]) v = append(v, gistogram_places[x + 1, y + 1]) if ((x == 0) and (y == 0)): res = v / ((dot(v, v) + eps)**(1 / 2)) created = 0 else: res = append(res, v / ((dot(v, v) + eps)**(1 / 2))) return res
def get_externals_pixel_feats(image: np.array, n_values: int, k_distance: int, eps=1e-4) -> np.array: h, w = image.shape grads_map = np.array([sobel_h(image), sobel_v(image)]) grads_map = grads_map / (np.linalg.norm(grads_map, axis=0) + eps) def get_shifted_feats(directions): shifts = np.round(directions * k_distance).astype(np.int) grid = np.stack( np.meshgrid(np.arange(h), np.arange(w), indexing='ij')) # calculate coords of inner/outer pixels coords = grid + shifts # clip values coords[coords < 0] = 0 coords[:, :, -k_distance:] = np.clip(coords[:, :, -k_distance:], 0, w - 1) coords[:, -k_distance:, :] = np.clip(coords[:, -k_distance:, :], 0, h - 1) # get required pixels feats = image[coords[0].reshape(-1), coords[1].reshape(-1)].reshape(h, w) feats = feats / (np.max(feats) + eps) feats = np.ceil((n_values - 1) * feats) feats = unfold(feats[None]).astype(np.int) return feats outer_feats = get_shifted_feats(grads_map) inner_feats = get_shifted_feats(-grads_map) return inner_feats, outer_feats
def L1difference(img_true, img_pred): [h, w] = img_true.shape true_gx = sobel_h(img_true) / 4.0 true_gy = sobel_v(img_true) / 4.0 pred_gx = sobel_h(img_pred) / 4.0 pred_gy = sobel_v(img_pred) / 4.0 dx = np.abs(true_gx - pred_gx) dy = np.abs(true_gy - pred_gy) prediction_error = np.sum(dx + dy) prediction_error = 128 * 128 * prediction_error / (h * w) eps = 0.0001 if prediction_error > eps: prediction_error = 10 * np.log( (255 * 255) / prediction_error) / np.log(10) else: prediction_error = 10 * np.log((255 * 255) / eps) / np.log(10) return prediction_error
def threshold_mask(img, sz, t): img_rescaled = ((img - img.min(axis=(0, 1))) / (img.max(axis=(0, 1)) - img.min(axis=(0, 1))) - 0.5) * 2 grey = rgb2grey(img_rescaled) grey_resized = resize(grey, sz, preserve_range=True) grad = np.sqrt(sobel_h(grey_resized)**2 + sobel_v(grey_resized)**2) thr = threshold_adaptive(grad, t, method='mean') return thr.astype(int)[None, :, :, None]
def global_gradient(self): gradient_x = filters.sobel_h(self.img) gradient_y = filters.sobel_v(self.img) magnitude = np.sqrt(gradient_x**2 + gradient_y**2) angle = np.arctan(gradient_y / (gradient_x + 1e-5)) angle[angle >= 180] -= 180 return magnitude, angle
def __init__(self, image, sigma=2, magnitude=2): image = rgb2gray(image) image = gaussian(image, sigma=sigma) im_x = sobel_v(image) im_y = sobel_h(image) self.grad = np.stack([im_x, im_y], -1) self.grad *= magnitude / np.maximum( norm(self.grad, axis=2, keepdims=True), 1e-4)
def sobel_rgb_decoded(grey_img): # (-) compensates weirdness of the skimage's sobel cernel img_sobel_h = -sobel_h(grey_img) * 0.5 + 0.5 #Gy img_sobel_v = -sobel_v(grey_img) * 0.5 + 0.5 #Gx # rgb encoding sobel_rgb = np.dstack((img_sobel_v, img_sobel_h, np.ones_like(grey_img))) return sobel_rgb
def dirrectional_grad(image, theta): grad_x0 = np.cos(theta) * sobel_h( image[:, :, 0]) + np.sin(theta) * sobel_v(image[:, :, 0]) grad_x1 = np.cos(theta) * sobel_h( image[:, :, 1]) + np.sin(theta) * sobel_v(image[:, :, 1]) grad_x2 = np.cos(theta) * sobel_h( image[:, :, 2]) + np.sin(theta) * sobel_v(image[:, :, 2]) grad_x3 = np.cos(theta) * sobel_h( image[:, :, 3]) + np.sin(theta) * sobel_v(image[:, :, 3]) grad0 = np.maximum(grad_x0, 0).T grad1 = np.maximum(grad_x1, 0).T grad2 = np.maximum(grad_x2, 0).T grad3 = np.maximum(grad_x3, 0).T image = np.array([grad0, grad1, grad2, grad3]).T return image
def test_sobel_h_horizontal(): """Horizontal Sobel on an edge should be a horizontal line.""" i, j = np.mgrid[-5:6, -5:6] image = (i >= 0).astype(float) result = filters.sobel_h(image) # Check if result match transform direction assert (np.all(result[i == 0] == 1)) assert (np.all(result[np.abs(i) > 1] == 0))
def dual_gradient_energy(img1): w, h = img1.shape[:2] R = img1[:, :, 0] G = img1[:, :, 1] B = img1[:, :, 2] A = sobel_h(R)**2 + sobel_v(R)**2 + \ sobel_h(G)**2 + sobel_v(G)**2 +\ sobel_h(B)**2 + sobel_v(B)**2 r = len(range(len(A))) c = len(range(len(A[0]))) for i in range(len(A[0])): A[0][i] = A[1][i] A[r - 1][i] = A[r - 2][i] for i in range(len(A)): A[i][0] = A[i][1] A[i][c - 1] = A[i][c - 2] return A
def create_masked_image(img): img = img[5:-500, 25:-25] gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) kernel = np.ones((3, 3), np.uint8) init_closed = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel, iterations=10) gray = init_closed sobel_v = filters.sobel_v(gray) sobel_h = filters.sobel_h(gray) sobel_v = filters.gaussian(sobel_v, sigma=2.0) sobel_h = filters.gaussian(sobel_h, sigma=2.0) mask_v = np.abs(sobel_v) > .03 mask_h = np.abs(sobel_h) > .03 # Coordinates of non-black pixels. coords_y = np.argwhere(mask_v) coords_x = np.argwhere(mask_h) # Bounding box of non-black pixels. x0 = max(coords_x.min(axis=0)[0] - 50, 0) x1 = coords_x.max(axis=0)[0] + 51 y0 = max(coords_y.min(axis=0)[1] - 50, 0) y1 = coords_y.max(axis=0)[1] + 51 # Get the contents of the bounding box. cropped_gray = gray[x0:x1, y0:y1] cropped_img = img[x0:x1, y0:y1] thresh_val = np.median(cropped_gray) / 1.05 ret, thresh = cv2.threshold(cropped_gray, thresh_val, 255, cv2.THRESH_BINARY_INV) kernel = np.ones((3, 3), np.uint8) closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2) blobs, num_blobs = ndimage.label(closing) iters = 5 opening = closing while num_blobs < 4: opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel, iterations=iters) iters += 5 blobs, num_blobs = ndimage.label(opening) dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5) ret, sure_fg = cv2.threshold(dist_transform, 0.4 * dist_transform.max(), 255, 0) mask = cv2.cvtColor(sure_fg, cv2.COLOR_GRAY2RGB) img_masked = cropped_img.copy() img_masked[mask == 0] = 0 return img_masked
def hog_descriptor(patch, pixels_per_cell=(8, 8)): """ Generating hog descriptor by the following steps: 1. compute the gradient image in x and y (already done for you) 2. compute gradient histograms 3. normalize across block 4. flattening block into a feature vector Args: patch: grayscale image patch of shape (h, w) pixels_per_cell: size of a cell with shape (m, n) Returns: block: 1D array of shape ((h*w*n_bins)/(m*n)) """ assert (patch.shape[0] % pixels_per_cell[0] == 0),\ 'Heights of patch and cell do not match' assert (patch.shape[1] % pixels_per_cell[1] == 0),\ 'Widths of patch and cell do not match' n_bins = 9 degrees_per_bin = 180 // n_bins Gx = filters.sobel_v(patch) Gy = filters.sobel_h(patch) # Unsigned gradients G = np.sqrt(Gx**2 + Gy**2) theta = (np.arctan2(Gy, Gx) * 180 / np.pi) % 180 G_cells = view_as_blocks(G, block_shape=pixels_per_cell) theta_cells = view_as_blocks(theta, block_shape=pixels_per_cell) rows = G_cells.shape[0] cols = G_cells.shape[1] cells = np.zeros((rows, cols, n_bins)) o = pixels_per_cell[0] p = pixels_per_cell[1] b = np.zeros((n_bins)) x_theta = np.zeros((o, p)) y_G = np.zeros((o, p)) block = np.zeros((patch.shape[0] * patch.shape[1] * n_bins / o / p)) block = [] for i in range(0, rows): for j in range(0, cols): x_theta = flat(theta_cells[i][j]) y_G = flat(G_cells[i][j]) x_theta = (x_theta / 20).astype(int) for m in range(0, o * p): b[x_theta[m]] += 1 * y_G[m] pass block.append(b) block = np.array(block) block = simple_descriptor(block) # YOUR CODE HERE return block
def hog_descriptor(patch, pixels_per_cell=(8, 8)): """ Generating hog descriptor by the following steps: 1. compute the gradient image in x and y (already done for you) 2. compute gradient histograms 3. normalize across block 4. flattening block into a feature vector Args: patch: grayscale image patch of shape (h, w) pixels_per_cell: size of a cell with shape (m, n) Returns: block: 1D array of shape ((h*w*n_bins)/(m*n)) """ assert (patch.shape[0] % pixels_per_cell[0] == 0),\ 'Heights of patch and cell do not match' assert (patch.shape[1] % pixels_per_cell[1] == 0),\ 'Widths of patch and cell do not match' n_bins = 9 degrees_per_bin = 180 // n_bins Gx = filters.sobel_v(patch) Gy = filters.sobel_h(patch) # Unsigned gradients G = np.sqrt(Gx**2 + Gy**2) theta = (np.arctan2(Gy, Gx) * 180 / np.pi) % 180 G_cells = view_as_blocks(G, block_shape=pixels_per_cell) theta_cells = view_as_blocks(theta, block_shape=pixels_per_cell) rows = G_cells.shape[0] cols = G_cells.shape[1] cells = np.zeros((rows, cols, n_bins)) # Compute histogram per cell ### YOUR CODE HERE for i in range(rows): for j in range(cols): for i1 in range(pixels_per_cell[0]): for j1 in range(pixels_per_cell[1]): Gt = G_cells[i, j, i1, j1] Tt = theta_cells[i, j, i1, j1] bin = int(Tt // degrees_per_bin) % 9 cells[i, j, bin] += Gt mean = np.mean(cells) std = np.std(cells) if std == 0: std = 1 cells = (cells - mean) / std block = cells.flatten() pass ### YOUR CODE HERE return block
def hog_descriptor(patch, pixels_per_cell=(8, 8)): """ Generating hog descriptor by the following steps: 1. Compute the gradient image in x and y directions (already done for you) 2. Compute gradient histograms for each cell 3. Flatten block of histograms into a 1D feature vector Here, we treat the entire patch of histograms as our block 4. Normalize flattened block by L2 norm Normalization makes the descriptor more robust to lighting variations Args: patch: grayscale image patch of shape (H, W) pixels_per_cell: size of a cell with shape (M, N) Returns: block: 1D patch descriptor array of shape ((H*W*n_bins)/(M*N)) """ assert (patch.shape[0] % pixels_per_cell[0] == 0),\ 'Heights of patch and cell do not match' assert (patch.shape[1] % pixels_per_cell[1] == 0),\ 'Widths of patch and cell do not match' n_bins = 9 degrees_per_bin = 180 // n_bins Gx = filters.sobel_v(patch) Gy = filters.sobel_h(patch) # Unsigned gradients G = np.sqrt(Gx**2 + Gy**2) theta = (np.arctan2(Gy, Gx) * 180 / np.pi) % 180 # Group entries of G and theta into cells of shape pixels_per_cell, (M, N) # G_cells.shape = theta_cells.shape = (H//M, W//N) # G_cells[0, 0].shape = theta_cells[0, 0].shape = (M, N) G_cells = view_as_blocks(G, block_shape=pixels_per_cell) theta_cells = view_as_blocks(theta, block_shape=pixels_per_cell) rows = G_cells.shape[0] cols = G_cells.shape[1] # For each cell, keep track of gradient histrogram of size n_bins cells = np.zeros((rows, cols, n_bins)) # Compute histogram per cell for r in range(rows): for c in range(cols): for rpixel in range(G_cells[r][c].shape[0]): for cpixel in range(G_cells[r][c].shape[1]): binNum = int(theta_cells[r, c, rpixel, cpixel] / degrees_per_bin) % n_bins #adds gradient in direction binNum at the cell histogram cells[r, c, binNum] += G_cells[r][c][rpixel][cpixel] block = cells.flatten() block /= np.linalg.norm(block) return block
def test_hsobel_horizontal(): """Horizontal Sobel on an edge should be a horizontal line.""" i, j = np.mgrid[-5:6, -5:6] image = (i >= 0).astype(float) result = filters.sobel_h(image) # Fudge the eroded points i[np.abs(j) == 5] = 10000 assert (np.all(result[i == 0] == 1)) assert (np.all(result[np.abs(i) > 1] == 0))
def dual_gradient_energy(img): R = img[:, :, 0] G = img[:, :, 1] B = img[:, :, 2] hor_R = filters.sobel_h(R) hor_G = filters.sobel_h(G) hor_B = filters.sobel_h(B) ver_R = filters.sobel_v(R) ver_G = filters.sobel_v(G) ver_B = filters.sobel_v(B) sq_x = np.add(np.square(hor_R), np.add(np.square(hor_G), np.square(hor_B))) sq_y = np.add(np.square(ver_R), np.add(np.square(ver_G), np.square(ver_B))) energy = np.add(sq_x, sq_y) return energy
def get_gradients(img): img /= np.max(img) horiz = sobel_h(img) vert = sobel_v(img) magnitude = np.sqrt(horiz**2 + vert**2) + 1e-5 vert /= magnitude horiz /= magnitude return (vert, horiz)
def dual_gradient_energy(img): """ calculates energy at each pixel in the image and stores in new matrix :param img: given image :return: WXH array of floats, having energy of each pixel in image """ red = img[:, :, 0] green = img[:, :, 1] blue = img[:, :, 2] gradient_horizontal_red = filters.sobel_h(red) gradient_vertical_red = filters.sobel_v(red) gradient_horizontal_green = filters.sobel_h(green) gradient_vertical_green = filters.sobel_v(green) gradient_horizontal_blue = filters.sobel_h(blue) gradient_vertical_blue = filters.sobel_v(blue) horizontal_energy = numpy.square(gradient_horizontal_red) + numpy.square(gradient_horizontal_green) + numpy.square(gradient_horizontal_blue) vertical_energy = numpy.square(gradient_vertical_red) + numpy.square(gradient_vertical_green) + numpy.square(gradient_vertical_blue) energy_matrix = horizontal_energy + vertical_energy return energy_matrix
def sobel_triple(frame): """ compute horizontal/ vertical sobel intensities and convert to red/ blue values. green channel will get the un-directed sobel filter. very pleasing effect. """ output = np.zeros(frame.shape, dtype=np.uint8) frame = grayscale(frame) output[:, :, 0] = normalize(np.abs(filters.sobel_h(frame))) output[:, :, 1] = normalize(filters.sobel(frame)) output[:, :, 2] = normalize(np.abs(filters.sobel_v(frame))) return output
def stack_origi_sobel(df): """stack original image with """ df_preproc = pd.DataFrame(df['Image']) df_preproc['sobelh'] = df_preproc['Image'].apply(lambda im: sobel_h(im.reshape(96, 96)).reshape(-1)) df_preproc['sobelv'] = df_preproc['Image'].apply(lambda im: sobel_v(im.reshape(96, 96)).reshape(-1)) col = 'Image' X = np.vstack(df_preproc[col].values).reshape(-1, 1, 96, 96) col = 'sobelh' tempx1 = np.vstack(df_preproc[col].values).reshape(-1, 1, 96, 96) col = 'sobelv' tempx2 = np.vstack(df_preproc[col].values).reshape(-1, 1, 96, 96) X = np.concatenate((X, tempx1, tempx2), axis=1).astype(np.float32) return X
def sobel_hv(frame): """ compute horizontal/ vertical sobel intensities and convert to red/ blue values. green channel will be left zero. """ output = np.zeros(frame.shape, dtype=np.uint8) frame = grayscale(frame) # dilation doesn't really improve much # morphology.dilation(normalize(np.abs(filters.sobel_h(frame))), out=output[:, :, 0]) # morphology.dilation(normalize(np.abs(filters.sobel_v(frame))), out=output[:, :, 2]) output[:, :, 0] = normalize(np.abs(filters.sobel_h(frame))) output[:, :, 2] = normalize(np.abs(filters.sobel_v(frame))) return output
def stack_origi_sobel(self): """stack original image with """ df_preproc = pd.DataFrame(self.df['Image']) df_preproc['sobelh'] = df_preproc['Image'].apply(lambda im: sobel_h(im.reshape(96, 96)).reshape(-1)) df_preproc['sobelv'] = df_preproc['Image'].apply(lambda im: sobel_v(im.reshape(96, 96)).reshape(-1)) col = 'Image' self.X = np.vstack(df_preproc[col].values).reshape(-1,1,96,96) self.y = self.df[self.df.columns[:-1]].values col = 'sobelh' tempx1 = np.vstack(df_preproc[col].values).reshape(-1, 1, 96, 96) col = 'sobelv' tempx2 = np.vstack(df_preproc[col].values).reshape(-1, 1, 96, 96) self.X = np.concatenate((self.X,tempx1,tempx2), axis=1)
def dual_gradient_energy(img): """ Calculating Energy gradient :return 3D image matrix, the returned matrix is 3D to enable plotting. """ R_sobel_h = sobel_h(img[:, :, 0]) R_sobel_v = sobel_v(img[:, :, 0]) G_sobel_h = sobel_h(img[:, :, 1]) G_sobel_v = sobel_v(img[:, :, 1]) B_sobel_h = sobel_h(img[:, :, 2]) B_sobel_v = sobel_v(img[:, :, 2]) a = img[:, 0, 0].size b = img[0, :, 0].size energy = numpy.zeros((a, b, 3)) sob = numpy.zeros((a, b, 3)) for i in range(0, img[:, 0, 0].size): for j in range(0, img[0, :, 0].size): energy[i, j, 0] = R_sobel_h[i, j]**2 + R_sobel_v[i, j]**2 energy[i, j, 1] = G_sobel_h[i, j]**2 + G_sobel_v[i, j]**2 energy[i, j, 2] = B_sobel_h[i, j]**2 + B_sobel_v[i, j]**2 sob[i, j, :] = energy[i, j, 0] + energy[i, j, 1] + energy[i, j, 2] return sob
def sobel_rgb(I,features): """Like skimage.sobel_{h,v} but works on RGB images. Returns a NxMx6 image with channels being hr, hg, hb, vr, vg, vb. """ #denoised = gaussian_filter(image, 2) I = np.atleast_3d(I) if features=='sobelHandv': return np.dstack( [sobel_h(I[..., c]) for c in range(I.shape[-1])]+ [sobel_v(I[..., c]) for c in range(I.shape[-1])] ) else: return np.dstack( [sobel(I[..., c]) for c in range(I.shape[-1])]# + #[sobel_v(I[..., c]) for c in range(I.shape[-1])] )
def get_spatial_information_score(image: Image) -> float: """Use Sobel filters to find the edge energy of an image. .. math:: SI_r = \sqrt{S_v^2 + S_h^2} SI_{mean} = \frac{1}{P}\sum{SI_r,} Where :math:`SI_r` is the spatial energy for each pixel and :math:`P` the number of pixels. .. seealso:: http://vintage.winklerbros.net/Publications/qomex2013si.pdf """ img = np.asarray(image) num_pixels = img.shape[0] * img.shape[1] energy = np.sum(np.sqrt(sobel_v(img) ** 2 + sobel_h(img) ** 2)) return energy / num_pixels
def eoh(image): data = { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0 } canny_image = canny(image, low_threshold=40) row, col = canny_image.shape sobel_v_image = sobel_v(image) sobel_h_image = sobel_h(image) for r in xrange(row): for c in xrange(col): if not canny_image[r][c]: continue interval = which_interval(sobel_v_image[r][c], sobel_h_image[r][c]) if data.has_key(interval): data[interval] += 1 return data
def test_hsobel_zeros(): """Horizontal sobel on an array of all zeros.""" result = filters.sobel_h(np.zeros((10, 10)), np.ones((10, 10), bool)) assert (np.all(result == 0))
def dual_gradient_energy(img): R = img[:, :, 0] G = img[:, :, 1] B = img[:, :, 2] return sobel_h(R)**2 + sobel_v(R)**2 + sobel_h(G)**2 + \ sobel_v(G)**2 + sobel_h(B)**2 + sobel_v(B)**2
def test_hsobel_vertical(): """Horizontal Sobel on a vertical edge should be zero.""" i, j = np.mgrid[-5:6, -5:6] image = (j >= 0).astype(float) * np.sqrt(2) result = filters.sobel_h(image) assert_allclose(result, 0, atol=1e-10)
def test_hsobel_mask(): """Horizontal Sobel on a masked array should be zero.""" np.random.seed(0) result = filters.sobel_h(np.random.uniform(size=(10, 10)), np.zeros((10, 10), bool)) assert (np.all(result == 0))