def eccentricity(im): ''' im should be a binary image with a single region ''' c = mahotas.center_of_mass(im) m11 = mahotas.moments(im, 1, 1, c) m02 = mahotas.moments(im, 2, 0, c) m20 = mahotas.moments(im, 0, 2, c) l1 = (m20 + m02) / 2 + np.sqrt(4*m11**2 + (m20-m02)**2) / 2 l2 = (m20 + m02) / 2 - np.sqrt(4*m11**2 + (m20-m02)**2) / 2 e = np.sqrt(1 - l2 / l1) return e
def _calc_orientation(img): com = mh.center_of_mass(img) mu00 = mh.moments(img, 0, 0, com) mu11 = mh.moments(img, 1, 1, com) mu20 = mh.moments(img, 2, 0, com) mu02 = mh.moments(img, 0, 2, com) mup_20 = mu20 / mu00 mup_02 = mu02 / mu00 mup_11 = mu11 / mu00 theta_rad = 0.5 * math.atan(2 * mup_11 / (mup_20 - mup_02)) # todo math -> numpy theta = theta_rad * (180 / math.pi) if (mup_20 - mup_02) > 0: theta += 90 return theta
def test_expression(): import mahotas as mh import numpy as np im = (np.zeros((12, 16)) + np.arange(16)) im -= im.mean() c0, c1 = 4, 7 p0, p1 = 2, 3 for p0, p1 in [(0, 1), (1, 0), (1, 2), (3, 2), (2, 0)]: sum_r = sum((im[i, j] * (i - c0)**p0 * (j - c1)**p1) for i in range(im.shape[0]) for j in range(im.shape[1])) mm_r = mh.moments(im, p0, p1, cm=(c0, c1)) assert np.abs(mm_r - sum_r) < 1.
def _process_image(self, im): thresh = skimage.filters.threshold_otsu(im) labeled, seeds = mahotas.label(im > thresh) labeled = mahotas.labeled.remove_bordering(labeled) features = [] for seed in range(1, seeds): precipitates = (labeled == seed).astype('int') m00 = mahotas.moments(precipitates, 0, 0) m01 = mahotas.moments(precipitates, 0, 1) m10 = mahotas.moments(precipitates, 1, 0) m11 = mahotas.moments(precipitates, 1, 1) m02 = mahotas.moments(precipitates, 0, 2) m20 = mahotas.moments(precipitates, 2, 0) xm = m10 / m00 ym = m01 / m00 u00 = 1.0 u11 = m11 / m00 - xm * ym u20 = m20 / m00 - xm ** 2 u02 = m02 / m00 - ym ** 2 w1 = u00**2 / (2.0 * numpy.pi * (u20 + u02)) w2 = u00**4 / (16.0 * numpy.pi * numpy.pi * (u20 * u02 - u11**2)) if numpy.isnan(w1) or numpy.isnan(w2) or numpy.isinf(w1) or numpy.isinf(w2): continue features.append(((xm, ym), [numpy.sqrt(m00), w1, w2, u20, u02]))# return features
def test_expression(): import mahotas as mh import numpy as np im = (np.zeros((12,16)) + np.arange(16)) im -= im.mean() c0,c1 = 4,7 p0,p1 = 2, 3 for p0,p1 in [(0,1), (1,0), (1,2), (3,2), (2,0)]: sum_r = sum((im[i,j] * (i - c0)**p0 * (j - c1)**p1) for i in range(im.shape[0]) for j in range(im.shape[1])) mm_r = mh.moments(im , p0, p1, cm=(c0,c1)) assert np.abs(mm_r - sum_r) < 1.
def moment_feats(im): thresh = skimage.filters.threshold_otsu(im) #plt.imshow(im > thresh) #plt.show() labeled, seeds = mahotas.label(im > thresh) labeled = mahotas.labeled.remove_bordering(labeled) features = [] for seed in range(1, seeds): precipitates = (labeled == seed).astype('int') m00 = mahotas.moments(precipitates, 0, 0) m01 = mahotas.moments(precipitates, 0, 1) m10 = mahotas.moments(precipitates, 1, 0) m11 = mahotas.moments(precipitates, 1, 1) m02 = mahotas.moments(precipitates, 0, 2) m20 = mahotas.moments(precipitates, 2, 0) #m12 = mahotas.moments(precipitates, 1, 2) #m21 = mahotas.moments(precipitates, 2, 1) #m22 = mahotas.moments(precipitates, 2, 2) #plt.imshow(precipitates) #plt.show() xm = m10 / m00 ym = m01 / m00 #if m00 < 1e-5: # continue #u00 = m00 u00 = 1.0 u11 = m11 / m00 - xm * ym u20 = m20 / m00 - xm ** 2 u02 = m02 / m00 - ym ** 2 w1 = u00**2 / (2.0 * numpy.pi * (u20 + u02)) w2 = u00**4 / (16.0 * numpy.pi * numpy.pi * (u20 * u02 - u11**2)) if w1 < 0.0 or w1 > 1.0 or w2 < 0.0 or w2 > 1.0 or numpy.isnan(w1) or numpy.isnan(w2): continue print m00, m01, m10, m11, m02, m20 print xm print ym print u00, u11, u20, u02 print w1, w2 1/0 features.append([numpy.sqrt(m00)])#w1, w2, , u20, u02 features = numpy.array(features) return features
def get_mahotas_features(image, bin_image): """ """ # pdb.set_trace() zer = zernike.zernike_moments(image, len(image)/2) #adding 25 mm = [] for i in range(7): for j in range(7): mm.append(np.log10(moments(image, i,j))) #adding 49 rou = features.roundness(bin_image) #adding 1 el1 = features.ellipse_axes(bin_image)[0] el2 = features.ellipse_axes(bin_image)[0]#adding 2 exe = features.eccentricity(bin_image) #adding 1 pft = features.pftas(image) #adding 54 hara = mahotas.features.haralick(image).reshape(-1) #adding 52 # return np.concatenate((zer, np.array([rou]), np.array(ellip), hara.reshape(-1), lbps.reshape(-1))) #adding in total 184 features return np.concatenate((zer, np.array(mm), np.array([rou, el1, el2, exe]), pft, hara))
def test_moments01(): im = np.zeros((16,16)) im += np.arange(16) im -= im.mean() assert np.abs(mh.moments(im , 1, 0)) < 0.1
def test_moments01(): im = np.zeros((16, 16)) im += np.arange(16) im -= im.mean() assert np.abs(mh.moments(im, 1, 0)) < 0.1