def _feature_vectors(): directory = _base_directory_training + "original/" features = [] for filename in sorted(glob.glob(os.path.join(directory, '*.tif'))): name = os.path.basename(filename) name = name.split(".")[0] original = cv2.imread(filename, 1) gray = cv2.imread(filename, 0) (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray) lab = cv2.cvtColor(original, cv2.COLOR_BGR2LAB) L, A, B = cv2.split(lab) manual = retina.Retina( None, _base_directory_training + "manual/" + name + ".png") manual.threshold_image() thr_img = manual.get_uint_image() cv2.circle(thr_img, maxLoc, 60, 0, -1) manual.skeletonization() skeleton_img = manual.get_uint_image() cv2.circle(skeleton_img, maxLoc, 60, 0, -1) landmarks, segmented_skeleton_img = l.potential_landmarks( skeleton_img, 3) av = cv2.imread(_base_directory_training + "av/" + name + ".png", 1) widths = _vessel_widths(segmented_skeleton_img, thr_img) data = _preparing_data(widths, 6, original, av, L, gray) features.extend(data) h5f = h5py.File(_base_directory_model + 'vector_features_interpolation.h5', 'w') h5f.create_dataset('training', data=features) return features
def test_compare_with(self): self.image.threshold_image() original_image = retina.Retina(None, _image_path) assert_array_equal( self.image.compare_with(original_image).np_image, self.image.np_image - original_image.np_image, "image does not match")
def test_dilate(self): self.image.threshold_image() self.image.dilate(1) original_image = retina.Retina(None, _image_path) original_image.threshold_image() assert_array_equal( self.image.np_image, ndimage.binary_dilation(original_image.np_image, iterations=1))
def test_set_tag_layer(self): image = retina.Retina(None, _image_path) image.reshape_by_window(56) window = retina.Window(image, 56, min_pixels=10) tags = np.full([window.shape[0], 4], 100) tags[:, 3] = 50 window.tags = tags window.set_tag_layer()
def test_undo(self): self.image.detect_edges_canny() original_image = retina.Retina(None, _image_path) self.assertRaises( AssertionError, assert_array_equal, self.image.np_image, original_image.np_image, "images should be different") self.image.undo() assert_array_equal(self.image.np_image, original_image.np_image, "image should be the same")
def density(image: np.ndarray, window_size: int = 10, min_pixels: int = 10, creation_method: str = "separated", threshold: float = 0.97) -> dict: image = retina.Retina(image, "tortuosity_density") dimension = image.reshape_by_window(window_size, True) image.threshold_image() image.skeletonization() windows = retina.Window(image, dimension, min_pixels=min_pixels, method=creation_method) evaluation = \ { "uri": "tortuosity_density", "data": [], # "image": image.original_base64 # TODO: maybe return a processed image? } for i in range(0, windows.shape[0]): window = windows.windows[i, 0] w_pos = windows.w_pos[i] image = retina.Retina(window, "td") vessels = retina.detect_vessel_border(image) processed_vessel_count = 0 for vessel in vessels: if len(vessel[0]) > 10: processed_vessel_count += 1 tortuosity_density = tortuosity_measures.tortuosity_density( vessel[0], vessel[1]) if tortuosity_density > threshold: evaluation["data"].append( _tortuosity_window( w_pos[0, 0].item(), w_pos[0, 1].item(), w_pos[1, 0].item(), w_pos[1, 1].item(), "{0:.2f}".format(tortuosity_density))) return evaluation
def test_iterator(self): image = retina.Retina(None, _image_path) image.reshape_by_window(56) windows = retina.Window(image, 56, min_pixels=10) size = windows.windows.shape[0] iterated_size = 0 for _ in windows: iterated_size += 1 self.assertEqual(size, iterated_size, "iterated structure size does not match") for window in windows: assert_array_equal(window, windows.windows[0]) break
def classification(image: np.ndarray, border_size: int): img = retina.Retina(image, None) img.reshape_for_landmarks(border_size) img.threshold_image() threshold = img.get_uint_image() img.skeletonization() skeleton = img.get_uint_image() img.bin_to_bgr() skeleton_rgb = img.get_uint_image() landmarks, segmented = potential_landmarks(skeleton, 3) widths = vessel_width(threshold, landmarks) vessels = finding_landmark_vessels(widths, landmarks, skeleton, skeleton_rgb) marked_skeleton, final_landmarks = vessel_number(vessels, landmarks, skeleton_rgb) bifurcations, crossings = principal_boxes(marked_skeleton, final_landmarks, border_size) return bifurcations, crossings
def _loading_model(original: np.ndarray, threshold: np.ndarray, av: np.ndarray, size: int): # Load model of the neuronal network json_file = open(_base_directory_model + 'modelVA.json', "r") loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # Load weights loaded_model.load_weights(_base_directory_model + 'modelVA.h5') gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY) (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray) lab = cv2.cvtColor(original, cv2.COLOR_BGR2LAB) L, A, B = cv2.split(lab) manual = retina.Retina(threshold, None) manual.threshold_image() thr_img = manual.get_uint_image() cv2.circle(thr_img, maxLoc, 60, 0, -1) manual.skeletonization() skeleton_img = manual.get_uint_image() cv2.circle(skeleton_img, maxLoc, 60, 0, -1) landmarks, segmented_skeleton_img = l.potential_landmarks(skeleton_img, 3) widths = _vessel_widths(segmented_skeleton_img, thr_img) data = _preparing_data(widths, 6, original, av, L, gray) features = np.array(data) predict_img = np.full( (segmented_skeleton_img.shape[0], segmented_skeleton_img.shape[1]), 3, dtype=float) for row in range(0, features.shape[0]): prediction = loaded_model.predict(np.divide( features[row:row + 1, 2:size], 255), batch_size=1) predict_img[features[row, 0], features[row, 1]] = prediction return features, segmented_skeleton_img, thr_img, predict_img
def setUp(self): self._retina_image = retina.Retina( np.zeros((self._image_size, self._image_size), np.uint8), _image_file_name)
def test_apply_thinning(self): retina_image = retina.Retina(np.zeros((64, 64), np.uint8), _image_file_name) retina_image.np_image[10:17, 10:13] = 1 retina_image.apply_thinning() output = [0, 1, 1, 1, 1, 0] assert_array_equal(retina_image.np_image[10:16, 11], output, "expected a line")
def test_constructor_existing_image(self): """Test the constructor with an existing image""" image = retina.Retina(None, _image_path) none_constructor_image = retina.Retina(image.np_image, _image_file_name) assert_array_equal(image.np_image, none_constructor_image.np_image, "created images should be the same")
def setUp(self): self.image = retina.Retina(None, _image_path)
def test_set_tag_layer_tags_not_set(self): image = retina.Retina(None, _image_path) image.reshape_by_window(56) window = retina.Window(image, 56, min_pixels=10) with self.assertRaises(ValueError): window.set_tag_layer()
def _validating_model(features: np.ndarray, skeleton_img: np.ndarray, original_img: np.ndarray, predicted_img: np.ndarray, size: int, av: int): max_acc = -1 rgb_prediction = [] network_prediction = [] original = [] if av == 0: manual_copy = retina.Retina(skeleton_img, None) manual_copy.bin_to_bgr() manual_copy = manual_copy.get_uint_image() original_copy = original_img.copy() predict_copy = predicted_img.copy() mask0 = predict_copy == 3 mask1 = (predict_copy >= 0) & (predict_copy < 0.8) mask2 = (predict_copy >= 0.8) & (predict_copy <= 1) predict_copy[mask1] = 1 predict_copy[mask2] = 2 predict_copy[mask0] = 0 for row in range(0, features.shape[0]): if predict_copy[features[row, 0], features[row, 1]] == 2: manual_copy[features[row, 0], features[row, 1]] = [0, 0, 255] original_copy[features[row, 0], features[row, 1]] = [0, 0, 255] elif predict_copy[features[row, 0], features[row, 1]] == 1: manual_copy[features[row, 0], features[row, 1]] = [255, 0, 0] original_copy[features[row, 0], features[row, 1]] = [255, 0, 0] rgb_prediction = manual_copy network_prediction = predict_copy original = original_copy else: for i in range(0, 1000): manual_copy = retina.Retina(skeleton_img, None) manual_copy.bin_to_bgr() manual_copy = manual_copy.get_uint_image() original_copy = original_img.copy() predict_copy = predicted_img.copy() k = 0.001 * i mask0 = predict_copy == 3 mask1 = (predict_copy >= 0) & (predict_copy < k) mask2 = (predict_copy >= k) & (predict_copy <= 1) predict_copy[mask1] = 1 predict_copy[mask2] = 2 predict_copy[mask0] = 0 true_positive = 0 true_negative = 0 false_positive = 0 false_negative = 0 for row in range(0, features.shape[0]): if predict_copy[features[row, 0], features[row, 1]] == 2: manual_copy[features[row, 0], features[row, 1]] = [0, 0, 255] original_copy[features[row, 0], features[row, 1]] = [0, 0, 255] elif predict_copy[features[row, 0], features[row, 1]] == 1: manual_copy[features[row, 0], features[row, 1]] = [255, 0, 0] original_copy[features[row, 0], features[row, 1]] = [255, 0, 0] if int(predict_copy[features[row, 0], features[row, 1]]) == 1 and features[row, size] == 0: true_negative += 1 elif int(predict_copy[features[row, 0], features[ row, 1]]) == 2 and features[row, size] == 1: true_positive += 1 elif int(predict_copy[features[row, 0], features[ row, 1]]) == 2 and features[row, size] == 0: false_positive += 1 elif int(predict_copy[features[row, 0], features[ row, 1]]) == 1 and features[row, size] == 1: false_negative += 1 accy = (100 * (true_positive + true_negative)) / features.shape[0] if max_acc < accy: max_acc = accy rgb_prediction = manual_copy network_prediction = predict_copy original = original_copy return max_acc, rgb_prediction, network_prediction, original
def test_create_windows_combined_error_dimension(self): new_image = retina.Retina(np.zeros((66, 66), np.uint8), _image_file_name) self.assertRaises(ValueError, retina.Window, new_image, 33, "combined", 0)
def test_fractal_tortuosity(self): self.assertAlmostEqual(tm.fractal_tortuosity( retina.Retina(None, "retipy/resources/images/img01.png")), 1.703965, msg="fractal tortuosity does not match", delta=0.00001)
def test_reshape_to_landmarks(self): self.image.reshape_for_landmarks(5) new_image = retina.Retina(None, _image_path) new_image.np_image = np.pad(new_image.np_image, pad_width=5, mode='constant', constant_values=0) assert_array_equal(new_image.np_image, self.image.np_image)
def test_skeletonization(self): self.image.threshold_image() self.image.skeletonization() original_image = retina.Retina(None, _image_path) original_image.threshold_image() assert_array_equal(self.image.np_image, skeletonize(original_image.np_image))
from retipy import configuration, retina, tortuosity_measures parser = argparse.ArgumentParser() parser.add_argument("-c", "--configuration", help="the configuration file location", default="resources/retipy.config") args = parser.parse_args() CONFIG = configuration.Configuration(args.configuration) for filename in sorted(glob.glob(os.path.join(CONFIG.image_directory, '*.png'))): print("processing {}...".format(filename)) segmentedImage = retina.Retina(None, filename) segmentedImage.threshold_image() segmentedImage.reshape_square() window_sizes = segmentedImage.get_window_sizes() window = retina.Window(segmentedImage, window_sizes[-1], min_pixels=CONFIG.pixels_per_window) tortuosity_measures.evaluate_window(window, CONFIG.pixels_per_window, CONFIG.sampling_size, CONFIG.r_2_threshold) hf = h5py.File( CONFIG.output_folder + "/" + segmentedImage.filename + ".h5", 'w') hf.create_dataset('windows', data=window.windows) hf.create_dataset('tags', data=window.tags) hf.close()