def reconstruct(self): hybridtrack.choose_initial_end( self.original_image_kev, self.options, self.info) try: self.segment_initial_end() # get a sub-image containing the initial end # also need a rough estimate of the electron direction # (using thinned) except CheckSegmentBoxError: self.error = 'CheckSegmentBoxError' return except RuntimeError: self.error = 'what the heck happened?' return # 1. self.get_coordlist() self.compute_first_moments() # 2ab. self.compute_central_moments() # 3ab. try: self.compute_optimal_rotation_angle() except MomentsError: self.error = 'Rotation angle conditions not met' return # 4. self.compute_arc_parameters() self.compute_direction() self.compute_pathology()
def reconstruct(self): hybridtrack.choose_initial_end(self.original_image_kev, self.options, self.info) try: self.segment_initial_end() # get a sub-image containing the initial end # also need a rough estimate of the electron direction # (using thinned) except CheckSegmentBoxError: self.error = 'CheckSegmentBoxError' return except RuntimeError: self.error = 'what the heck happened?' return # 1. self.get_coordlist() self.compute_first_moments() # 2ab. self.compute_central_moments() # 3ab. try: self.compute_optimal_rotation_angle() except MomentsError: self.error = 'Rotation angle conditions not met' return # 4. self.compute_arc_parameters() self.compute_direction() self.compute_pathology()
def check_segment_box(self): """ Check whether the box intersects too many hot pixels. That would indicate that we should draw a new box. """ problem_length = 60 # microns # xmesh, ymesh get used in get_pixlist, also. so save into self. img_shape = self.original_image_kev.shape self.xmesh, self.ymesh = np.meshgrid( range(img_shape[0]), range(img_shape[1]), indexing='ij') # get the pixels along the line segment that passes through the track, # by walking along from one endpoint toward the other. xcheck = [self.box_x[-2]] ycheck = [self.box_y[-2]] dx = np.sign(self.box_x[-1] - self.box_x[-2]) dy = np.sign(self.box_y[-1] - self.box_y[-2]) while xcheck[-1] != self.box_x[-1] or ycheck[-1] != self.box_y[-1]: xcheck.append(xcheck[-1] + dx) ycheck.append(ycheck[-1] + dy) xcheck.append(self.box_x[-1]) ycheck.append(self.box_y[-1]) xcheck = np.array(xcheck) ycheck = np.array(ycheck) lgbad = ((xcheck < 0) | (xcheck >= self.original_image_kev.shape[0]) | (ycheck < 0) | (ycheck >= self.original_image_kev.shape[1])) xcheck = xcheck[np.logical_not(lgbad)] ycheck = ycheck[np.logical_not(lgbad)] # threshold from HybridTrack options low_threshold_kev = self.options.low_threshold_kev # see what pixels are over the threshold. over_thresh = np.array( [self.original_image_kev[xcheck[i], ycheck[i]] > low_threshold_kev for i in xrange(len(xcheck))]) # in order to avoid counting pixels from a separate segment, # start from end_coordinates and count outward until you hit a 0. over_thresh_pix = 1 start_ind = np.nonzero( (xcheck == self.end_coordinates[0]) & (ycheck == self.end_coordinates[1]))[0][0] # +dx, +dy side (start_ind+1 --> end): for i in xrange(start_ind + 1, len(xcheck), 1): if over_thresh[i]: over_thresh_pix += 1 else: break # -dx, -dy side (start_ind-1 --> 0): for i in xrange(start_ind - 1, -1, -1): if over_thresh[i]: over_thresh_pix += 1 else: break over_thresh_length = ( over_thresh_pix * self.options.pixel_size_um * np.sqrt(dx**2 + dy**2)) if over_thresh_length > problem_length: # have we done this too much already? if self.options.ridge_starting_distance_from_track_end_um < 30: raise CheckSegmentBoxError("Couldn't get a clean end segment") # try again, with a shorter track segment self.options.ridge_starting_distance_from_track_end_um -= 10.5 # now, repeat what we've done so far hybridtrack.choose_initial_end( self.original_image_kev, self.options, self.info) self.get_segment_initial_values() self.get_segment_box() self.check_segment_box()
def check_segment_box(self): """ Check whether the box intersects too many hot pixels. That would indicate that we should draw a new box. """ problem_length = 60 # microns # xmesh, ymesh get used in get_pixlist, also. so save into self. img_shape = self.original_image_kev.shape self.xmesh, self.ymesh = np.meshgrid(range(img_shape[0]), range(img_shape[1]), indexing='ij') # get the pixels along the line segment that passes through the track, # by walking along from one endpoint toward the other. xcheck = [self.box_x[-2]] ycheck = [self.box_y[-2]] dx = np.sign(self.box_x[-1] - self.box_x[-2]) dy = np.sign(self.box_y[-1] - self.box_y[-2]) while xcheck[-1] != self.box_x[-1] or ycheck[-1] != self.box_y[-1]: xcheck.append(xcheck[-1] + dx) ycheck.append(ycheck[-1] + dy) xcheck.append(self.box_x[-1]) ycheck.append(self.box_y[-1]) xcheck = np.array(xcheck) ycheck = np.array(ycheck) lgbad = ((xcheck < 0) | (xcheck >= self.original_image_kev.shape[0]) | (ycheck < 0) | (ycheck >= self.original_image_kev.shape[1])) xcheck = xcheck[np.logical_not(lgbad)] ycheck = ycheck[np.logical_not(lgbad)] # threshold from HybridTrack options low_threshold_kev = self.options.low_threshold_kev # see what pixels are over the threshold. over_thresh = np.array([ self.original_image_kev[xcheck[i], ycheck[i]] > low_threshold_kev for i in xrange(len(xcheck)) ]) # in order to avoid counting pixels from a separate segment, # start from end_coordinates and count outward until you hit a 0. over_thresh_pix = 1 start_ind = np.nonzero((xcheck == self.end_coordinates[0]) & (ycheck == self.end_coordinates[1]))[0][0] # +dx, +dy side (start_ind+1 --> end): for i in xrange(start_ind + 1, len(xcheck), 1): if over_thresh[i]: over_thresh_pix += 1 else: break # -dx, -dy side (start_ind-1 --> 0): for i in xrange(start_ind - 1, -1, -1): if over_thresh[i]: over_thresh_pix += 1 else: break over_thresh_length = (over_thresh_pix * self.options.pixel_size_um * np.sqrt(dx**2 + dy**2)) if over_thresh_length > problem_length: # have we done this too much already? if self.options.ridge_starting_distance_from_track_end_um < 30: raise CheckSegmentBoxError("Couldn't get a clean end segment") # try again, with a shorter track segment self.options.ridge_starting_distance_from_track_end_um -= 10.5 # now, repeat what we've done so far hybridtrack.choose_initial_end(self.original_image_kev, self.options, self.info) self.get_segment_initial_values() self.get_segment_box() self.check_segment_box()