def apply_filters(images, sigmas): """ Apply multiple filters to 'images' """ filtered_images = [] for img in images: filtered = [] for sigma in sigmas: for conv_filter in [ gaussian_filter, gaussian_laplace, gaussian_gradient_magnitude ]: filtered.append(conv_filter(img, sigma)) # *_eigenvals has changed from version 0.13 to 0.14. try: # v. 0.14 eigs_struc = structure_tensor_eigvals( *structure_tensor(img, sigma=sigma)) eigs_hess = hessian_matrix_eigvals( hessian_matrix(img, sigma=sigma, order="xy")) except TypeError as e: # v. 0.13 eigs_struc = structure_tensor_eigvals( *structure_tensor(img, sigma=sigma)) eigs_hess = hessian_matrix_eigvals( *hessian_matrix(img, sigma=sigma, order="xy")) for eig_h, eig_s in zip(eigs_struc, eigs_hess): filtered.append(eig_h) filtered.append(eig_s) filtered.append(equalize_hist(img)) #selem = disk(30) #filtered.append(equalize(img, selem=selem)) filtered_images.append(filtered) return np.array(filtered_images)
def fromimage(cls, image, sigma=1.0): """ Construct a structure tensor from an input image. Parameters ---------- image: array_like Input image. sigma: float, optional. standard deviation of Gaussian kernel. (See skimage.feature.structure_tensor) Returns ------- output: StructureTensor2D Instance of structure tensor. """ image = np.asarray(image) if not (image.ndim == 2 or image.ndim == 3): raise ValueError('Image must be 2 or 3-dimensional!') image = image / abs(image.max()) if image.ndim == 3: t11, t12, t22 = ski.structure_tensor(image[:, :, 0], sigma=sigma, mode='constant', cval=0.0) else: t11, t12, t22 = ski.structure_tensor(image, sigma=sigma, mode='constant', cval=0.0) return cls(t11, t12, t22)
def test_structure_tensor_3d_rc_only(): cube = np.zeros((5, 5, 5)) with pytest.raises(ValueError): structure_tensor(cube, sigma=0.1, order='xy') A_elems_rc = structure_tensor(cube, sigma=0.1, order='rc') A_elems_none = structure_tensor(cube, sigma=0.1) assert_array_equal(A_elems_rc, A_elems_none)
def test_structure_tensor_orders(): square = np.zeros((5, 5)) square[2, 2] = 1 A_elems_default = structure_tensor(square, sigma=0.1) A_elems_xy = structure_tensor(square, sigma=0.1, order='xy') A_elems_rc = structure_tensor(square, sigma=0.1, order='rc') assert_array_equal(A_elems_rc, A_elems_default) assert_array_equal(A_elems_xy, A_elems_default[::-1])
def test_structure_tensor_orders(): square = np.zeros((5, 5)) square[2, 2] = 1 with expected_warnings(['the default order of the structure']): A_elems_default = structure_tensor(square, sigma=0.1) A_elems_xy = structure_tensor(square, sigma=0.1, order='xy') A_elems_rc = structure_tensor(square, sigma=0.1, order='rc') assert_array_equal(A_elems_xy, A_elems_default) assert_array_equal(A_elems_xy, A_elems_rc[::-1])
def get_feature_one(img, msk=None, para=para): chans, grade, w, items = para['chans'], para['grade'], para['w'], para[ 'items'] feats, titles = [], [] img = img.reshape(img.shape[:2] + (-1, )) if msk is None: msk = np.ones(img.shape[:2], dtype=np.bool) if chans is None: chans = range(img.shape[2]) for c in [chans] if isinstance(chans, int) else chans: if 'ori' in items: feats.append(img[:, :, c][msk].astype(np.float32)) titles.append('c%d_ori' % c) for o in range(grade): blurimg = ndimg.gaussian_filter(img[:, :, c], 2**o, output=np.float32) feat_sobel = sobel(blurimg)[msk] if 'sob' in items else None if 'eig' in items: Axx, Axy, Ayy = structure_tensor(blurimg, w) l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy) feat_l1, feat_l2 = l1[msk], l2[msk] else: feat_l1 = feat_l2 = None feat_gauss = blurimg[msk] if 'blr' in items else None featcr = [feat_gauss, feat_sobel, feat_l1, feat_l2] title = [ 'c%d_s%d_%s' % (c, o, i) for i in ['gauss', 'sobel', 'l1', 'l2'] ] titles.extend( [title[i] for i in range(4) if not featcr[i] is None]) feats.extend( [featcr[i] for i in range(4) if not featcr[i] is None]) return np.array(feats).T, titles
def local_shape_features_minmax(im, scaleStart): """ Creates features based on those in the Luca Fiaschi paper but on 5 scales independent of object size, but using a gaussian pyramid to calculate at multiple scales more efficiently and then linear upsampling to original image scale also includes gaussian smoothed image """ # Smoothing and scale parameters chosen to approximate those in 'fine' # features imSizeC = im.shape[0] imSizeR = im.shape[1] f = np.zeros((imSizeC, imSizeR, 31)) f[:, :, 0] = im # create pyramid structure pyr = skimage.transform.pyramid_gaussian(im, sigma=1.5, max_layer=5, downscale=2, multichannel=False) a = im for layer in range(0, 5): # calculate scale scale = [ float(im.shape[0]) / float(a.shape[0]), float(im.shape[1]) / float(a.shape[1]) ] # create features lap = scipy.ndimage.filters.laplace(a) [m, n] = np.gradient(a) ggm = np.hypot(m, n) x, y, z = skfeat.structure_tensor(a, 1) st = skfeat.structure_tensor_eigvals(x, y, z) maxim = scipy.ndimage.maximum_filter(a, 3) minim = scipy.ndimage.minimum_filter(a, 3) # upsample features to original image lap = scipy.ndimage.interpolation.zoom(lap, scale, order=1) ggm = scipy.ndimage.interpolation.zoom(ggm, scale, order=1) st0 = scipy.ndimage.interpolation.zoom(st[0], scale, order=1) st1 = scipy.ndimage.interpolation.zoom(st[1], scale, order=1) maxim = scipy.ndimage.interpolation.zoom(maxim, scale, order=1) minim = scipy.ndimage.interpolation.zoom(minim, scale, order=1) f[:, :, layer * 6 + 1] = lap f[:, :, layer * 6 + 2] = ggm f[:, :, layer * 6 + 3] = st0 f[:, :, layer * 6 + 4] = st1 f[:, :, layer * 6 + 5] = minim f[:, :, layer * 6 + 6] = maxim # get next layer a = next(pyr) return f
def structure_2d_filter(image): """Computes the structure tensor around each pixel of an image. For 3D compatibability, see here: https://github.com/scikit-image/scikit-image/issues/2972 Keyword arguments: image -- The image to which the filter will be applied Returns: list(ndarray): a list of images that is the same size as the original image representing the largest and smallest eigenvalues of the structure tensor at each pixel of the original image. """ structure_tensor = feature.structure_tensor(image, sigma=1, mode="constant", cval=0) largest_eig_image = np.zeros(shape=image.shape) smallest_eig_image = np.zeros(shape=image.shape) for row in range(structure_tensor[0].shape[0]): for col in range(structure_tensor[0].shape[1]): Axx = structure_tensor[0][row, col] Axy = structure_tensor[1][row, col] Ayy = structure_tensor[2][row, col] eigs = np.linalg.eigvals([[Axx, Axy], [Axy, Ayy]]) largest_eig_image[row, col] = np.max(eigs) smallest_eig_image[row, col] = np.min(eigs) return [largest_eig_image, smallest_eig_image]
def estimateSeismicAttributes(data, dt, strucSigma=10, stftWinLen=51, stftWinOver=50): # calculate structural tensor of stacked seismic data seisStructTensX, seisStructTensXY, seisStructTensY = skimfeat.structure_tensor( data, sigma=strucSigma) # I can do better but this will have to do for now. cf = np.zeros([nT, nCDP]) cfsig = np.zeros([nT, nCDP]) for i in range(nCDP): freqs, stftTimes, dataTimeFreq = scipysig.stft(data[:, i], fs=1000.0 / dt, nperseg=stftWinLen, noverlap=stftWinOver) # calculate centroid frequency and centroid bandwidth and store in array for j in range(len(stftTimes)): sumSpecs = np.sum(np.abs(dataTimeFreq[:, j])) cf[j, i] = np.sum(freqs * np.abs(dataTimeFreq[:, j])) / sumSpecs cfsig[j, i] = (np.sum( (freqs - cf[j, i])**2 * np.abs(dataTimeFreq[:, j])) / sumSpecs)**0.5 # output progress if np.mod(i, 100) == 0: print('%i traces of %i total transformed' % (i, nCDP)) # form envelope of signal envelope = np.abs(scipysig.hilbert(data, axis=0)) return seisStructTensX, seisStructTensXY, seisStructTensY, cf, cfsig, envelope
def test_structure_tensor_eigenvalues_3d(): image = np.pad(cube(9), 5) * 1000 boundary = (np.pad(cube(9), 5) - np.pad(cube(7), 6)).astype(bool) A_elems = structure_tensor(image, sigma=0.1) e0, e1, e2 = structure_tensor_eigenvalues(A_elems) # e0 should detect facets assert np.all(e0[boundary] != 0)
def test_structure_tensor_eigvals(): square = np.zeros((5, 5)) square[2, 2] = 1 A_elems = structure_tensor(square, sigma=0.1, order='rc') with expected_warnings(['structure_tensor_eigvals is deprecated']): eigvals = structure_tensor_eigvals(*A_elems) eigenvalues = structure_tensor_eigenvalues(A_elems) assert_array_equal(eigvals, eigenvalues)
def calc_orientation_tensor(patch): Axx, Axy, Ayy = feature.structure_tensor(patch, sigma=0.1) tensor_vals = np.array([[np.mean(Axx), np.mean(Axy)], [np.mean(Axy), np.mean(Ayy)]]) w, v = np.linalg.eig(tensor_vals) orientation = math.atan2(*v[:, np.argmax(w)]) y0, x0 = patch.shape[0] / 2, patch.shape[1] / 2 return orientation, y0, x0
def run(self, ips, snap, img, para=None): axx, axy, ayy = structure_tensor(snap, sigma=para['sigma']) l1, l2 = structure_tensor_eigvals(axx, axy, ayy) if para['axis'] == 'major': rst = l1 elif para['axis'] == 'minor': rst = l2 else: rst = (l1**2 + l2**2)**0.5 if para['log']: rst += 1 np.log(rst, out=rst) img[:] = scale(rst, ips.range[0], ips.range[1])
def calculate_skfeat_eigenvalues(im, s): #designed to be similar to vigra use of vigra.filters.structureTensorEigenvalues #slight differences in results gim = scipy.ndimage.filters.gaussian_filter(im, 0.5 * s, mode='reflect', truncate=4) x, y, z = skfeat.structure_tensor(gim, 1 * s, mode='reflect') st_skfeat = skfeat.structure_tensor_eigvals(x, y, z) return ((st_skfeat[0] / (50 + s)), (st_skfeat[1] / (50 + s)))
def apply_filters(imDsets, lblDsets, sigma, gauss, gaussLaplace, gaussGradientMagnitude, structTensor, hessEigenvalues): for i in range(3): for j in range(len(sigma)): gauss.append(ndi.gaussian_filter(imDsets[i], sigma[j])) gaussLaplace.append(ndi.gaussian_laplace(imDsets[i], sigma[j])) gaussGradientMagnitude.append( ndi.gaussian_gradient_magnitude(imDsets[i], sigma[j])) structTensor.append(feat.structure_tensor(imDsets[i], sigma[j])) hessianMat = feat.hessian_matrix(imDsets[i], sigma[j])
def test_structure_tensor_eigvals(): square = np.zeros((5, 5)) square[2, 2] = 1 Axx, Axy, Ayy = structure_tensor(square, sigma=0.1) l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy) assert_array_equal( l1, np.array([[0, 0, 0, 0, 0], [0, 2, 4, 2, 0], [0, 4, 0, 4, 0], [0, 2, 4, 2, 0], [0, 0, 0, 0, 0]]) ) assert_array_equal( l2, np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]) )
def test_structure_tensor_eigenvalues(): square = np.zeros((5, 5)) square[2, 2] = 1 A_elems = structure_tensor(square, sigma=0.1, order='rc') l1, l2 = structure_tensor_eigenvalues(A_elems) assert_array_equal( l1, np.array([[0, 0, 0, 0, 0], [0, 2, 4, 2, 0], [0, 4, 0, 4, 0], [0, 2, 4, 2, 0], [0, 0, 0, 0, 0]])) assert_array_equal( l2, np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]))
def test_structure_tensor_eigvals(): square = np.zeros((5, 5)) square[2, 2] = 1 Axx, Axy, Ayy = structure_tensor(square, sigma=0.1) l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy) assert_array_equal( l1, np.array([[0, 0, 0, 0, 0], [0, 2, 4, 2, 0], [0, 4, 0, 4, 0], [0, 2, 4, 2, 0], [0, 0, 0, 0, 0]])) assert_array_equal( l2, np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]))
def test_structure_tensor(): square = np.zeros((5, 5)) square[2, 2] = 1 Axx, Axy, Ayy = structure_tensor(square, sigma=0.1) assert_array_equal( Axx, np.array([[0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 4, 0, 4, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0]]) ) assert_array_equal( Axy, np.array([[0, 0, 0, 0, 0], [0, 1, 0, -1, 0], [0, 0, 0, -0, 0], [0, -1, -0, 1, 0], [0, 0, 0, 0, 0]]) ) assert_array_equal( Ayy, np.array([[0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0]]) )
def test_structure_tensor_sigma(ndim): img = np.zeros((5, ) * ndim) img[[2] * ndim] = 1 A_default = structure_tensor(img, sigma=0.1, order='rc') A_tuple = structure_tensor(img, sigma=(0.1, ) * ndim, order='rc') A_list = structure_tensor(img, sigma=[0.1] * ndim, order='rc') assert_array_equal(A_tuple, A_default) assert_array_equal(A_list, A_default) with pytest.raises(ValueError): structure_tensor(img, sigma=(0.1, ) * (ndim - 1), order='rc') with pytest.raises(ValueError): structure_tensor(img, sigma=[0.1] * (ndim + 1), order='rc')
def read_image(self, image_name, size=None): options = self.get_options() if size: im = np.array(Image.open(image_name).convert("L").resize(size)) else: im = np.array(Image.open(image_name).convert("L")) options["image"] = im Axx, Axy, Ayy = structure_tensor(im) feature = structure_tensor_eigvals(Axx, Axy, Ayy)[0] # plt.imshow(feature) # plt.show() return feature.reshape((1, feature.shape[0] * feature.shape[1]))[0]
def test_structure_tensor_eigenvalues(dtype): square = np.zeros((5, 5), dtype=dtype) square[2, 2] = 1 A_elems = structure_tensor(square, sigma=0.1, order='rc') l1, l2 = structure_tensor_eigenvalues(A_elems) out_dtype = _supported_float_type(dtype) assert all(a.dtype == out_dtype for a in (l1, l2)) assert_array_equal( l1, np.array([[0, 0, 0, 0, 0], [0, 2, 4, 2, 0], [0, 4, 0, 4, 0], [0, 2, 4, 2, 0], [0, 0, 0, 0, 0]])) assert_array_equal( l2, np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]))
def stru(im): """Attempts to find edges in the image through use of structure tensor. Computes a mask of values that are higher mostly on edges by using the square difference of the structure tensor eigen values. Args: im: input greyscale image Returns: mask of values higher on edges than interiors (see above) """ A = structure_tensor(im, sigma = 0.8, mode="reflect") e1, e2 = structure_tensor_eigvals(*A) d = e1 + e2 st = np.where(d > np.finfo(np.float16).eps, (e1 - e2) ** 2, 0) return st[..., np.newaxis]
def test_structure_tensor(): square = np.zeros((5, 5)) square[2, 2] = 1 Axx, Axy, Ayy = structure_tensor(square, sigma=0.1) assert_array_equal( Axx, np.array([[0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 4, 0, 4, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( Axy, np.array([[0, 0, 0, 0, 0], [0, 1, 0, -1, 0], [0, 0, 0, -0, 0], [0, -1, -0, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( Ayy, np.array([[0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0]]))
def test_structure_tensor(): square = np.zeros((5, 5)) square[2, 2] = 1 Arr, Arc, Acc = structure_tensor(square, sigma=0.1, order='rc') assert_array_equal( Acc, np.array([[0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 4, 0, 4, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( Arc, np.array([[0, 0, 0, 0, 0], [0, 1, 0, -1, 0], [0, 0, 0, -0, 0], [0, -1, -0, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( Arr, np.array([[0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0]]))
def diffusion_tensor(image, sigma=10.0, weighted=False): """ Compute spatially variant diffusion tensor. For a given image I, the structure tensor is given by | Ixx Ixy | | Ixy Iyy | with eigenvectors u and v. Eigenvectors v correspond to the smaller eigenvalue and point in directions of maximum coherence. The diffusion tensor D is given by, D = v * transpose(v). This tensor is designed for anisotropic smoothing. Local D tensors are singular. Parameters ---------- image: array_like Input image. sigma: float, optional Standard deviation of Gaussian kernel. (See skimage.feature.structure_tensor) weighted: boolean, optional Defines how eigenvalues of diffusion tensor are computed. weighted=False does not produce isotropic diffusion tensors. Returns ------- d11, d12, d22: ndarray Independent components of diffusion tensor. """ image = np.asarray(image) if image.ndim != 2: image = np.asarray(image) # normalize image image = image / abs(image.max()) # Compute gradient of scalar field using derivative filter Ixx, Ixy, Iyy = ski.structure_tensor(image, sigma=sigma, mode='nearest', cval=0.0) d11, d12, d22 = _get_diffusion_tensor(Ixx, Ixy, Iyy, weighted=weighted) return d11, d12, d22
def test_structure_tensor_3d(): cube = np.zeros((5, 5, 5)) cube[2, 2, 2] = 1 A_elems = structure_tensor(cube, sigma=0.1) assert_equal(len(A_elems), 6) assert_array_equal( A_elems[0][:, 1, :], np.array([[0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( A_elems[0][1], np.array([[0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 4, 16, 4, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( A_elems[3][2], np.array([[0, 0, 0, 0, 0], [0, 4, 16, 4, 0], [0, 0, 0, 0, 0], [0, 4, 16, 4, 0], [0, 0, 0, 0, 0]]))
def _get_structure_tensor_features(im, features_window_size, sigma_st): features_arr = np.empty(0) assert im.shape[0] == im.shape[1], "image must have a square shape" center = im_center(im) Axx, Axy, Ayy = feature.structure_tensor(im, sigma=sigma_st) l1, l2 = feature.structure_tensor_eigvals(Axx, Axy, Ayy) l1 = utils.extract_pad_image(input_img=l1, pt=center, window_size=features_window_size) l2 = utils.extract_pad_image(input_img=l2, pt=center, window_size=features_window_size) l1 = arr_to_vec(l1) l2 = arr_to_vec(l2) features_arr = np.append(features_arr, l1) features_arr = np.append(features_arr, l2) return features_arr
def local_shape_features_fine_imhist(im, scaleStart): """As per pyramid features but with histogram equalisation""" imSizeC = im.shape[0] imSizeR = im.shape[1] f = np.zeros((imSizeC, imSizeR, 26)) im = exposure.equalize_hist(im) #im=exposure.equalize_adapthist(im, kernel_size=5) f[:, :, 0] = im # set up pyramid pyr = skimage.transform.pyramid_gaussian(im, sigma=1.5, max_layer=5, downscale=2, multichannel=False) a = im for layer in range(0, 5): scale = [ float(im.shape[0]) / float(a.shape[0]), float(im.shape[1]) / float(a.shape[1]) ] lap = scipy.ndimage.filters.laplace(a) [m, n] = np.gradient(a) ggm = np.hypot(m, n) x, y, z = skfeat.structure_tensor(a, 1) st = skfeat.structure_tensor_eigvals(x, y, z) lap = scipy.ndimage.interpolation.zoom(lap, scale, order=1) ggm = scipy.ndimage.interpolation.zoom(ggm, scale, order=1) st0 = scipy.ndimage.interpolation.zoom(st[0], scale, order=1) st1 = scipy.ndimage.interpolation.zoom(st[1], scale, order=1) up = scipy.ndimage.interpolation.zoom(a, scale, order=1) f[:, :, layer * 5 + 1] = lap f[:, :, layer * 5 + 2] = ggm f[:, :, layer * 5 + 3] = st0 f[:, :, layer * 5 + 4] = st1 f[:, :, layer * 5 + 5] = up a = next(pyr) return f
def create_features(p, x, y, r): """Create feature for patch Tested features: + Mean intensity of RGB CIE[:,:,0] + Mean intensity of RGB CIE[:,:,1] + Mean intensity of RGB CIE[:,:,2] + Mean intensity of HSV[:,:,0] + Canny edge image + Eigenvalues from structure tensor """ features = [] # Get patch/patches structure_img = p.get_version("structure")[:, :, 2] structure_patch = structure_img[int(y - r):int(y + r), int(x - r):int(x + r)] rgb_img = p.get_version("contrast_rgb_cie") rgb_patch = rgb_img[int(y - r):int(y + r), int(x - r):int(x + r)] # Color quantization with k_means # patch = quantize_colors(patch) # ==== FEATURES ======= # Median values of the individual color channels features.append(np.median(rgb_patch[:, :, 0])) features.append(np.median(rgb_patch[:, :, 1])) features.append(np.median(rgb_patch[:, :, 2])) # Edges edges = canny(structure_patch) features += edges.astype(float).ravel().tolist() # Structure Tensor Axx, Axy, Ayy = structure_tensor(structure_patch) l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy) features += l1.ravel().tolist() features += l2.ravel().tolist() return features
def test_structure_tensor(dtype): square = np.zeros((5, 5), dtype=dtype) square[2, 2] = 1 Arr, Arc, Acc = structure_tensor(square, sigma=0.1, order='rc') out_dtype = _supported_float_type(dtype) assert all(a.dtype == out_dtype for a in (Arr, Arc, Acc)) assert_array_equal( Acc, np.array([[0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 4, 0, 4, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( Arc, np.array([[0, 0, 0, 0, 0], [0, 1, 0, -1, 0], [0, 0, 0, -0, 0], [0, -1, -0, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( Arr, np.array([[0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0]]))
def test_structure_tensor_3d(dtype): cube = np.zeros((5, 5, 5), dtype=dtype) cube[2, 2, 2] = 1 A_elems = structure_tensor(cube, sigma=0.1) assert all(a.dtype == _supported_float_type(dtype) for a in A_elems) assert_equal(len(A_elems), 6) assert_array_equal( A_elems[0][:, 1, :], np.array([[0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( A_elems[0][1], np.array([[0, 0, 0, 0, 0], [0, 1, 4, 1, 0], [0, 4, 16, 4, 0], [0, 1, 4, 1, 0], [0, 0, 0, 0, 0]])) assert_array_equal( A_elems[3][2], np.array([[0, 0, 0, 0, 0], [0, 4, 16, 4, 0], [0, 0, 0, 0, 0], [0, 4, 16, 4, 0], [0, 0, 0, 0, 0]]))
def create_features(p, x, y, r): """Create feature for patch Tested features: + Mean intensity of RGB CIE[:,:,0] + Mean intensity of RGB CIE[:,:,1] + Mean intensity of RGB CIE[:,:,2] + Mean intensity of HSV[:,:,0] + Canny edge image + Eigenvalues from structure tensor """ features = [] # Get patch/patches structure_img = p.get_version("structure")[:,:,2] structure_patch = structure_img[int(y-r):int(y+r), int(x-r):int(x+r)] rgb_img = p.get_version("contrast_rgb_cie") rgb_patch = rgb_img[int(y-r):int(y+r), int(x-r):int(x+r)] # Color quantization with k_means # patch = quantize_colors(patch) # ==== FEATURES ======= # Median values of the individual color channels features.append(np.median(rgb_patch[:,:,0])) features.append(np.median(rgb_patch[:,:,1])) features.append(np.median(rgb_patch[:,:,2])) # Edges edges = canny(structure_patch) features += edges.astype(float).ravel().tolist() # Structure Tensor Axx, Axy, Ayy = structure_tensor(structure_patch) l1, l2 = structure_tensor_eigvals(Axx, Axy, Ayy) features += l1.ravel().tolist() features += l2.ravel().tolist() return features
def _suppress_lines(feature_mask, image, sigma, line_threshold): Axx, Axy, Ayy = structure_tensor(image, sigma) feature_mask[(Axx + Ayy) ** 2 > line_threshold * (Axx * Ayy - Axy ** 2)] = False