def compute_tensor_struct_1(img, im_x, im_y, path, out_names, img_shw_step): #============================================== # configuration parameter H, W = img.shape SIGMA = 0 MODE = 'constant' CVAL = 0 #============================================== grad_vector = np.concatenate( (np.expand_dims(im_x, -1), np.expand_dims(im_y, -1)), -1).reshape([H, W, 2, 1]) Axx = ndi.gaussian_filter(im_x * im_x, [SIGMA, SIGMA], mode=MODE, cval=CVAL) Axy = ndi.gaussian_filter(im_x * im_y, [SIGMA, SIGMA], mode=MODE, cval=CVAL) Ayy = ndi.gaussian_filter(im_y * im_y, [SIGMA, SIGMA], mode=MODE, cval=CVAL) if img_shw_step: vap_imshow_set(1, 3, 1, Axx, "Axx") vap_imshow_set(1, 3, 2, Axy, "Axy") vap_imshow_set(1, 3, 3, Ayy, "Ayy") vap_imsave(path, out_names + ['04Axx_Axy_Ayy']) Axx = np.expand_dims(Axx, -1) Axy = np.expand_dims(Axy, -1) Ayy = np.expand_dims(Ayy, -1) struct_tensor = np.reshape(np.concatenate((Axx, Axy, Axy, Ayy), -1), [H, W, 2, 2]) return struct_tensor, grad_vector
def _3d_shadow_detect_smooth_filter_by_mean_shift(img, img_path, img_shw_step=False): # ============================================== # === configuration parameter H, W = img.shape # ============================================== # === mean-shift flatImg = np.reshape(img, [-1, 1]) print(H, W) print(flatImg.shape) print(img.shape) bandwidth = estimate_bandwidth(flatImg, quantile=0.2, n_samples=1000) ms = MeanShift(bandwidth=bandwidth, bin_seeding=True) ms.fit(flatImg) labels = ms.labels_ print(labels.shape) print(labels) segmentedImg = np.reshape(labels, (H, W)) segmentedImg = normalize(segmentedImg) vap_imshow_set(1, 1, 1, segmentedImg, "log Blue") vap_imsave(img_path, ['ill_3DSegmentedImg']) return img
def vap_ridgeness_detection(img, img_path, img_output_names, img_shw_step=False): #============================================== # Pre-Process img = _prepare_grayscale_input_2D(img) [H, W] = img.shape if img_shw_step: vap_imshow(img, "Input image") vap_imsave(img_path, img_output_names + ['01Input_image']) #============================================== # Configuration setting SIGMA = 0.5 MODE = 'constant' CVAL = 0 #============================================== # Implement algorithm """ Step 1: Gaussian smoothing """ img = img_gaussian_smooth(img, MODE, CVAL) if img_shw_step: vap_imshow(img, "Smooth_version_of_image") vap_imsave(img_path, img_output_names + ['02Smooth_version_of_image']) """ Step 2: Compute derivatives """ [im_x, im_y] = compute_derivatives(img, mode=MODE, cval=CVAL) if img_shw_step: vap_imshow_set(1, 2, 1, im_x, "X derivative") vap_imshow_set(1, 2, 2, im_y, "Y derivative") vap_imsave(img_path, img_output_names + ['03X_Y_derivative']) """ Step 3: Build structure tensor """ struct_tensor, grad_vector = compute_tensor_struct_1( img, im_x, im_y, img_path, img_output_names, img_shw_step) # struct_tensor, grad_vector = compute_tensor_struct_2(img, im_x, im_y, img_path, img_output_names, img_shw_step) """ Step 4: find dominant gradient vector """ ### Compute eigenvalue, eigenvector for each structure tensor [eig_val, eig_vector] = np.linalg.eig(struct_tensor) # eig_vector = eig_vector.transpose((0, 1, 3, 2)) ### Find dominant gradient by finding the greatest eigen value dominant_idx = np.argmax(eig_val, axis=-1) [axV, axU] = np.indices([H, W]) dominant_vector = np.reshape( eig_vector[axV.flat, axU.flat, dominant_idx.flat], [H, W, 2]) dominant_vector_t = np.reshape(dominant_vector, (H, W, 1, 2)) if img_shw_step: idx = slice(None, None, 10) a = np.linspace(0, W - 1, W) b = np.linspace(0, H - 1, H) vap_imnew() plt.quiver(a[idx], b[idx], dominant_vector[idx, idx, 0], dominant_vector[idx, idx, 1], pivot='mid') plt.title("Dominate vector", loc='center') vap_imsave(img_path, img_output_names + ['05dominant_vector']) ### Dominant vector with direction sign_mask = np.matmul(dominant_vector_t, grad_vector).reshape(H, W) dominant_vector[sign_mask < 0] *= -1 dominant_vector[sign_mask == 0] *= 0 if img_shw_step: vap_imnew() plt.quiver(np.arange(W), np.arange(H), dominant_vector[:, :, 0], dominant_vector[:, :, 1], pivot='mid') plt.title("Dominate vector with direction", loc='center') vap_imsave(img_path, img_output_names + ['06dominant_vector_with_direction']) """ Step 5: Compute divergence """ vector_u = dominant_vector[:, :, 0] vector_v = dominant_vector[:, :, 1] ridge_img = -divergence([vector_v, vector_u]) ridge_img[ridge_img < 0.25] = 0 if img_shw_step: vap_imshow(ridge_img, "Original Ridgeness Image") vap_imsave(img_path, img_output_names + ['07Original_Ridgeness_Image']) """ Step 6: Discard ridge point with large horizontal component """ theta = np.abs(np.arctan2(vector_v, vector_u)) mask = np.logical_and(theta > np.pi * 0.4, theta < np.pi * 0.6) ridge_img[mask] = 0 if img_shw_step: vap_imshow(ridge_img, "Theta Filter Ridgeness image") vap_imsave(img_path, img_output_names + ['08Theta_Filter_Ridgeness_image']) """ Step 7: Confident Filter image """ ridge_img *= ( 1 - np.exp(-np.power(eig_val[:, :, 0] - eig_val[:, :, 1], 2) / 0.001)) ridge_img[ridge_img < 0.5] = 0 if img_shw_step: vap_imshow(ridge_img, "Confident Filter Image") vap_imsave(img_path, img_output_names + ['09Confident_Filter_Image']) return np.uint8(ridge_img * 128)
def write_img_with_intensity_3(img1, title1, img2, title2, img3, title3, img_path, output_name): vap_imshow_set(2, 2, 1, img1, title1) vap_imshow_set(2, 2, 2, img2, title2) vap_imshow_set(2, 2, 3, img3, title3) vap_imsave(img_path, output_name)
def write_img_with_intensity(img, title, img_path, output_name): vap_imshow_set(1, 1, 1, img, title) vap_imsave(img_path, output_name)
def vap_ridgeness_detection(image, path, output_names, show_step_result=True): image = _prepare_grayscale_input_2D(image) [rowImg, colImg] = image.shape if show_step_result: vap_imshow(image, "Input image") vap_imsave(path, output_names + ['01Input_image']) # Configuration setting SIGMA = 0.5 MODE = 'constant' CVAL = 0 # Gaussian smoothing image = img_gaussian_smooth(image, MODE, CVAL) if show_step_result: vap_imshow(image, "Smooth_version_of_image") vap_imsave(path, output_names + ['02Smooth_version_of_image']) # Step 1: Compute derivatives imx, imy = compute_derivatives(image, mode=MODE, cval=CVAL) # np.gradient(image)# if show_step_result: vap_imshow_set(1, 2, 1, imx, "X derivative") vap_imshow_set(1, 2, 2, imy, "Y derivative") vap_imsave(path, output_names + ['03X_Y_derivative']) # Step 3: Create a local 2x2 structure tensor for each pixel imx = np.expand_dims(imx, -1) imy = np.expand_dims(imy, -1) gradient_vector = np.concatenate((imx, imy), axis=2) gradient_vector = np.reshape(gradient_vector, (rowImg, colImg, 2, 1)) gradient_vector_t = np.reshape(gradient_vector, (rowImg, colImg, 1, 2)) structure_tensor = np.matmul(gradient_vector, gradient_vector_t) tensor_std = 0.5 structure_tensor = ndi.gaussian_filter( structure_tensor, [tensor_std, tensor_std, tensor_std, tensor_std], mode="constant") # for x in range(len(structure_tensor)): # for y in range(len(structure_tensor[x])): # structure_tensor[x,y]=gradient_vector[x,y].dot(gradient_vector_t[x,y]) # Step 4: Compute eigenvalue, eigenvector for each structure tensor [eigValue, eigVector] = np.linalg.eig(structure_tensor) # eigVector = eigVector.transpose((0,1,3,2)) # Step 5: Find dominant gradient by finding the greatest eigen value dominantIndex = np.argmax(eigValue, axis=-1) [axV, axU] = np.indices([rowImg, colImg]) dominantVector = np.reshape( eigVector[axV.flat, axU.flat, dominantIndex.flat], [rowImg, colImg, 2]) print("dominantVector shape", dominantVector.shape) dominantVector_T = np.reshape(dominantVector, (rowImg, colImg, 1, 2)) if show_step_result: vap_imnew() idx = slice(None, None, 10) a = np.linspace(0, colImg - 1, colImg) b = np.linspace(0, rowImg - 1, rowImg) q = plt.quiver(a[idx], b[idx], dominantVector[idx, idx, 0], dominantVector[idx, idx, 1], pivot='mid') plt.title("Dominate vector", loc='center') vap_imsave(path, output_names + ['05dominant_vector']) # Step 6: Dominant vector with direction signMask = np.matmul(dominantVector_T, gradient_vector) sign_max = np.amax(signMask) print("sign_max", np.unique(signMask)) # for x in range(len(signMask)): # for y in range(len(signMask[x])): # signMask[x,y]=dominantVector_T[x,y].dot(gradient_vector[x,y]) signMask = np.reshape(signMask, (rowImg, colImg)) dominantVector[signMask < 0] *= -1 dominantVector[abs(signMask) < 1e-5] *= 0 dominantVector[signMask > 0] *= 1 if show_step_result: vap_imnew() # idx=slice(None,None,10) # a=np.linspace(0,colImg-1,colImg) # b=np.linspace(0,rowImg-1,rowImg) # q=plt.quiver(a[idx], b[idx], dominantVector[idx,idx,0], dominantVector[idx,idx,1], pivot='mid') plt.quiver(np.arange(colImg), np.arange(rowImg), dominantVector[:, :, 0], dominantVector[:, :, 1], pivot='mid') plt.title("Dominate vector with direction", loc='center') # plt.show() vap_imsave(path, output_names + ['06dominant_vector_with_direction']) # Step 7: Compute divergence vectorU = dominantVector[:, :, 0] vectorV = dominantVector[:, :, 1] ridgeImage = -divergence([vectorV, vectorU]) ridgeImage[ridgeImage < 0.25] = 0 if show_step_result: vap_imshow(ridgeImage, "Original Ridgeness Image") vap_imsave(path, output_names + ['07Original_Ridgeness_Image']) # Step 8: Discard ridge point with large horizontal component theta = np.abs(np.arctan2(vectorV, vectorU)) mask = np.logical_and(theta > np.pi * 0.4, theta < np.pi * 0.6) ridgeImage[mask] = 0 if show_step_result: vap_imshow(ridgeImage, "Theta Filter Ridgeness image") vap_imsave(path, output_names + ['08Theta_Filter_Ridgeness_image']) # Step 9: Confident Filter image ridgeImage *= (1 - np.exp(-(eigValue[:, :, 0] - eigValue[:, :, 1])**4 / 0.001)) ridgeImage[ridgeImage < 0.5] = 0 # ridgeImage[ridgeImage > 0.5] = 1 ridgeImage[:, 0:5] = 0 ridgeImage[:, colImg - 10:colImg] = 0 if show_step_result: vap_imshow(ridgeImage, "Confident Filter Image") vap_imsave(path, output_names + ['09Confident_Filter_Image']) #Step 10: RANSAC filter ransac_result, test_result = vap_ransac_method(ridgeImage) if show_step_result: vap_imshow(ransac_result, "Ransac Image") vap_imsave(path, output_names + ['10RANSAC_Image']) vap_imshow(test_result, "Test Ransac Image") vap_imsave(path, output_names + ['11 Test RANSAC_Image']) return ridgeImage