def fixed_align_frames(self,maxDisplacement=50): '''perform fixed alignment on the framestack maxDisplacement=50 - maximum allowed displacement, frames with > than this will be removed from the stack''' if self.data is None: logger.warning('No frames found') return if self.data.templateFrame is None: logger.warning('template frame not set') return framesToProcess = [i for i in self.data.frameIds if i != self.data.templateFrameId] midRow = int(self.data.frameWidth / 2) midCol = int(self.data.frameHeight / 2) targetFrame = self.data.templateFrame targetFrame = targetFrame[midRow - self.largeFrameSize : midRow + self.largeFrameSize, midCol - self.largeFrameSize : midRow + self.largeFrameSize] results = [] #ensure the target frame is included in the output results.append({'frameid':self.data.templateFrameId, 'correlation':1, 'shift':(0,0)}) for iFrame in framesToProcess: templateFrame = self.data.get_frame_by_id(iFrame) templateFrame = templateFrame[midRow - self.smallFrameSize : midRow + self.smallFrameSize, midCol - self.smallFrameSize : midCol + self.smallFrameSize] displacement = ImageTools.find_frame_shift(targetFrame, templateFrame, topLeft=[(midRow - self.largeFrameSize,midCol - self.largeFrameSize), (midRow - self.smallFrameSize,midCol - self.smallFrameSize)], applyBlur=True, method='xcorr', attemptSubPixelAlignment=False) results.append({'frameid':iFrame, 'correlation':displacement['maxcorr'], 'shift':displacement['coords']}) #Check displacement is les than 50 pixels good_results = [result for result in results if abs(result['shift'][1])<=maxDisplacement and abs(result['shift'][0]) <= maxDisplacement] bad_results = [result['frameid'] for result in results if abs(result['shift'][1]) > maxDisplacement or abs(result['shift'][0]) > maxDisplacement] logger.info('Removing frames {} for too large displacements'.format(bad_results)) if not good_results: #no good frames found logger.warning('frame displacements are too large') raise RuntimeError('frame displacements are too large') alignedData = StackTools.apply_displacements(self.data,good_results) self.data = alignedData self.currentStack = alignedData
def create_average_frame(self,type='mean'): assert type in ['lucky','mean'] #check to see if an error has occured before this if self.data is None: logger.debug('') if type == 'lucky': #creating a lucky average if self.data.frameCount > 20: self.currentAverageFrame = StackTools.compute_lucky_image(self.data) else: self.currentAverageFrame = None logger.warning('Too few frames to create lucky average') else: self.currentAverageFrame = self.data.mean(axis=0)
def create_average_frame(self, type='mean'): assert type in ['lucky', 'mean'] #check to see if an error has occured before this if self.data is None: logger.debug('') if type == 'lucky': #creating a lucky average if self.data.frameCount > 20: self.currentAverageFrame = StackTools.compute_lucky_image( self.data) else: self.currentAverageFrame = None logger.warning('Too few frames to create lucky average') else: self.currentAverageFrame = self.data.mean(axis=0)
def compute_stdev_image(stack): '''Compute the standard deviation image''' nFrames, height, width = stack.shape if nFrames < 10: logger.error('stdev image requires at least 10 frames') return None global results results = np.empty(stack.shape) stack = StackTools.getMaskedStack(stack) medianImage = np.median(stack,axis=0) #pool = mp.Pool() for idxFrame in range(stack.shape[0]): srcImg = stack[idxFrame,:,:] #pool.apply_async(compute_variance_image, #args=(srcImg, #medianImage, #idxFrame), #callback=_compute_stdev_image_callback) results[idxFrame,:,:] = compute_variance_image(srcImg,medianImage,idxFrame)[1] #pool.close #pool.join #newStack = np.ma.array(results) imageStdev = results.std(axis=0) imageAvg = results.mean(axis=0) #some values in imageAvg can be 0, create this as a masked array imageAvg = np.ma.MaskedArray(data = imageAvg, mask = imageAvg == 0) imageNorm = imageAvg / imageAvg.max() imageRatio = imageStdev / imageNorm imageRatio[imageRatio > 0.3] = 0.3 imageRatio = cv2.medianBlur(imageRatio.astype(np.float32),3) return imageRatio
def fixed_align_frames(self, maxDisplacement=50): '''perform fixed alignment on the framestack maxDisplacement=50 - maximum allowed displacement, frames with > than this will be removed from the stack''' if self.data is None: logger.warning('No frames found') return if self.data.templateFrame is None: logger.warning('template frame not set') return framesToProcess = [ i for i in self.data.frameIds if i != self.data.templateFrameId ] framesToProcess2 = [ i for i in self.data2.frameIds if i != self.data2.templateFrameId ] midRow = int(self.data.frameWidth / 2) midCol = int(self.data.frameHeight / 2) midRow2 = int(self.data2.frameWidth / 2) midCol2 = int(self.data2.frameHeight / 2) targetFrame = self.data.templateFrame targetFrame = targetFrame[midRow - self.largeFrameSize:midRow + self.largeFrameSize, midCol - self.largeFrameSize:midRow + self.largeFrameSize] targetFrame2 = self.data2.templateFrame targetFrame2 = targetFrame2[midRow2 - self.largeFrameSize:midRow2 + self.largeFrameSize, midCol2 - self.largeFrameSize:midRow2 + self.largeFrameSize] results = [] results2 = [] #ensure the target frame is included in the output results.append({ 'frameid': self.data.templateFrameId, 'correlation': 1, 'shift': (0, 0) }) results2.append({ 'frameid': self.data2.templateFrameId, 'correlation': 1, 'shift': (0, 0) }) for iFrame in framesToProcess: templateFrame = self.data.get_frame_by_id(iFrame) templateFrame = templateFrame[midRow - self.smallFrameSize:midRow + self.smallFrameSize, midCol - self.smallFrameSize:midCol + self.smallFrameSize] templateFrame2 = self.data2.get_frame_by_id(iFrame) templateFrame2 = templateFrame2[midRow2 - self.smallFrameSize:midRow2 + self.smallFrameSize, midCol2 - self.smallFrameSize:midCol2 + self.smallFrameSize] displacement = ImageTools.find_frame_shift( targetFrame, templateFrame, topLeft=[(midRow - self.largeFrameSize, midCol - self.largeFrameSize), (midRow - self.smallFrameSize, midCol - self.smallFrameSize)], applyBlur=True, method='xcorr', attemptSubPixelAlignment=False) displacement2 = ImageTools.find_frame_shift( targetFrame2, templateFrame2, topLeft=[(midRow2 - self.largeFrameSize, midCol2 - self.largeFrameSize), (midRow2 - self.smallFrameSize, midCol2 - self.smallFrameSize)], applyBlur=True, method='xcorr', attemptSubPixelAlignment=False) results.append({ 'frameid': iFrame, 'correlation': displacement['maxcorr'], 'shift': displacement['coords'] }) results2.append({ 'frameid': iFrame, 'correlation': displacement['maxcorr'], 'shift': displacement['coords'] }) #Check displacement is les than 50 pixels good_results = [ result for result in results if abs(result['shift'][1]) <= maxDisplacement and abs(result['shift'][0]) <= maxDisplacement ] good_results2 = [ result2 for result2 in results2 if abs(result2['shift'][1]) <= maxDisplacement and abs(result2['shift'][0]) <= maxDisplacement ] bad_results = [ result['frameid'] for result in results if abs(result['shift'][1]) > maxDisplacement or abs(result['shift'][0]) > maxDisplacement ] bad_results2 = [ result2['frameid'] for result2 in results2 if abs(result2['shift'][1]) > maxDisplacement or abs(result2['shift'][0]) > maxDisplacement ] logger.info('Removing frames {} for too large displacements'.format( bad_results)) if not good_results: #no good frames found logger.warning('frame displacements are too large') raise RuntimeError('frame displacements are too large') alignedData = StackTools.apply_displacements(self.data, good_results) self.data = alignedData alignedData2 = StackTools.apply_displacements(self.data2, good_results2) self.data2 = alignedData2 self.currentStack = alignedData self.currentStack2 = alignedData2
def fixInterlace(self): StackTools.interlaceStack(self.data)