def on_file_open(self, event): dlg = wx.FileDialog(self.frame, style=wx.FD_OPEN) if dlg.ShowModal() == wx.ID_OK: img = pil_to_array( PILImage.open( os.path.join(dlg.GetDirectory(), dlg.GetFilename()))) if img.ndim == 3: img = img[:, :, 0] + img[:, :, 1] + img[:, :, 2] img = stretch(img.astype(float)) lt = self.frame.MenuBar.Menus[0][0].MenuItems[7].IsChecked() if lt: limg, d = log_transform(img) else: limg = img self.frame.subplot_imshow_grayscale(0, 0, limg) limg = limg.flatten() menu_items = self.frame.MenuBar.Menus[0][0].MenuItems if menu_items[2].IsChecked(): t1 = t2 = otsu(limg) elif menu_items[3].IsChecked(): t1 = t2 = entropy(limg) elif menu_items[4].IsChecked(): t1, t2 = otsu3slow(limg) elif menu_items[5].IsChecked(): t1, t2 = entropy3(limg) else: t1, t2 = otsu3(limg) if lt: t1, t2 = inverse_log_transform(np.array([t1, t2]), d) m1 = img < t1 m2 = np.logical_and(img >= t1, img < t2) m3 = img > t2 cimg = np.zeros((m1.shape[0], m1.shape[1], 3)) cimg[:, :, 0][m1] = img[m1] cimg[:, :, 1][m2] = img[m2] cimg[:, :, 2][m3] = img[m3] self.frame.subplot_imshow(1, 0, cimg, sharex=self.frame.subplot(0, 0), sharey=self.frame.subplot(0, 0)) self.frame.Refresh() wx.MessageBox("Low threshold = %f, high threshold = %f" % (t1, t2), parent=self.frame)
def test_05_02_otsu_entropy(self): '''Test the entropy version of Otsu''' np.random.seed(0) image = np.hstack((np.random.exponential(1.5,size=600), np.random.poisson(15,size=300))) image.shape=(30,30) image = stretch(image) limage, d = T.log_transform(image) threshold = entropy(limage) threshold = T.inverse_log_transform(threshold, d) expected = image > threshold workspace, module = self.make_workspace(image) module.binary.value = A.BINARY module.threshold_method.value = T.TM_OTSU_GLOBAL module.use_weighted_variance.value = I.O_ENTROPY module.two_class_otsu.value = I.O_TWO_CLASS module.run(workspace) output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) self.assertTrue(np.all(output.pixel_data == expected))
def test_05_06_otsu3_entropy_high(self): '''Test the three-class otsu, entropy, middle = background''' np.random.seed(0) image = np.hstack((np.random.exponential(1.5,size=300), np.random.poisson(15,size=300), np.random.poisson(30,size=300))) image.shape=(30,30) image = stretch(image) limage, d = T.log_transform(image) t1,t2 = entropy3(limage) threshold = T.inverse_log_transform(t1, d) expected = image > threshold workspace, module = self.make_workspace(image) module.binary.value = A.BINARY module.threshold_method.value = T.TM_OTSU_GLOBAL module.use_weighted_variance.value = I.O_ENTROPY module.two_class_otsu.value = I.O_THREE_CLASS module.assign_middle_to_foreground.value = I.O_FOREGROUND module.run(workspace) output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) self.assertTrue(np.all(output.pixel_data == expected))
def test_05_06_otsu3_entropy_high(self): '''Test the three-class otsu, entropy, middle = background''' np.random.seed(0) image = np.hstack( (np.random.exponential(1.5, size=300), np.random.poisson(15, size=300), np.random.poisson(30, size=300))) image.shape = (30, 30) image = stretch(image) limage, d = T.log_transform(image) t1, t2 = entropy3(limage) threshold = T.inverse_log_transform(t1, d) expected = image > threshold workspace, module = self.make_workspace(image) module.binary.value = A.BINARY module.threshold_method.value = T.TM_OTSU_GLOBAL module.use_weighted_variance.value = I.O_ENTROPY module.two_class_otsu.value = I.O_THREE_CLASS module.assign_middle_to_foreground.value = I.O_FOREGROUND module.run(workspace) output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) self.assertTrue(np.all(output.pixel_data == expected))
def on_file_open(self, event): dlg = wx.FileDialog(self.frame,style=wx.FD_OPEN) if dlg.ShowModal() == wx.ID_OK: img = pil_to_array(PILImage.open(os.path.join(dlg.GetDirectory(),dlg.GetFilename()))) if img.ndim == 3: img = img[:,:,0]+img[:,:,1]+img[:,:,2] img = stretch(img.astype(float)) lt = self.frame.MenuBar.Menus[0][0].MenuItems[7].IsChecked() if lt: limg, d = log_transform(img) else: limg = img self.frame.subplot_imshow_grayscale(0, 0, limg) limg = limg.flatten() menu_items = self.frame.MenuBar.Menus[0][0].MenuItems if menu_items[2].IsChecked(): t1 = t2 = otsu(limg) elif menu_items[3].IsChecked(): t1 = t2 = entropy(limg) elif menu_items[4].IsChecked(): t1, t2 = otsu3slow(limg) elif menu_items[5].IsChecked(): t1, t2 = entropy3(limg) else: t1, t2 = otsu3(limg) if lt: t1,t2 = inverse_log_transform(np.array([t1,t2]), d) m1 = img < t1 m2 = np.logical_and(img >= t1, img < t2) m3 = img > t2 cimg = np.zeros((m1.shape[0],m1.shape[1],3)) cimg[:,:,0][m1]=img[m1] cimg[:,:,1][m2]=img[m2] cimg[:,:,2][m3]=img[m3] self.frame.subplot_imshow(1, 0, cimg, sharex = self.frame.subplot(0,0), sharey = self.frame.subplot(0,0)) self.frame.Refresh() wx.MessageBox("Low threshold = %f, high threshold = %f"%(t1,t2), parent=self.frame)
def test_05_04_otsu3_wv_high(self): '''Test the three-class otsu, weighted variance middle = foreground''' np.random.seed(0) image = np.hstack((np.random.exponential(1.5,size=300), np.random.poisson(15,size=300), np.random.poisson(30,size=300))) image.shape=(30,30) image = stretch(image) limage, d = T.log_transform(image) t1,t2 = otsu3(limage) threshold = T.inverse_log_transform(t1, d) workspace, module = self.make_workspace(image) module.binary.value = A.BINARY module.threshold_scope.value = I.TS_GLOBAL module.threshold_method.value = T.TM_OTSU module.use_weighted_variance.value = I.O_WEIGHTED_VARIANCE module.two_class_otsu.value = I.O_THREE_CLASS module.assign_middle_to_foreground.value = I.O_FOREGROUND module.run(workspace) m = workspace.measurements m_threshold = m[cpmeas.IMAGE, I.FF_ORIG_THRESHOLD % module.get_measurement_objects_name()] self.assertAlmostEqual(m_threshold, threshold)
def test_05_05_otsu3_entropy_low(self): '''Test the three-class otsu, entropy, middle = background''' np.random.seed(0) image = np.hstack((np.random.exponential(1.5,size=300), np.random.poisson(15,size=300), np.random.poisson(30,size=300))) image.shape=(30,30) image = stretch(image) limage, d = T.log_transform(image) t1,t2 = entropy3(limage) threshold = T.inverse_log_transform(t2, d) workspace, module = self.make_workspace(image) module.binary.value = A.BINARY module.threshold_scope.value = I.TS_GLOBAL module.threshold_method.value = T.TM_OTSU module.use_weighted_variance.value = I.O_ENTROPY module.two_class_otsu.value = I.O_THREE_CLASS module.assign_middle_to_foreground.value = I.O_BACKGROUND module.run(workspace) output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) m = workspace.measurements m_threshold = m[cpmeas.IMAGE, I.FF_ORIG_THRESHOLD % module.get_measurement_objects_name()] self.assertAlmostEqual(m_threshold, threshold)