def RemoveModalBackground(image): mode=ModalValue(image) back = np.zeros(image.shape, image.dtype) back.fill(mode) #print "def background:",back.mean() im=pymorph.subm(image,back) return im
def RemoveModalBackground(image): mode = ModalValue(image) back = np.zeros(image.shape, image.dtype) back.fill(mode) # print "def background:",back.mean() im = pymorph.subm(image, back) return im
def _get_line_filter(segment_size, variation): """Computes the filters that can be used to enhance vertical lines in an Image. Args: segment_size: Size of the segment variatoin: Variation in horizontal axes if user wants not exact vertical lines. Returns: filters saved in 3D matrices, each 3rd dimension includes a filter """ smalldisk = pymorph.sedisk(1); bigdisk = pymorph.sedisk(2); horizontal_filter = numpy.zeros((variation*2+1, variation*2+1, segment_size)) horizontal_surrounding = numpy.zeros((variation*2+1, variation*2+1, segment_size)) index = -1 # Generates the filters for each direction of lines for variation_index in range(-variation, variation + 1): index = index + 1; points = bresenham(variation + variation_index,0, variation - variation_index, segment_size - 1) tmp = numpy.zeros((variation*2+1)*segment_size).reshape((variation*2+1, segment_size)) for point_ind in range(0, len(points)): tup_point = points[point_ind] tmp[tup_point[0], tup_point[1]] = 1 tmp_filter = pymorph.dilate(pymorph.binary(tmp), smalldisk) tmp_surrounding = pymorph.subm(pymorph.dilate(pymorph.binary(tmp), bigdisk) , \ pymorph.dilate(pymorph.binary(tmp), smalldisk)) horizontal_filter[index,:,:] = tmp_filter horizontal_surrounding[index,:,:] = tmp_surrounding return horizontal_filter, horizontal_surrounding
def test_subm(): assert not np.any( pymorph.subm(np.ones((3,3), np.bool), np.ones((3,3), np.bool)) ) assert np.all( pymorph.subm(np.ones((32,33), np.bool), np.zeros((32,33), np.bool)) ) assert np.all( pymorph.subm(np.ones((32,33), np.bool), np.zeros((32,33), np.uint8)) ) assert np.all( pymorph.subm(np.ones((32,33), np.uint8)+15, np.zeros((32,33), np.uint8)) ) assert not np.any( pymorph.subm(np.ones((3,3), np.uint8), np.ones((3,3), np.bool)) ) assert not np.any( pymorph.subm(np.ones((3,3), np.uint8), 2+np.ones((3,3), np.uint8)) ) assert np.all(pymorph.subm(np.ones((3,3), np.uint8), 2+np.ones((3,3), np.uint8)) == 0)
def LowResSegmentation(image): """Perform a simple threshold after DoG filtering""" noBack = RemoveModalBackground(image) # print "from Segmeta noBack:",noBack.min(),noBack.mean() blurLowRes = nd.filters.gaussian_filter(noBack, 13) blurHiRes = nd.filters.gaussian_filter(noBack, 1) midPass = pymorph.subm(blurHiRes, 0.70 * blurLowRes) bin = midPass > 1.5 * midPass.mean() binLowRes = pymorph.open(bin, pymorph.sedisk(4)) return binLowRes
def LowResSegmentation(image): '''Perform a simple threshold after DoG filtering''' noBack=RemoveModalBackground(image) #print "from Segmeta noBack:",noBack.min(),noBack.mean() blurLowRes=nd.filters.gaussian_filter(noBack,13) blurHiRes=nd.filters.gaussian_filter(noBack,1) midPass=pymorph.subm(blurHiRes,0.70*blurLowRes) bin=(midPass>1.5*midPass.mean()) binLowRes=pymorph.open(bin,pymorph.sedisk(4)) return binLowRes
def test_subm(): assert not np.any( pymorph.subm(np.ones((3, 3), np.bool), np.ones((3, 3), np.bool))) assert np.all( pymorph.subm(np.ones((32, 33), np.bool), np.zeros((32, 33), np.bool))) assert np.all( pymorph.subm(np.ones((32, 33), np.bool), np.zeros((32, 33), np.uint8))) assert np.all( pymorph.subm( np.ones((32, 33), np.uint8) + 15, np.zeros((32, 33), np.uint8))) assert not np.any( pymorph.subm(np.ones((3, 3), np.uint8), np.ones((3, 3), np.bool))) assert not np.any( pymorph.subm(np.ones((3, 3), np.uint8), 2 + np.ones((3, 3), np.uint8))) assert np.all( pymorph.subm(np.ones((3, 3), np.uint8), 2 + np.ones((3, 3), np.uint8)) == 0)
def _get_line_filter(segment_size, variation): """Computes the filters that can be used to enhance vertical lines in an Image. Args: segment_size: Size of the segment variatoin: Variation in horizontal axes if user wants not exact vertical lines. Returns: filters saved in 3D matrices, each 3rd dimension includes a filter """ smalldisk = pymorph.sedisk(1) bigdisk = pymorph.sedisk(2) horizontal_filter = numpy.zeros( (variation * 2 + 1, variation * 2 + 1, segment_size)) horizontal_surrounding = numpy.zeros( (variation * 2 + 1, variation * 2 + 1, segment_size)) index = -1 # Generates the filters for each direction of lines for variation_index in range(-variation, variation + 1): index = index + 1 points = bresenham(variation + variation_index, 0, variation - variation_index, segment_size - 1) tmp = numpy.zeros((variation * 2 + 1) * segment_size).reshape( (variation * 2 + 1, segment_size)) for point_ind in range(0, len(points)): tup_point = points[point_ind] tmp[tup_point[0], tup_point[1]] = 1 tmp_filter = pymorph.dilate(pymorph.binary(tmp), smalldisk) tmp_surrounding = pymorph.subm(pymorph.dilate(pymorph.binary(tmp), bigdisk) , \ pymorph.dilate(pymorph.binary(tmp), smalldisk)) horizontal_filter[index, :, :] = tmp_filter horizontal_surrounding[index, :, :] = tmp_surrounding return horizontal_filter, horizontal_surrounding
def _getLineFilter(self, segmentSize, variation): smallDisk = pymorph.sedisk(1); bigDisk = pymorph.sedisk(2); horizontal_filter = numpy.zeros((variation*2+1,variation*2+1,segmentSize)) horizontal_surrounding = numpy.zeros((variation*2+1,variation*2+1,segmentSize)) index = -1 for i in range(-variation,variation+1): index = index + 1; # find the line between selected points points = bresenham(variation+i,0,variation-i,segmentSize-1) tmp = numpy.zeros((variation*2+1)*segmentSize).reshape((variation*2+1, segmentSize)) for l in range(0, len(points)): tup_point = points[l] tmp[tup_point[0], tup_point[1]] = 1 tmp_filter = pymorph.dilate(pymorph.binary(tmp), smallDisk) tmp_surrounding = pymorph.subm(pymorph.dilate(pymorph.binary(tmp), bigDisk) , pymorph.dilate(pymorph.binary(tmp), smallDisk)) horizontal_filter[index,:,:] = tmp_filter horizontal_surrounding[index,:,:] = tmp_surrounding return horizontal_filter, horizontal_surrounding
def HighPass(im,size): blur=nd.gaussian_filter(im, size) hi=pymorph.subm(im,blur) return hi
angle=i*step majorAngle=PeakByModalValue(compassTable) self.rotatedIm=nd.rotate(self.particuleImage,majorAngle) self.rotatedFlag=True return majorAngle,compassTable,self.rotatedIm user=os.path.expanduser("~") #modify the path to your image workdir=os.path.join(user,"Applications","ImagesTest","CytoProject","Jpp48","8","DAPI","particles") file="part3.png" complete_path=os.path.join(workdir,file) if __name__ == "__main__": im=readmagick.readimg(complete_path) im0=np.copy(im) hip=pymorph.subm(im,nd.gaussian_filter(im,5)) hip0=np.copy(hip) im=mahotas.thin(im) #im=mahotas.bwperim(im>0) hip=mahotas.thin(hip) #print im.dtype#print uint16 p1=particle(im) print "particle 1",p1.cvxhull_area() p2=particle(hip) contour=mahotas.bwperim(im>0) p3=particle(contour) print "particle 1 hi pass",p2.cvxhull_area() theta1,rosedesvents,VImage=p1.orientationByErosion(5) theta2,rdv,VHip=p2.orientationByErosion(5) x=rosedesvents[0,:] y=rosedesvents[1,:]
def HighPass(im, size): blur = nd.gaussian_filter(im, size) hi = pymorph.subm(im, blur) return hi
self.rotatedIm = nd.rotate(self.particuleImage, majorAngle) self.rotatedFlag = True return majorAngle, compassTable, self.rotatedIm user = os.path.expanduser("~") #modify the path to your image workdir = os.path.join(user, "Applications", "ImagesTest", "CytoProject", "Jpp48", "8", "DAPI", "particles") file = "part3.png" complete_path = os.path.join(workdir, file) if __name__ == "__main__": im = readmagick.readimg(complete_path) im0 = np.copy(im) hip = pymorph.subm(im, nd.gaussian_filter(im, 5)) hip0 = np.copy(hip) im = mahotas.thin(im) #im=mahotas.bwperim(im>0) hip = mahotas.thin(hip) #print im.dtype#print uint16 p1 = particle(im) print "particle 1", p1.cvxhull_area() p2 = particle(hip) contour = mahotas.bwperim(im > 0) p3 = particle(contour) print "particle 1 hi pass", p2.cvxhull_area() theta1, rosedesvents, VImage = p1.orientationByErosion(5) theta2, rdv, VHip = p2.orientationByErosion(5) x = rosedesvents[0, :] y = rosedesvents[1, :]
def alternative_solution(self, a, orientation='coronal', linethickness=10, outimg=False): ''' Paramenters ----------- a: original image in graylevel ''' H, W = a.shape if orientation == 'coronal': # UL = mm.limits(a)[1] # upper limit UL = 255 b = 1 - iacircle(a.shape, H / 3, (1.4 * H / 3, W / 2)) # Circle b = b[0:70, W / 2 - 80:W / 2 + 80] # Rectangle # if outimg: # b_ = 0 * a; b_[0:70, W / 2 - 80:W / 2 + 80] = UL * b # b_ only for presentation # b_[:, W / 2 - linethickness / 2:W / 2 + linethickness / 2] = UL # b_ only for presentation c = a + 0 c[:, W / 2 - linethickness / 2:W / 2 + linethickness / 2] = UL c[0:70, W / 2 - 80:W / 2 + 80] = (1 - b) * c[0:70, W / 2 - 80:W / 2 + 80] + b * UL c[0:40, W / 2 - 70:W / 2 + 70] = UL d = mm.open(c, mm.img2se(mm.binary(np.ones((20, 10))))) e = mm.close(d, mm.seline(5)) f = mm.close_holes(e) g = mm.subm(f, d) h = mm.close_holes(g) i = mm.areaopen(h, 1000) j1, j2 = iaotsu(i) # j = i > j1 ret, j = cv2.threshold(cv2.GaussianBlur(i, (7, 7), 0), j1, 255, cv2.THRESH_BINARY) k = mm.open(j, mm.seline(20, 90)) l = mm.areaopen(k, 1000) # m = mm.label(l) res = np.vstack( [np.hstack([c, d, e, f, g]), np.hstack([h, i, j, k, l])]) cv2.imshow('Result', res) cv2.waitKey(0) cv2.destroyAllWindows() ################################ # l_ = mm.blob(k,'AREA','IMAGE') # l = l_ == max(ravel(l_)) # m = mm.open(l, mm.sedisk(3)) # VERIFICAR O MELHOR ELEMENTO ESTRUTURANTE AQUI # n = mm.label(m) if outimg: if not os.path.isdir('outimg'): os.mkdir('outimg') def N(x): # y = uint8(ianormalize(x, (0, 255)) + 0.5) y = (ianormalize(x, (0, 255)) + 0.5).astype(np.uint8) return y adwrite('outimg/a.png', N(a)) adwrite('outimg/b.png', N(b_)) adwrite('outimg/c.png', N(c)) adwrite('outimg/d.png', N(d)) adwrite('outimg/e.png', N(e)) adwrite('outimg/f.png', N(f)) adwrite('outimg/g.png', N(g)) adwrite('outimg/h.png', N(h)) adwrite('outimg/i.png', N(i)) adwrite('outimg/j.png', N(j)) adwrite('outimg/k.png', N(k)) adwrite('outimg/l.png', N(l)) adwrite('outimg/m.png', N(m)) # adwrite('outimg/n.png', N(n)) return m else: b = mm.areaopen(a, 500) c = mm.close(b, mm.sebox(3)) d = mm.close_holes(c) e = mm.subm(d, c) f = mm.areaopen(e, 1000) # g = f > 5 ret, g = cv2.threshold(cv2.GaussianBlur(f, (5, 5), 0), 3, 255, cv2.THRESH_BINARY) # ret, g = cv2.threshold( # cv2.GaussianBlur(f, (7, 7), 0), # 5, 255, # cv2.THRESH_BINARY_INV) h = mm.asf(g, 'CO', mm.sedisk(5)) i = mm.close_holes(h) res = np.vstack( [np.hstack([a, b, c, d, e]), np.hstack([f, g, h, i, a])]) cv2.imshow('Result', res) cv2.waitKey(0) cv2.destroyAllWindows() if outimg: if not os.path.isdir('outimg'): os.mkdir('outimg') def N(x): y = (ianormalize(x, (0, 255)) + 0.5).astype(np.uint8) return y adwrite('outimg/a.png', N(a)) adwrite('outimg/b.png', N(b)) adwrite('outimg/c.png', N(c)) adwrite('outimg/d.png', N(d)) adwrite('outimg/e.png', N(e)) adwrite('outimg/f.png', N(f)) adwrite('outimg/g.png', N(g)) adwrite('outimg/h.png', N(h)) adwrite('outimg/i.png', N(i)) return i
# tmp = mm.gshow(img, img_out) # tmp = colorize_segmentation(img, img_out) # num = filename[:-4] # adwrite(num + '_out.png', tmp) # adwrite(num + '_out.png', img_out) if optype == 'coronal' and opnmasks == 2: u, i, j = iaunique(ravel(img_out)) tmp1 = mm.gradm(img_out == u[1], mm.sedisk(opthickness), mmsedisk(opthickness)) tmp2 = mm.gradm(img_out == u[2], mm.sedisk(opthickness), mmsedisk(opthickness)) s1 = sum(tmp1, 0) s2 = sum(tmp2, 0) s = mm.subm(s1, s2) meio = len(s) / 2 if sum(s[0:meio]) > sum(s[meio::]): tmpR = tmp1 tmpL = tmp2 else: tmpR = tmp2 tmpL = tmp1 # adwrite(num + '_maskL.png', 255 * tmpL) # adwrite(num + '_maskR.png', 255 * tmpR) adwrite('{}/maskLIM ({}).png'.format(DIR_dest, i + 1), 255 * tmpL) adwrite('{}/maskRIM ({}).png'.format(DIR_dest, i + 1), 255 * tmpR) else: print("Write image") tmp = mm.gradm(img_out > 0, mm.sedisk(opthickness), mm.sedisk(opthickness))