def test_zeros(): zeros = np.zeros((64,64), np.uint8) feats = texture.haralick(zeros) assert not np.any(np.isnan(feats)) feats = texture.haralick(zeros, ignore_zeros=True) assert not np.any(np.isnan(feats))
def test_haralick3d(): np.random.seed(22) img = mahotas.stretch(255 * np.random.rand(20, 20, 4)) features = texture.haralick(img) assert features.shape == (13, 13) features = texture.haralick(img[:, :, 0]) assert features.shape == (4, 13)
def test_haralick3d(): np.random.seed(22) img = mahotas.stretch(255*np.random.rand(20,20,4)) features = texture.haralick(img) assert features.shape == (13,13) features = texture.haralick(img[:,:,0]) assert features.shape == (4,13)
def test_haralick3d(): np.random.seed(22) img = mahotas.stretch(255*np.random.rand(20,20,4)) features = texture.haralick(img) assert features.shape == (13,13) features = texture.haralick(img[:,:,0]) assert features.shape == (4,13) features = texture.haralick(img.max(0), ignore_zeros=True, preserve_haralick_bug=True, compute_14th_feature=True) assert features.shape == (4,14)
def compute_fluor_info(seg, fluor_img): rps = regionprops(seg) mean_intensity = np.zeros((len(rps))) std_intensity = np.zeros((len(rps))) intensity_range = np.zeros((len(rps))) fluor_haralick = [] for i in range(len(rps)): cell_num = int(i + 1) r = rps[i] cell_mask = (seg == cell_num) region_cell_mask = cell_mask[r.bbox[0]:r.bbox[2], r.bbox[1]:r.bbox[3]] crop_img = fluor_img[r.bbox[0]:r.bbox[2], r.bbox[1]:r.bbox[3]] cell_img = (fluor_img * cell_mask)[r.bbox[0]:r.bbox[2], r.bbox[1]:r.bbox[3]] mean_intensity[i] = np.sum(cell_img) * 1.0 / r.area std_intensity[i] = np.std(cell_img[region_cell_mask]) min_value, max_value = np.amin(cell_img[region_cell_mask]), np.amax( cell_img[region_cell_mask]) min_value = min_value - 1 intensity_range[i] = max_value - min_value #the haralick features have four directions, to meet rotation invariance,use average for each feature fl_hara = np.mean(mht.haralick(cell_img, ignore_zeros=True), axis=0) fluor_haralick.append(fl_hara) fluor_haralick = np.array(fluor_haralick) return mean_intensity, std_intensity, intensity_range, fluor_haralick
def _calculateStatistics(self, img, haralick=False, zernike=False): result = [] # 3-bin histogram result.extend(mquantiles(img)) # First four moments result.extend([img.mean(), img.var(), skew(img, axis=None), kurtosis(img, axis=None)]) # Haralick features if haralick: integerImage = dtype.img_as_ubyte(img) result.extend(texture.haralick(integerImage).flatten()) # Zernike moments if zernike: result.extend(zernike_moments(img, int(self.rows) / 2 + 1)) return result
def _calculateStatistics(self, img, haralick=False, zernike=False): result = [] #3-bin histogram result.extend(mquantiles(img)) #First four moments result.extend([ img.mean(), img.var(), skew(img, axis=None), kurtosis(img, axis=None) ]) #Haralick features if haralick: integerImage = dtype.img_as_ubyte(img) result.extend(texture.haralick(integerImage).flatten()) #Zernike moments if zernike: result.extend(zernike_moments(img, int(self.rows) / 2 + 1)) return result
def _compute_haralick_features(patch, distance): """Compute the haralick feature. This function is used for parallel processing. Parameters ---------- patch: ndarray, (patch_size) The patch to consider to compute the cooccurence matrix. distance: int, The distance to use to compute the cooccurence matrix. Returns ------- haralick_features: ndarray, shape (4, 13) The haralick features. """ return haralick(patch, distance=distance)
def img_feature_extraction(self, img): # Change CHW to HWC HWC_img = img_as_float(img.permute(1, 2, 0)) segments_slic = slic(HWC_img, n_segments=20, compactness=10, sigma=1, start_label=1) unique_lables = np.unique(segments_slic) if 0 in unique_lables: unique_lables = np.delete(unique_lables, 0) N = len(unique_lables) image = np.zeros((N, 19)) # image = np.zeros((N, 6)) regions = measure.regionprops(segments_slic, intensity_image=HWC_img) for (i, segVal) in enumerate(unique_lables): # convert image to grayscale ubyte_img = img_as_ubyte(HWC_img) ubyte_img[ubyte_img == 0] = 1 gray = cv2.cvtColor(ubyte_img, cv2.COLOR_BGR2GRAY) # construct a mask for the segment mask = np.zeros(gray.shape, dtype=np.bool) mask[segments_slic != segVal] = True gray[mask] = 0 # extract texture feature from mahotas package texture_features = texture.haralick(gray, ignore_zeros=True) ht_mean = texture_features.mean(axis=0) # extract mean_intensity feature mean_intensity = regions[i].mean_intensity # extract centroid feature centroid = regions[i].centroid #extract area feature area = regions[i].area image[i] = np.hstack((mean_intensity,centroid, area, ht_mean)) # image[i] = np.hstack((mean_intensity, centroid, area)) return image
def test_4d_image(): texture.haralick(np.arange(4**5).reshape((4,4,4,4,4)))
def test_ignore_zeros_raise(): zeros = np.zeros((64,64), np.uint8) texture.haralick(zeros, ignore_zeros=True)
regions = measure.regionprops(segments_slic, intensity_image=img) print(len(regions)) if 0 in c: c = np.delete(c, 0) for (i, segVal) in enumerate(c): # construct a mask for the segment print(i) new = img_as_ubyte(img) new[new == 0] = 1 gray = cv2.cvtColor(new, cv2.COLOR_BGR2GRAY) # print(i) # print(new) mask = np.zeros(gray.shape, dtype=np.bool) mask[segments_slic != segVal] = True gray[mask] = 0 features = texture.haralick(gray, ignore_zeros=True) ht_mean = features.mean(axis=0) a = np.hstack((regions[i].mean_intensity, regions[i].centroid, ht_mean)) print(regions[i].mean_intensity) # print(regions[i].centroid) # print(ht_mean) # print(a.shape) fig, ax = plt.subplots(2, 2, figsize=(10, 10), sharex=True, sharey=True) ax[0, 1].imshow(mark_boundaries(img, segments_slic)) ax[0, 1].set_title('SLIC') for a in ax.ravel(): a.set_axis_off()
def test_float_haralick(): A = np.zeros((5,5), np.float32) A[2,2]=12 texture.haralick(A)
def test_haralick3(): np.random.seed(123) f = np.random.rand(34, 12, 8) f = (f * 255).astype(np.int32) feats = texture.haralick(f) assert not np.any(np.isnan(feats))
def test_ignore_zeros_raise(): zeros = np.zeros((64, 64), np.uint8) texture.haralick(zeros, ignore_zeros=True)
def test_4d_image(): with pytest.raises(ValueError): texture.haralick(np.arange(4**5).reshape((4, 4, 4, 4, 4)))
def test_ignore_zeros_raise(): zeros = np.zeros((64, 64), np.uint8) with pytest.raises(ValueError): texture.haralick(zeros, ignore_zeros=True)
def test_float_haralick(): A = np.zeros((5, 5), np.float32) A[2, 2] = 12 with pytest.raises(TypeError): texture.haralick(A)