def iagradm(f, Bdil=None, Bero=None): from ia870 import iasubm,iadil,iaero,iasecross if Bdil is None: Bdil = iasecross() if Bero is None: Bero = iasecross() y = iasubm( iadil(f,Bdil),iaero(f,Bero)) return y
def iasedisk(r=3, DIM="2D", METRIC="EUCLIDEAN", FLAT="FLAT", h=0): from ia870 import iabinary, iasecross, iasedil, iasesum from ia870 import iasebox, iaseintersec, iagray METRIC = METRIC.upper() FLAT = FLAT.upper() assert DIM == '2D', 'Supports only 2D structuring elements' if FLAT == 'FLAT': y = iabinary([1]) else: y = int32([h]) if r == 0: return y if METRIC == 'CITY-BLOCK': if FLAT == 'FLAT': b = iasecross(1) else: b = int32([[-2147483647, 0, -2147483647], [0, 1, 0], [-2147483647, 0, -2147483647]]) return iasedil(y, iasesum(b, r)) elif METRIC == 'CHESSBOARD': if FLAT == 'FLAT': b = iasebox(1) else: b = int32([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) return iasedil(y, iasesum(b, r)) elif METRIC == 'OCTAGON': if FLAT == 'FLAT': b1, b2 = iasebox(1), iasecross(1) else: b1 = int32([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) b2 = int32([[-2147483647, 0, -2147483647], [0, 1, 0], [-2147483647, 0, -2147483647]]) if r == 1: return b1 else: return iasedil(iasedil(y, iasesum(b1, r // 2)), iasesum(b2, (r + 1) // 2)) elif METRIC == 'EUCLIDEAN': v = arange(-r, r + 1) x = resize(v, (len(v), len(v))) y = transpose(x) Be = iabinary(sqrt(x * x + y * y) <= (r + 0.5)) if FLAT == 'FLAT': return Be be = h + int32( sqrt(maximum((r + 0.5) * (r + 0.5) - (x * x) - (y * y), 0))) be = iaintersec(iagray(Be, 'int32'), be) return be else: assert 0, 'Non valid metric' return B
def iadil(f, b=None): from ia870 import iamat2set, ialimits, iaisbinary from ia870 import iaintersec, iagray, iaadd4dil, iasecross if b is None: b = iasecross() if len(f.shape) == 1: f = f[newaxis, :] h, w = f.shape x, v = iamat2set(b) if len(x) == 0: y = (ones((h, w)) * ialimits(f)[0]).astype(f.dtype) else: if iaisbinary(v): v = iaintersec(iagray(v, 'int32'), 0) mh, mw = max(abs(x)[:, 0]), max(abs(x)[:, 1]) y = (ones((h + 2 * mh, w + 2 * mw)) * ialimits(f)[0]).astype(f.dtype) for i in range(x.shape[0]): if v[i] > -2147483647: y[mh + x[i, 0]:mh + x[i, 0] + h, mw + x[i, 1]:mw + x[i, 1] + w] = maximum( y[mh + x[i, 0]:mh + x[i, 0] + h, mw + x[i, 1]:mw + x[i, 1] + w], iaadd4dil(f, v[i])) y = y[mh:mh + h, mw:mw + w] return y
def iabshow(f1, f2=None, f3=None, factor=17): from ia870 import iabinary, iaframe, iadil, iaunion from ia870 import iasedisk, iaserot, iasecross, iasesum assert f1.dtype == bool, 'f1 must be boolean image' factor = max(factor, 9) hfactor = factor // 2 if size(f1.shape) == 1: f1 = f1[newaxis, :] if f2 != None: f2 = f2[newaxis, :] if f3 != None: f3 = f3[newaxis, :] bz = zeros(factor * array(f1.shape)).astype(bool) b0 = asarray(bz) b0[hfactor::factor, hfactor::factor] = f1 fr1 = iaframe(zeros((factor, factor), bool)) fr1 = iadil(b0, fr1) if f2 is not None: assert f1.shape == f2.shape, 'f1 and f2 must have same shape' b1 = asarray(bz) b1[hfactor::factor, hfactor::factor] = f2 fr2 = iadil(b1, iasedisk(hfactor - 4)) fr1 = iaunion(fr1, fr2) if f3 is not None: assert f1.shape == f3.shape, 'f1 and f3 must have same shape' bz[hfactor::factor, hfactor::factor] = f3 fr3 = iadil(bz, iasesum(iaserot(iasecross(1), 45), hfactor - 1)) fr1 = iaunion(fr1, fr3) return fr1
def iaareaclose(f, a, Bc=None): from ia870 import ianeg,iaareaopen,iasecross if Bc is None: Bc = iasecross() y = ianeg( iaareaopen( ianeg(f),a,Bc)) return y
def iawatershed(f, Bc=iasecross(), option='LINES'): from ipdp import se2offset offset = se2offset(Bc) w = connectedComponents(f, offset) if option == 'LINES': w = iasubm(w, iaero(w)) w = iabinary(w) return w
def iaclose(f, b=None): from ia870 import iaero, iadil, iasecross if b is None: b = iasecross() y = iaero(iadil(f, b), b) return y
def iacwatershed(f, g, Bc=iasecross(), option='LINES'): from ia870.ipdp import se2offset if iaisbinary(g): g = ialabel(g, Bc) offset = se2offset(Bc) w = ift_m(f, offset, g) if option == 'LINES': w = iasubm(w, iaero(w)) w = iabinary(w) return w
def iacwatershed(f, g, Bc=iasecross(), option='LINES'): from ipdp import se2offset if iaisbinary(g): g = ialabel(g, Bc) offset = se2offset(Bc) w = ift_m(f, offset, g) if option == 'LINES': w = iasubm(w, iaero(w)) w = iabinary(w) return w
def iaero(f, b=None): from ia870 import ianeg, iadil, iasereflect, iasecross if b is None: b = iasecross() y = ianeg(iadil(ianeg(f), iasereflect(b))) return y
def iablob(fr, measurement, option="image"): measurement = upper(measurement) option = upper(option) if len(fr.shape) == 1: fr = fr[newaxis,:] n = fr.max() if option == 'DATA': y = [] elif measurement == 'CENTROID': y = np.zeros(fr.shape,bool) elif measurement == 'BOUNDINGBOX': y = np.zeros(fr.shape,bool) elif measurement == 'PERIMETER': y = np.zeros(fr.shape,np.int32) elif measurement == 'AREA': y = np.zeros(fr.shape,np.int32) else: y = np.zeros(fr.shape) if measurement == 'AREA': area = np.bincount(fr.ravel()) if option == 'DATA': y = area[1::] else: for i in xrange(1,n+1): y[fr==i] = area[i] elif measurement == 'CENTROID': for i in xrange(1,n+1): aux = (fr==i) xind,yind = np.nonzero(aux) area = len(xind) centroid = [xind.sum()/area,yind.sum()/area] if option == 'DATA': y.append([centroid[1],centroid[0]]) else : y[centroid[0],centroid[1]] = 1 elif measurement == 'BOUNDINGBOX': for i in xrange(1,n+1): aux = (fr==i) col, = np.nonzero(aux.any(0)) row, = np.nonzero(aux.any(1)) if option == 'DATA': y.append([col[0],row[0],col[-1],row[-1]]) else: y[row[0]:row[-1],col[0] ] = 1 y[row[0]:row[-1],col[-1]] = 1 y[row[0], col[0]:col[-1]] = 1 y[row[-1],col[0]:col[-1]] = 1 elif measurement == 'PERIMETER': Bc = ia870.iasecross() for i in xrange(1,n+1): aux = fr == i grad = aux - ia870.iaero(aux,Bc) if option == 'DATA': y.append(grad.sum()) else: y[aux] = grad.sum() elif measurement == 'CIRCULARITY': Bc = ia870.iasecross() area = np.bincount(fr.ravel()) perim = [] for i in xrange(1,n+1): aux = fr == i grad = aux - ia870.iaero(aux,Bc) perim.append(grad.sum()) if option != 'DATA': y[aux] = 4*np.pi*area[i]/(perim[i-1]**2) if option == 'DATA': perim = np.array(perim) y = 4*np.pi*area[1::]/(perim**2) elif measurement == 'ASPECTRATIO': for i in xrange(1,n+1): aux = (fr==i) col, = np.nonzero(aux.any(0)) row, = np.nonzero(aux.any(1)) if option == 'DATA': y.append(1.*(min(col[-1]-col[0],row[-1]-row[0])+1)/(max(col[-1]-col[0],row[-1]-row[0])+1)) else: y[aux] = 1.*(min(col[-1]-col[0],row[-1]-row[0])+1)/(max(col[-1]-col[0],row[-1]-row[0])+1) elif measurement == 'COMPACTNESS': for i in xrange(1,n+1): aux = (fr==i) xind,yind = np.nonzero(aux) area = len(xind) centroid = [xind.sum()/area,yind.sum()/area] m20 = ((1.*xind-centroid[0])**2).sum()/area m02 = ((1.*yind-centroid[1])**2).sum()/area compactness = area/(2*np.pi*(m20+m02)) if option == 'DATA': y.append(compactness) else : y[aux] = compactness elif measurement == 'ECCENTRICITY': for i in xrange(1,n+1): aux = (fr==i) xind,yind = np.nonzero(aux) area = len(xind) centroid = [xind.sum()/area,yind.sum()/area] m11 = ((1.*xind-centroid[0])*(yind-centroid[1])).sum()/area m20 = ((1.*xind-centroid[0])**2).sum()/area m02 = ((1.*yind-centroid[1])**2).sum()/area eccentricity = sqrt((m20-m02)**2+4*m11**2)/(m20+m02) if option == 'DATA': y.append(eccentricity) else : y[aux] = eccentricity else: print "Measurement option should be 'AREA','CENTROID', 'BOUNDINGBOX', 'PERIMETER', " print "'ASPECTRATIO', 'CIRCULARITY', 'COMPACTNESS' or 'ECCENTRICITY'." return np.array(y)
def iatz(f, Bc=iasecross()): from ipdp import se2offset offset = se2offset(Bc) return tieZone(f, offset)
def iablob(fr, measurement, option="image"): measurement = upper(measurement) option = upper(option) if len(fr.shape) == 1: fr = fr[newaxis, :] n = fr.max() if option == 'DATA': y = [] elif measurement == 'CENTROID': y = np.zeros(fr.shape, bool) elif measurement == 'BOUNDINGBOX': y = np.zeros(fr.shape, bool) elif measurement == 'PERIMETER': y = np.zeros(fr.shape, np.int32) elif measurement == 'AREA': y = np.zeros(fr.shape, np.int32) else: y = np.zeros(fr.shape) if measurement == 'AREA': area = np.bincount(fr.ravel()) if option == 'DATA': y = area[1::] else: for i in xrange(1, n + 1): y[fr == i] = area[i] elif measurement == 'CENTROID': for i in xrange(1, n + 1): aux = (fr == i) xind, yind = np.nonzero(aux) area = len(xind) centroid = [xind.sum() / area, yind.sum() / area] if option == 'DATA': y.append([centroid[1], centroid[0]]) else: y[centroid[0], centroid[1]] = 1 elif measurement == 'BOUNDINGBOX': for i in xrange(1, n + 1): aux = (fr == i) col, = np.nonzero(aux.any(0)) row, = np.nonzero(aux.any(1)) if option == 'DATA': y.append([col[0], row[0], col[-1], row[-1]]) else: y[row[0]:row[-1], col[0]] = 1 y[row[0]:row[-1], col[-1]] = 1 y[row[0], col[0]:col[-1]] = 1 y[row[-1], col[0]:col[-1]] = 1 elif measurement == 'PERIMETER': Bc = ia870.iasecross() for i in xrange(1, n + 1): aux = fr == i grad = aux - ia870.iaero(aux, Bc) if option == 'DATA': y.append(grad.sum()) else: y[aux] = grad.sum() elif measurement == 'CIRCULARITY': Bc = ia870.iasecross() area = np.bincount(fr.ravel()) perim = [] for i in xrange(1, n + 1): aux = fr == i grad = aux - ia870.iaero(aux, Bc) perim.append(grad.sum()) if option != 'DATA': y[aux] = 4 * np.pi * area[i] / (perim[i - 1]**2) if option == 'DATA': perim = np.array(perim) y = 4 * np.pi * area[1::] / (perim**2) elif measurement == 'ASPECTRATIO': for i in xrange(1, n + 1): aux = (fr == i) col, = np.nonzero(aux.any(0)) row, = np.nonzero(aux.any(1)) if option == 'DATA': y.append(1. * (min(col[-1] - col[0], row[-1] - row[0]) + 1) / (max(col[-1] - col[0], row[-1] - row[0]) + 1)) else: y[aux] = 1. * (min(col[-1] - col[0], row[-1] - row[0]) + 1) / ( max(col[-1] - col[0], row[-1] - row[0]) + 1) elif measurement == 'COMPACTNESS': for i in xrange(1, n + 1): aux = (fr == i) xind, yind = np.nonzero(aux) area = len(xind) centroid = [xind.sum() / area, yind.sum() / area] m20 = ((1. * xind - centroid[0])**2).sum() / area m02 = ((1. * yind - centroid[1])**2).sum() / area compactness = area / (2 * np.pi * (m20 + m02)) if option == 'DATA': y.append(compactness) else: y[aux] = compactness elif measurement == 'ECCENTRICITY': for i in xrange(1, n + 1): aux = (fr == i) xind, yind = np.nonzero(aux) area = len(xind) centroid = [xind.sum() / area, yind.sum() / area] m11 = ((1. * xind - centroid[0]) * (yind - centroid[1])).sum() / area m20 = ((1. * xind - centroid[0])**2).sum() / area m02 = ((1. * yind - centroid[1])**2).sum() / area eccentricity = sqrt((m20 - m02)**2 + 4 * m11**2) / (m20 + m02) if option == 'DATA': y.append(eccentricity) else: y[aux] = eccentricity else: print "Measurement option should be 'AREA','CENTROID', 'BOUNDINGBOX', 'PERIMETER', " print "'ASPECTRATIO', 'CIRCULARITY', 'COMPACTNESS' or 'ECCENTRICITY'." return np.array(y)