def filter_offdiagonal(P, D=10, freq=0.4): # filter off-diagonal weights from projection matrix Px = _repeat(np.array(P), D) re, im = gabor(Px, theta=-np.pi/4.0, frequency=freq) F = np.sqrt(re**2+im**2)[D:(-D),D:(-D)] re, im = gabor(Px, theta=np.pi/4.0, frequency=freq) G = np.sqrt(re**2+im**2)[D:(-D),D:(-D)] P = F if np.sum(F)>np.sum(G) else G P = P**2 # aleviate filter low-passing P = np.diag(1/np.sum(P, axis=1)).dot(P) return P
def gabor_filter(image, frequency): if isinstance(image, list): image_matrix = list() for img in image: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) filt_real, filt_imag = gabor(img, frequency=frequency) image_matrix.append(filt_real) return image_matrix else: image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) filt_real, filt_imag = gabor(image, frequency=frequency) return filt_real
def get_gaborfeat(image_list, name_list, label_list, savePath): i = 0 for image in image_list: try: # 如果是灰度图片 把3改为-1 image = np.reshape(image, (image_height, image_width, 3)) except: print('发生了异常,图片大小size不满足要求:',name_list[i]) continue gray = rgb2gray(image) / 255.0 # 这句话根据你的尺寸改改 fd_real,fd_image = filters.gabor(gray,frequency=0.6) plt.figure('gabor',figsize=(8,8)) plt.subplot(121) plt.title('filt_real') plt.imshow(fd_real) plt.subplot(121) plt.title('filt_image') plt.imshow(fd_image) plt.show() fd_name = name_list[i] fd_path = os.path.join(savePath, fd_name) joblib.dump(fd_real, fd_path) i += 1 print("Test features are extracted and saved.")
def gab(left,the,fre): fmax=numpy.pi/2 a=numpy.sqrt(2) freq=fmax/(a**fre) thea=numpy.pi*the/8 filt_real,filt_imag=numpy.asarray(gabor(left,theta=thea,frequency=freq)) return filt_real
def get_Gab(img_array, grid_size=1): """ Gabor filters. :param im_path: :param grid_size: :return: """ img = np.asarray(img_array) window_size = (np.asarray([img.shape]) / grid_size).astype(int)[0] im_grid = np.asarray(skimage.util.view_as_blocks(img, tuple(window_size))) windows = [] for i in range(grid_size): for j in range(grid_size): windows.append(im_grid[i, j]) freq_step = np.sqrt(2) or_step = np.pi / 4. gab_feat = [] for i in range(len(windows)): for j in range(6): # frequencies for k in range(8): # orientations a = gabor(windows[i], frequency=(.25 / (freq_step ** j)), theta=k * or_step, sigma_x=1, sigma_y=1) b = np.sqrt(a[0] ** 2 + a[1] ** 2) mean = np.mean(b) std = np.std(b) if mean == np.inf: mean = 0 if std == np.inf: std = 0 gab_feat.append(mean) gab_feat.append(std) out = np.ravel(np.asarray(gab_feat)) return out
def apply_pipeline(im, pipeline): for pip in pipeline: name = pip['name'] pms = pip['params'] if (name == 'gabor'): im = filters.gabor(im, frequency=pms['frequency'], theta=pms['theta'], bandwidth=pms['bandwidth'], mode=pms['mode']) elif (name == 'gaussian'): im = filters.gaussian(im, sigma=float(pms['sigma']), mode=pms['mode']) elif (name == 'median'): im = filters.median(im) elif (name == 'scharr'): im = filters.scharr(im) elif (name == 'roberts'): im = filters.roberts(im) # Morphology elif (name == 'closing'): im = morphology.closing(im) elif (name == 'dilation'): im = morphology.dilation(im) elif (name == 'erosion'): im = morphology.erosion(im) elif (name == 'opening'): im = morphology.opening(im) # Transforms elif (name == 'rgb2gray'): im = color.rgb2gray(im) else: print '$$$ Error: ' + name + ' not valid kernel.' return im
def getGaborFiltered(n,x): gaborFilteredReal = x[0:n][:] gaborFilteredImaginary = x[0:n][:] image = np.array([np.array([0 for e in range(32)]) for k in range(32)]) # counter=0 for p in range(n): counter=0 for e in range(32): image[e] = x[p][counter:counter+32] counter = counter+32 # fig, ax = plt.subplots(ncols=3) # ax[0].imshow(image, cmap=plt.cm.gray) # #plt.show() gabor_real, gabor_imaginary = gabor(image, frequency=0.1, theta=0.5) # ax[1].imshow(gabor_real, cmap=plt.cm.gray) # #plt.show() # ax[2].imshow(gabor_imaginary, cmap=plt.cm.gray) # ax[0].set_title('Original Image') # ax[1].set_title('Gabor (Real)') # ax[2].set_title('Gabor (Imaginary)') # plt.show() count=0 for e in range(32): gaborFilteredReal[p][count:count+32]=gabor_real[e] gaborFilteredImaginary[p][count:count+32]=gabor_imaginary[e] return gaborFilteredReal,gaborFilteredImaginary
def extract_gabor(self): im_grey, lab_features = S.rgb2lab() frequencies = [1.571] orientation_rads = [0, 0.785398, 1.5708, 2.35619] ## generate list of combinations combos = list(IT.product(frequencies, orientation_rads)) list_of_mags = [] for item in combos: real_gabor, imaginary_gabor = filters.gabor( im_grey, item[0], item[1]) ## find the magnitude (square root of the sum of squares of real and imaginary gabor mag = np.sqrt( np.square(real_gabor, dtype=np.float64) + np.square(imaginary_gabor, dtype=np.float64)) sigma = .5 * ((2 * pi) / item[0]) K = 2 ## apply gaussian filter and scale the result with zero mean and unit variance mag_gaussian = filters.gaussian(mag, K * sigma) mag_gaussian_flattened = np.reshape(mag_gaussian, (1, (self.x * self.y))) list_of_mags.append(scale(mag_gaussian_flattened, axis=1)) list_of_mags = np.asarray(list_of_mags).reshape( len(combos), self.dimensions) ## combine gabor features with lab features im_grey_flattened = np.reshape(im_grey, (1, (self.x * self.y))) features = np.vstack((im_grey_flattened, list_of_mags)) return features
def GaborFilterComplex(img): scale = 4 orientation = 6 minf = 0.1 maxf = 0.3 step = (maxf - minf) / (scale - 1) base = maxf / minf a = math.pow(base, 1.0 / (scale - 1)) imgs = [] for i in range(2): for j in range(scale): f = j * step + minf print(j, f) u0 = maxf / math.pow(a, scale - j) Uvar = (a - 1) * u0 / ((a + 1) * np.sqrt(2 * np.log(2.0))) z = -2 * np.log(2) * (Uvar * Uvar) / u0 Vvar = np.tan(math.pi / (2.0 * orientation)) * ( u0 + z) / np.sqrt(2 * np.log(2.0) - z * z / (Uvar * Uvar)) Xvar = 1 / (2 * math.pi * Uvar) Yvar = 1 / (2 * math.pi * Vvar) std = np.sqrt(Xvar * Xvar + Yvar * Yvar) real, imagin = gabor(img, frequency = f, \ n_stds = 3, theta = math.pi*i/orientation, sigma_x=Xvar, sigma_y=Yvar) imgs.append(real.reshape(1, -1)) imgs.append(imagin.reshape(1, -1)) return imgs
def master_control(image): # image = cv2.resize(image, (int(image.shape[1]*0.3), int(image.shape[0]*0.3)), interpolation=cv2.INTER_CUBIC) # 图片分辨率很大是要变小 b, g, r = cv2.split(image) # image sk_frangi_img = frangi( g, scale_range=(0, 1), scale_step=0.01, beta1=1.5, beta2=0.01) # 线宽范围,步长,连接程度(越大连接越多),减少程度(越大减得越多)0.015 sk_frangi_img = morphology.closing(sk_frangi_img, morphology.disk(1)) sk_gabor_img_1, sk_gabor_1 = gabor(g, frequency=0.35, theta=0) sk_gabor_img_2, sk_gabor_2 = gabor(g, frequency=0.35, theta=45) # 越小越明显 sk_gabor_img_3, sk_gabor_3 = gabor(g, frequency=0.35, theta=90) sk_gabor_img_4, sk_gabor_4 = gabor(g, frequency=0.35, theta=360) # 横向皱纹 sk_gabor_img_1 = morphology.opening(sk_gabor_img_1, morphology.disk(2)) sk_gabor_img_2 = morphology.opening(sk_gabor_img_2, morphology.disk(1)) sk_gabor_img_3 = morphology.opening(sk_gabor_img_3, morphology.disk(2)) sk_gabor_img_4 = morphology.opening(sk_gabor_img_4, morphology.disk(2)) all_img = cv2.add( 0.1 * sk_gabor_img_2, 0.9 * sk_frangi_img ) # + 0.02 * sk_gabor_img_1 + 0.02 * sk_gabor_img_2 + 0.02 * sk_gabor_img_3 all_img = morphology.closing(all_img, morphology.disk(1)) _, all_img = cv2.threshold(all_img, 0.3, 1, 0) img1 = all_img # print(all_img, all_img.shape, type(all_img)) # contours, image_cont = cv2.findContours(all_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # all_img = all_img + image_cont bool_img = all_img.astype(bool) label_image = measure.label(bool_img) count = 0 for region in measure.regionprops(label_image): if region.area < 10: # or region.area > 700 x = region.coords for i in range(len(x)): all_img[x[i][0]][x[i][1]] = 0 continue if region.eccentricity > 0.98: count += 1 else: x = region.coords for i in range(len(x)): all_img[x[i][0]][x[i][1]] = 0 skel, distance = morphology.medial_axis(all_img.astype(int), return_distance=True) skels = morphology.closing(skel, morphology.disk(1)) trans1 = skels # 细化 return skels, count # np.uint16(skels.astype(int))
def gabor(im, frequency=0.1, theta=0): """ Texture filter - finds specific frequency content in a specific direction CURRENTLY COMPUTES BASED ON 2D (but input can be 3D) Arguments: im - input image frequency - the number of pixels taken from each side to create a neighborhood. theta - orientation in radians (angel of the gabor patch). Use values like: 0, np.pi/x """ if im.ndim == 3: f = np.zeros(im.shape) f_temp = np.zeros(im.shape) for i in range(im.shape[0]): f[i], f_temp = filters.gabor(im[i], frequency, theta) else: f, f_temp = filters.gabor(im, frequency, theta) return f
def image_pattern_features(img, N, sigmas, freqs): pattern_features = [] for n in range(N): theta = (n * np.pi) / N for sigma in sigmas: for freq in freqs: filtered_real, filtered_img = gabor(img, freq, theta=theta, sigma_x=sigma, sigma_y=sigma) pattern_features.append(filtered_real + np.complex(0, 1) * filtered_img) return pattern_features
def gabor_features(image, scales=(2, 4, 8), orientations=(0, np.pi / 4, np.pi / 2, 3 * np.pi / 4)): features = [] for theta in orientations: for scale in scales: features.append(np.abs(gabor(image, scale, theta)[0]).ravel()) return np.array(features)
def gabor_filter(): gen = get_single_img_label() for img, label in gen: img = resize_pic(img, [96, 96]) gauss_img = filters.gaussian(img, sigma=0.9) gabor_real0, gabor_virt0 = filters.gabor(gauss_img, frequency=0.8, theta=0) gabor_real45, gabor_virt45 = filters.gabor(gauss_img, frequency=0.8, theta=45) gabor_real90, gabor_virt90 = filters.gabor(gauss_img, frequency=0.9, theta=90) gabor_real135, gabor_virt135 = filters.gabor(gauss_img, frequency=0.9, theta=135) plt.figure() plt.subplot(2, 3, 1) plt.imshow(img) plt.title('ori') plt.subplot(2, 3, 2) plt.imshow(gauss_img) plt.title('gauss') plt.subplot(2, 3, 3) plt.imshow(gabor_real0) plt.title('0') plt.subplot(2, 3, 4) plt.imshow(gabor_real45) plt.title('45') plt.subplot(2, 3, 5) plt.imshow(gabor_real90) plt.title('90') plt.subplot(2, 3, 6) plt.imshow(gabor_real135) plt.title('135') plt.show()
def get_gabor_feature(self, image): classify = np.array([]) # For 5 scales for i in range(0, 5, 1): # 8 orientations for j in range(0, 8, 1): # Get the real values from gabor function real_val = gabor(image, frequency=(i + 1) / 10.0, theta=j * pi / 8)[0] # Get the imaginary values from gabor function img_val = gabor(image, frequency=(i + 1) / 10.0, theta=j * pi / 8)[1] # Get the square of both values and add them up to get a complete result result = real_val * real_val + img_val * img_val res_mean = np.mean(result) classify = np.append(classify, res_mean) return classify
def gabor_filter(gray, frequency, theta): mask = 10 sigma = mask / 2 filt_real, filt_imag = gabor(gray, frequency, theta=theta * np.pi, sigma_x=sigma, sigma_y=sigma, n_stds=mask) return filt_real
def gabor(frame, frequency=0.2, real=True): """ return real or imaginary response to the gabor filter """ real_frame, imag_frame = filters.gabor(grayscale(frame), frequency) if real: return real_frame else: return imag_frame
def gab(left, the, fre): fmax = numpy.pi / 2 a = numpy.sqrt(2) freq = fmax / (a**fre) thea = numpy.pi * the / 8 img = [] for i in range(left.shape[0]): filt_real, filt_imag = numpy.asarray( gabor(left[i, :, :], theta=thea, frequency=freq)) img.append(filt_real) return np.asarray(img)
def get_gabor(): ''' 调用skimage中的函数创建gabor滤波器 :return: ''' # image = data.coins() image = io.imread(r'../test/2.jpg', as_grey=True) print image # detecting edges in a coin image filt_real, filt_imag = gabor(image, frequency=0.6, n_stds=7, theta=180) plt.figure() io.imshow(filt_real) io.show() # less sensitivity to finer details with the lower frequency kernel filt_real, filt_imag = gabor(image, frequency=0.1) plt.figure() io.imshow(filt_real) # io.imshow(filt_imag) io.show()
def get_sample_bbox_features(original_image, bbox): # -----> &&& # Extract the original image: raw_image = get_bbox_image(original_image, bbox) # -----> GO TO eee # Resize to 60 x 60 and convert to grayscale: trans_image = resize(color.rgb2gray(raw_image), (RESIZE_VALUE, RESIZE_VALUE)) # Histogram Equalization image_eq = exposure.equalize_hist(trans_image) # Gabor_filter: filt_real, _ = gabor(image_eq, frequency=0.6) # Sampling: sampled_image = transform.downscale_local_mean(filt_real, (2, 2)).flatten() return sampled_image
def gabor_filter(image, frequency=1, theta=0): """Execute Gabor filter to extract texture feature. Args: frequency(float): frequency to calculate gabor filter method theta(float): angle to calculate gabor filter method Returns: tuple: returns the real and imaginary coordinates """ image_grey = ColorProcessor.convert_rgb_to_grey(image) return gabor(image_grey, frequency=frequency, theta=theta)
def gabor_filter(self, image, frequency): """ :param image: :param frequency: :return: """ try: if isinstance(image, list): image_matrix = list() for img in image: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) filt_real, filt_imag = gabor(img, frequency=frequency) image_matrix.append(filt_real) return image_matrix else: image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) filt_real, filt_imag = gabor(image, frequency=frequency) return filt_real except Exception as e: self.logger.exception(e) sys.exit(1)
def _compute_features(im): gabor_frequencies = np.logspace(-3, 1, num=5, base=2) thetas = [0, np.pi/2] nb_fq = len(gabor_frequencies) * len(thetas) im = np.atleast_3d(im) im_gabor = np.empty((im.shape[-1], nb_fq) + im.shape[:2]) for ch in range(im.shape[-1]): img = img_as_float(im[..., ch]) for i_fq, fq in enumerate(gabor_frequencies): for i_th, theta in enumerate(thetas): tmp = filters.gabor(img, fq, theta=theta) im_gabor[ch, len(thetas) * i_fq + i_th] = \ np.abs(tmp[0] + 1j * tmp[1]) return im_gabor
def single_img_pre_process(img, filter_in_channel): ori_shape = img.shape ori_dtype = img.dtype img = np.reshape(img, newshape=(ori_shape[0], ori_shape[1])) gab = [] for i in range(filter_in_channel): gabor_real, gabor_img = filters.gabor(img, frequency=0.6, theta=i * 45) gab.append(gabor_real) # sobel_img = filters.sobel(img) # gab.append(gabor_real) result = np.stack(gab, axis=2) result = result.astype(dtype=ori_dtype) return result
def skyViewGabor(skyViewArr): gabors = [] for i in np.arange(0.03, 0.08, 0.01): print(i) for j in np.arange(0, 3, 0.52): gabors.append(gabor(skyViewArr, theta=j, frequency=i)[0]) merged = skyViewArr.copy() for i in range(len(merged)): for j in range(len(merged[i])): merged[i][j] = 0 for i in range(len(merged)): for j in range(len(merged[i])): for k in range(len(gabors)): merged[i][j] += gabors[k][i][j] return merged
def get_gabor_feature(image, name): classify = np.array([]) label = np.array([]) # For 5 scales for i in range(0, 5, 1): # 8 orientations for j in range(0, 8, 1): # Get the real values from gabor function real_val = gabor(image, frequency=(i + 1) / 10.0, theta=j * pi / 8)[0] # Get the imaginary values from gabor function img_val = gabor(image, frequency=(i + 1) / 10.0, theta=j * pi / 8)[1] # Get the square of both values and add them up to get a complete result result = real_val * real_val + img_val * img_val res_mean = np.mean(result) classify = np.append(classify, res_mean) label = np.append(label, name) print('Gabor Features:') print(classify) print('Length Gabor Features:') print(len(classify)) allVectors.append(classify) allLabels.append(name)
def get_image_feature(path): """ frequency为正弦函数的频率,sigma_x/sigma_y为Gaussian 包络的标准差, theta为控制函数的方向.由于掌纹方向单一,令theta=O, 取frequency=0.0916,sigma_x/sigma_y=5.6179 参考文档: https://www.w3cschool.cn/doc_scikit_image/scikit_image-api-skimage-filters.html :param path: 图片路径 :return: """ img = color.rgb2gray(io.imread(path)) real, imag = filters.gabor(img, frequency=0.0916, sigma_x=5.6179, sigma_y=5.6179, theta=0) return imag
def filter_show(data): for k in [12, 3088]: img = np.array(data[k]).reshape((28, 28)) image_scharr = scharr(img) image_sobel = sobel(img) image_prewitt = prewitt(img) image_gabor_real, image_gabor_im = gabor(img, frequency=0.65) image_roberts = roberts(img) image_roberts_pos = roberts_pos_diag(img) image_roberts_neg = roberts_neg_diag(img) image_frangi = frangi(img) image_laplace = laplace(img) image_hessian = hessian(img) image_threshold_local_3 = threshold_local(img, 3) image_threshold_local_5 = threshold_local(img, 5) image_threshold_local_7 = threshold_local(img, 7) image_threshold_niblack = threshold_niblack(img, window_size=5, k=0.1) image_threshold_sauvola = threshold_sauvola(img) image_threshold_triangle = img > threshold_triangle(img) fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True, figsize=(8, 8)) ax = axes.ravel() ax[0].imshow(img, cmap=plt.cm.gray) ax[0].set_title('Original image') ax[1].imshow(image_threshold_niblack, cmap=plt.cm.gray) ax[1].set_title('Niblack') ax[2].imshow(image_threshold_sauvola, cmap=plt.cm.gray) ax[2].set_title('Sauvola') ax[3].imshow(image_threshold_triangle, cmap=plt.cm.gray) ax[3].set_title('Triangle') for a in ax: a.axis('off') plt.tight_layout() plt.show() plt.close()
def single_img_pre_process(img): ori_shape = img.shape ori_dtype = img.dtype img = np.reshape(img, newshape=(ori_shape[0], ori_shape[1])) gab = [ img, ] for i in range(8): gabor_real, gabor_virt = filters.gabor(img, frequency=0.8, theta=i * 22.5) gab.append(gabor_real) result = np.stack(gab, axis=2) result = result.astype(dtype=ori_dtype) return result
def gabor_2d_filter(image): """ TODO For 3D compatibility: https://github.com/scikit-image/scikit-image/issues/2704 """ gabor_real_image, gabor_imaginary_image = filters.gabor( image, frequency=3, theta=0, bandwidth=1, sigma_x=None, sigma_y=None, n_stds=3, offset=0, mode="reflect", cval=0, ) return gabor_real_image
from skimage.filter import threshold_otsu, threshold_adaptive global_thresh = threshold_otsu(gray0.astype(np.float32)) binary_global = gray0.astype(np.float32) > global_thresh plt.imshow(binary_global) block_size = 45 binary_adaptive = threshold_adaptive(gray0, block_size, offset=5) plt.imshow(binary_adaptive) from skimage.filters import gabor from skimage import data, io from matplotlib import pyplot as plt #### ESSE E FODA !!!!! filt_real, filt_imag = gabor(gray0, frequency=2.1) plt.figure() io.imshow(filt_real) io.show() plt.figure() io.imshow(filt_imag) io.show() ## from skimage.filters import gaussian, gaussian_filter, laplace,prewitt from skimage.filters import prewitt_v,prewitt_h,scharr, wiener gauss=gaussian(gray0, sigma=5, multichannel=True) plt.imshow(gauss)