def centerParticles(self, oldstack, centerstack, badstack): maxshift = self.params['maxshift'] centerparts = [] badparts = [] keeplist = [] i = 0 while partnum < numparts: ### if need more particles ### read 4000 parts from oldstack ### write centerparts to centerstack ### write badparts to badstack ### set current image oldpart = oldparts[i] ### mirror about x xmirror = numpy.flipud(oldpart) ### cross-correlate xcc = correlator.cross_correlate(oldpart, xmirror) ### find peak peakdict = peakfinder.findSubpixelPeak(xcc) xpeak = correlator.wrap_coord(peakdict['pixel peak'], xcc.shape) ### mirror about y ymirror = numpy.fliplr(oldpart) ### cross-correlate ycc = correlator.cross_correlate(oldpart, ymirror) ### find peak peakdict = peakfinder.findSubpixelPeak(ycc) ypeak = correlator.wrap_coord(peakdict['pixel peak'], ycc.shape) ### mirror about y then x xymirror = numpy.flipud(ymirror) ### cross-correlate xycc = correlator.cross_correlate(oldpart, xymirror) ### find peak peakdict = peakfinder.findSubpixelPeak(xycc) xypeak = correlator.wrap_coord(peakdict['pixel peak'], xycc.shape) ### do some math to get shift xshift = (ypeak[0] + xypeak[0]) / 4.0 yshift = (xpeak[0] + xypeak[0]) / 4.0 ### shift particle, by integers only if xshift < maxshift and yshift < maxshift: xyshift = (xshift, yshift) centerpart = ndimage.shift(oldpart, shift=xyshift, mode='wrap', order=0) centerparts.append(centerpart) keeplist.append(partnum) else: badparts.append(oldpart) return keeplist
def centerParticles(self, oldstack, centerstack, badstack): maxshift = self.params['maxshift'] centerparts = [] badparts = [] keeplist = [] i = 0 while partnum < numparts: ### if need more particles ### read 4000 parts from oldstack ### write centerparts to centerstack ### write badparts to badstack ### set current image oldpart = oldparts[i] ### mirror about x xmirror = numpy.flipud(oldpart) ### cross-correlate xcc = correlator.cross_correlate(oldpart, xmirror) ### find peak peakdict = peakfinder.findSubpixelPeak(xcc) xpeak = correlator.wrap_coord(peakdict['pixel peak'], xcc.shape) ### mirror about y ymirror = numpy.fliplr(oldpart) ### cross-correlate ycc = correlator.cross_correlate(oldpart, ymirror) ### find peak peakdict = peakfinder.findSubpixelPeak(ycc) ypeak = correlator.wrap_coord(peakdict['pixel peak'], ycc.shape) ### mirror about y then x xymirror = numpy.flipud(ymirror) ### cross-correlate xycc = correlator.cross_correlate(oldpart, xymirror) ### find peak peakdict = peakfinder.findSubpixelPeak(xycc) xypeak = correlator.wrap_coord(peakdict['pixel peak'], xycc.shape) ### do some math to get shift xshift = (ypeak[0] + xypeak[0])/4.0 yshift = (xpeak[0] + xypeak[0])/4.0 ### shift particle, by integers only if xshift < maxshift and yshift < maxshift: xyshift = (xshift, yshift) centerpart = ndimage.shift(oldpart, shift=xyshift, mode='wrap', order=0) centerparts.append(centerpart) keeplist.append(partnum) else: badparts.append(oldpart) return keeplist
def correlate_template(self): ''' Correlate template that is already created and configured. ''' fromimage = 'original' if self.__results[fromimage] is None or self.__results['template'] is None: raise RuntimeError('need image %s and template before correlation' % (fromimage,)) edges = self.__results[fromimage] edges = self.maskBlack(edges) template = self.__results['template'] cortype = self.correlation_config['cortype'] corfilt = self.correlation_config['corfilt'] if cortype == 'cross': cc = correlator.cross_correlate(edges, template) elif cortype == 'phase': cc = correlator.phase_correlate(edges, template, zero=False) else: raise RuntimeError('bad correlation type: %s' % (cortype,)) if corfilt is not None: kernel = convolver.gaussian_kernel(*corfilt) self.convolver.setKernel(kernel) cc = self.convolver.convolve(image=cc) self.__update_result('correlation', cc) if self.save_mrc: mrc.write(cc, 'correlation.mrc')
def correlate_template(self): fromimage = 'edges' if None in (self.__results[fromimage], self.__results['template']): raise RuntimeError('need image %s and template before correlation' % (fromimage,)) edges = self.__results[fromimage] template = self.__results['template'] cortype = self.correlation_config['cortype'] corfilt = self.correlation_config['corfilt'] if cortype == 'cross': cc = correlator.cross_correlate(edges, template) elif cortype == 'phase': cc = correlator.phase_correlate(edges, template, zero=False) else: raise RuntimeError('bad correlation type: %s' % (cortype,)) cc = numpy.absolute(cc) if corfilt is not None: kernel = convolver.gaussian_kernel(*corfilt) self.edgefinder.setKernel(kernel) cc = self.edgefinder.convolve(image=cc) #cc = imagefun.zscore(smooth) #cc = imagefun.zscore(cc) self.__update_result('correlation', cc) if self.save_mrc: mrc.write(cc, 'correlation.mrc')
def shift_holes(self,I_image=None,I0_image=None): I=I_image I0=I0_image cc = correlator.cross_correlate(I, I0) self.peakfinder.setImage(cc) peak=self.peakfinder.pixelPeak() delta = correlator.wrap_coord(peak, cc.shape) delta_transposed=(delta[1],delta[0]) return delta_transposed
def correlateOriginal(self, index, imagedata): if index == 0: self.originalimage = imagedata['image'] unbinned = {'row': 0.0, 'col': 0.0} else: ## correlate self.startTimer('scope change correlation') try: correlation_type = self.settings['correlation type'] except KeyError: correlation_type = 'phase' if correlation_type == 'cross': cor = correlator.cross_correlate(self.originalimage, imagedata['image']) elif correlation_type == 'phase': cor = correlator.phase_correlate(self.originalimage, imagedata['image'], False, wiener=True) else: raise RuntimeError('invalid correlation type') self.stopTimer('scope change correlation') self.displayCorrelation(cor) ## find peak self.startTimer('shift peak') peak = peakfinder.findSubpixelPeak(cor) self.stopTimer('shift peak') self.logger.debug('Peak %s' % (peak, )) pixelpeak = peak['subpixel peak'] self.startTimer('shift display') self.displayPeak(pixelpeak) self.stopTimer('shift display') peakvalue = peak['subpixel peak value'] shift = correlator.wrap_coord(peak['subpixel peak'], cor.shape) self.logger.debug('pixel shift (row,col): %s' % (shift, )) ## need unbinned result binx = imagedata['camera']['binning']['x'] biny = imagedata['camera']['binning']['y'] unbinned = {'row': shift[0] * biny, 'col': shift[1] * binx} shiftinfo = { 'previous': self.originalimage, 'next': imagedata, 'pixel shift': unbinned } return shiftinfo
def alignZeroShiftImages(imgtree,zerotilts): """Align 0 degree images for tilt series where data is collect in two halves""" #index for imgtree images are one less than index in zerotilts b/c protomo images start at 1 im1=imgtree[zerotilts[0]-1]['image'] im2=imgtree[zerotilts[1]-1]['image'] print "correlating images",zerotilts[0], "and", zerotilts[1] cc=correlator.cross_correlate(im1,im2) peak=peakfinder.findPixelPeak(cc) #print peak newy,newx=shiftPeak(cc,peak) print "new origin for image", zerotilts[0], "is", newx, newy shifty=cc.shape[0]/2 - newy shiftx=cc.shape[1]/2 - newx return {'shiftx':shiftx, 'shifty':shifty}
def alignZeroShiftImages(imgtree, zerotilts): """Align 0 degree images for tilt series where data is collect in two halves""" #index for imgtree images are one less than index in zerotilts b/c protomo images start at 1 im1 = imgtree[zerotilts[0] - 1]['image'] im2 = imgtree[zerotilts[1] - 1]['image'] print "correlating images", zerotilts[0], "and", zerotilts[1] cc = correlator.cross_correlate(im1, im2) peak = peakfinder.findPixelPeak(cc) #print peak newy, newx = shiftPeak(cc, peak) print "new origin for image", zerotilts[0], "is", newx, newy shifty = cc.shape[0] / 2 - newy shiftx = cc.shape[1] / 2 - newx return {'shiftx': shiftx, 'shifty': shifty}
def correlateOriginal(self,index,imagedata): if index == 0: self.originalimage = imagedata['image'] unbinned = {'row':0.0, 'col': 0.0} else: ## correlate self.startTimer('scope change correlation') try: correlation_type = self.settings['correlation type'] except KeyError: correlation_type = 'phase' if correlation_type == 'cross': cor = correlator.cross_correlate(self.originalimage,imagedata['image']) elif correlation_type == 'phase': cor = correlator.phase_correlate(self.originalimage,imagedata['image'],False,wiener=True) else: raise RuntimeError('invalid correlation type') self.stopTimer('scope change correlation') self.displayCorrelation(cor) ## find peak self.startTimer('shift peak') peak = peakfinder.findSubpixelPeak(cor) self.stopTimer('shift peak') self.logger.debug('Peak %s' % (peak,)) pixelpeak = peak['subpixel peak'] self.startTimer('shift display') self.displayPeak(pixelpeak) self.stopTimer('shift display') peakvalue = peak['subpixel peak value'] shift = correlator.wrap_coord(peak['subpixel peak'], cor.shape) self.logger.debug('pixel shift (row,col): %s' % (shift,)) ## need unbinned result binx = imagedata['camera']['binning']['x'] biny = imagedata['camera']['binning']['y'] unbinned = {'row':shift[0] * biny, 'col': shift[1] * binx} shiftinfo = {'previous': self.originalimage, 'next': imagedata, 'pixel shift': unbinned} return shiftinfo
def getTiltedRotateShift(img1, img2, tiltdiff, angle=0, bin=1, msg=True): """ takes two images tilted with respect to one another and tries to find overlap img1 (as numpy array) img2 (as numpy array) tiltdiff (in degrees) negative, img1 is more compressed (tilted) positive, img2 is more compressed (tilted) """ ### untilt images by stretching and compressing # choose angle s/t compressFactor = 1/stretchFactor # this only works if one image is untilted (RCT) of both images are opposite tilt (OTR) #halftilt = abs(tiltdiff)/2.0 halftiltrad = math.acos(math.sqrt(math.cos(abs(tiltdiff)/180.0*math.pi))) # go from zero tilt to half tilt compressFactor = math.cos(halftiltrad) # go from max tilt to half tilt stretchFactor = math.cos(halftiltrad) / math.cos(abs(tiltdiff)/180.0*math.pi) if tiltdiff > 0: if msg is True: apDisplay.printMsg("compress image 1") untilt1 = transformImage(img1, compressFactor, angle) untilt2 = transformImage(img2, stretchFactor, angle) xfactor = compressFactor else: if msg is True: apDisplay.printMsg("stretch image 1") untilt1 = transformImage(img1, stretchFactor, angle) untilt2 = transformImage(img2, compressFactor, angle) xfactor = stretchFactor ### filtering was done earlier filt1 = untilt1 filt2 = untilt2 if filt1.shape != filt2.shape: newshape = ( max(filt1.shape[0],filt2.shape[0]), max(filt1.shape[1],filt2.shape[1]) ) apDisplay.printMsg("Resizing images to: "+str(newshape)) filt1 = apImage.frame_constant(filt1, newshape, filt1.mean()) filt2 = apImage.frame_constant(filt2, newshape, filt2.mean()) ### cross-correlate cc = correlator.cross_correlate(filt1, filt2, pad=True) rad = min(cc.shape)/20.0 cc = apImage.highPassFilter(cc, radius=rad) cc = apImage.normRange(cc) cc = blackEdges(cc) cc = apImage.normRange(cc) cc = blackEdges(cc) cc = apImage.normRange(cc) cc = apImage.lowPassFilter(cc, radius=10.0) #find peak peakdict = peakfinder.findSubpixelPeak(cc, lpf=0) #import pprint #pprint.pprint(peak) pixpeak = peakdict['subpixel peak'] if msg is True: apDisplay.printMsg("Pixel peak: "+str(pixpeak)) apImage.arrayToJpegPlusPeak(cc, "guess-cross-ang"+str(abs(angle))+".jpg", pixpeak) rawpeak = numpy.array([pixpeak[1], pixpeak[0]]) #swap coord shift = numpy.asarray(correlator.wrap_coord(rawpeak, cc.shape))*bin if msg is True: apDisplay.printMsg("Found xy-shift btw two images" +";\n\t SNR= "+str(round(peakdict['snr'],2)) +";\n\t halftilt= "+str(round(halftiltrad*180/math.pi, 3)) +";\n\t compressFactor= "+str(round(compressFactor, 3)) +";\n\t stretchFactor= "+str(round(stretchFactor, 3)) +";\n\t xFactor= "+str(round(xfactor, 3)) +";\n\t rawpeak= "+str(numpy.around(rawpeak*bin, 1)) +";\n\t shift= "+str(numpy.around(shift, 1)) ) return shift, xfactor, peakdict['snr']
def correlateTemplate(self, edges, temp): cc = correlator.cross_correlate(edges, temp) save_mrc(cc, 'cc.mrc') center = self.peakfinder.subpixelPeak(cc) return center