diff --git a/registration/MatchingArea.py b/registration/MatchingArea.py index 55bd0e5..832580a 100644 --- a/registration/MatchingArea.py +++ b/registration/MatchingArea.py @@ -15,9 +15,8 @@ class MatchingArea: SECTION_AREA_BIAS = 10.0 PROCESSED_THRESHOLD = 50 N_LABELED_AREA = 40 - - - def __init__(self, homedir=None, labeled_filename=None): + + def __init__(self, homedir=None, labeled_filename=None): if homedir is None: homedir = os.path.join('/', 'data', 'registration', 'Test') @@ -28,7 +27,6 @@ def __init__(self, homedir=None, labeled_filename=None): if labeled_filename is None: self.labeled_filename = os.path.join(homedir, 'reslice_labeled_8bit', 'reslice_labeled%04d.tif') - self.ish_dir = os.path.join(homedir, 'work4') self.ish_color_dir = os.path.join(homedir, 'work5') self.metric_dir = os.path.join(homedir, 'metric') @@ -36,7 +34,7 @@ def __init__(self, homedir=None, labeled_filename=None): self.result_dir = os.path.join(homedir, 'area') if not os.path.exists(self.result_dir): os.mkdir(self.result_dir) - + self.result_log = [] def matching(self, filename): @@ -46,9 +44,9 @@ def matching(self, filename): image_ish_color = self._read_ish_color_file(filename_base) image_labeled = Image.open(self.labeled_filename % slice_id) - #print(self.labeled_filename % slice_id) + # print(self.labeled_filename % slice_id) - #labeled_intensity = self._matching_method_threashold(image_ish, image_labeled) + # labeled_intensity = self._matching_method_threashold(image_ish, image_labeled) labeled_intensity = self._matching_method_threashold_color(filename_base, image_ish_color, image_labeled) self._write_result_file(filename_base, labeled_intensity) @@ -56,8 +54,7 @@ def matching(self, filename): line = '%s' % filename_base for k, v in labeled_intensity.items(): line += ', %f' % v - self.result_log.append(line+'\n') - + self.result_log.append(line + '\n') def _matching_method_threashold(self, image_ish, image_labeled): # Initialize data @@ -73,24 +70,20 @@ def _matching_method_threashold(self, image_ish, image_labeled): labeled_intensity[i] = 0 labeled_intensity_per_area[i] = 0.0 - # Process for val_ish, val_labeled in zip(data_ish, data_labeled): labeled_area[val_labeled] += 1 if val_labeled != 0 and val_ish < self.INTENSITY_THRESHOLD: labeled_intensity[val_labeled] += 1 - # Normalize intensity by volume for i in range(self.N_LABELED_AREA): if labeled_area[i] > self.SECTION_AREA_THRESHOLD: labeled_intensity_per_area[i] = float(labeled_intensity[i]) / (labeled_area[i] + self.SECTION_AREA_BIAS) - - #return labeled_intensity + # return labeled_intensity return labeled_intensity_per_area - - + def _matching_method_threashold_color(self, filename_base, image_ish, image_labeled): # Initialize data data_ish = image_ish.getdata() @@ -105,55 +98,51 @@ def _matching_method_threashold_color(self, filename_base, image_ish, image_labe labeled_intensity[i] = 0 labeled_intensity_per_area[i] = 0.0 - # Process processed_data = [] for val_ish, val_labeled in zip(data_ish, data_labeled): labeled_area[val_labeled] += 1 - ave = (val_ish[0] + val_ish[1] + val_ish[2])/3 - value = val_ish[2]*2 - (val_ish[0] + val_ish[1]) + ave = (val_ish[0] + val_ish[1] + val_ish[2]) / 3 + value = val_ish[2] * 2 - (val_ish[0] + val_ish[1]) processed_data.append(value) - if val_labeled != 0 and ave > self.INTENSITY_THRESHOLD: + if val_labeled != 0 and ave > self.INTENSITY_THRESHOLD: if value > self.PROCESSED_THRESHOLD: labeled_intensity[val_labeled] += 1 - - self.save_processed_image(os.path.join('..', 'private', 'processed_image', filename_base+'.tif'), processed_data) + + self.save_processed_image(os.path.join('..', 'private', 'processed_image', filename_base + '.tif'), + processed_data) # Normalize intensity by volume for i in range(self.N_LABELED_AREA): if labeled_area[i] > self.SECTION_AREA_THRESHOLD: labeled_intensity_per_area[i] = float(labeled_intensity[i]) / (labeled_area[i] + self.SECTION_AREA_BIAS) - - #return labeled_intensity + # return labeled_intensity return labeled_intensity_per_area - def _read_ish_file(self, filename_base): metric = {} - metric_filename = os.path.join(self.metric_dir, filename_base+'.txt') + metric_filename = os.path.join(self.metric_dir, filename_base + '.txt') with open(metric_filename, 'r') as f: reader = csv.reader(f) for row in reader: metric[int(row[0])] = float(row[1]) - - min_slice_id = min(metric.items(), key=lambda x:x[1])[0] - #print min_slice + + min_slice_id = min(metric.items(), key=lambda x: x[1])[0] + # print min_slice regist_filename = os.path.join(self.ish_dir, filename_base, ('%04d.tif' % min_slice_id)) - #print regist_filename + # print regist_filename return Image.open(regist_filename), min_slice_id - def _read_ish_color_file(self, filename_base): - ish_color_filename = os.path.join(self.ish_color_dir, filename_base+'.tif') + ish_color_filename = os.path.join(self.ish_color_dir, filename_base + '.tif') return Image.open(ish_color_filename) - def _write_result_file(self, filename_base, labeled_intensity): - result_filename = os.path.join(self.result_dir, filename_base+'.txt') + result_filename = os.path.join(self.result_dir, filename_base + '.txt') print result_filename with open(result_filename, 'w') as f: for k, v in labeled_intensity.items(): @@ -162,7 +151,7 @@ def _write_result_file(self, filename_base, labeled_intensity): def write_log(self, filename): with open(filename, 'w') as f: f.writelines(self.result_log) - + def save_processed_image(self, filename, data): processed = Image.new('L', (512, 256), 0) processed.putdata(data) @@ -172,7 +161,7 @@ def save_processed_image(self, filename, data): if __name__ == '__main__': - #filelistname = os.path.join('/', 'data', 'registration', 'Test', 'filelist_all0.txt') + # filelistname = os.path.join('/', 'data', 'registration', 'Test', 'filelist_all0.txt') filelistname = os.path.join('..', 'private', 'filelist_all0.txt') filelist = RegistrationCommon.read_filelist(filelistname) @@ -182,4 +171,3 @@ def save_processed_image(self, filename, data): matchingarea.matching(filename) matchingarea.write_log(os.path.join('..', 'private', 'intensity_all.txt')) -