def test_chan_vese_incorrect_level_set(): img = np.zeros((10, 10)) ls = np.zeros((10, 9)) with testing.raises(ValueError): chan_vese(img, mu=0.0, init_level_set=ls) with testing.raises(ValueError): chan_vese(img, mu=0.0, init_level_set="a")
def test_chan_vese_flat_level_set(): # because the algorithm evolves the level set around the # zero-level, it the level-set has no zero level, the algorithm # will not produce results in theory. However, since a continuous # approximation of the delta function is used, the algorithm # still affects the entirety of the level-set. Therefore with # infinite time, the segmentation will still converge. img = np.zeros((10, 10)) img[3:6, 3:6] = np.ones((3, 3)) ls = np.full((10, 10), 1000) result = chan_vese(img, mu=0.0, tol=1e-3, init_level_set=ls) assert_array_equal(result.astype(np.float), np.ones((10, 10))) result = chan_vese(img, mu=0.0, tol=1e-3, init_level_set=-ls) assert_array_equal(result.astype(np.float), np.zeros((10, 10)))
def test_chan_vese_gap_closing(): ref = np.zeros((20, 20)) ref[8:15, :] = np.ones((7, 20)) img = ref.copy() img[:, 6] = np.zeros((20)) result = chan_vese(img, mu=0.7, tol=1e-3, max_iter=1000, dt=1000, init_level_set="disk").astype(np.float) assert_array_equal(result, ref)
def test_chan_vese_remove_noise(): ref = np.zeros((10, 10)) ref[1:6, 1:6] = np.array([[0, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [0, 1, 1, 1, 0]]) img = ref.copy() img[8, 3] = 1 result = chan_vese(img, mu=0.3, tol=1e-3, max_iter=100, dt=10, init_level_set="disk").astype(np.float) assert_array_equal(result, ref)
def test_chan_vese_simple_shape(): img = np.zeros((10, 10)) img[3:6, 3:6] = np.ones((3, 3)) result = chan_vese(img, mu=0.0, tol=1e-8).astype(np.float) assert_array_equal(result, img)
def test_chan_vese_extended_output(): img = np.zeros((10, 10)) img[3:6, 3:6] = np.ones((3, 3)) result = chan_vese(img, mu=0.0, tol=1e-8, extended_output=True) assert_array_equal(len(result), 3)
Sevmom = [] Local_binary = [] Hist = [] Final_list = [] # calling function which reads training images training_data = image_resize() ch = 0 for i in training_data: # Splitting image to B, G and R planes image, G, R = cv2.split(i) # Applying chan-vese segmentation on blue plane cv = chan_vese(image, mu=0.5, lambda1=1, lambda2=1, tol=1e-3, max_iter=300, dt=0.5, init_level_set="checkerboard", extended_output=True) ch = ch + 1 print(ch) segmented.append(cv[0]) # New B, G and R planes for image B = np.zeros([300, 300]) G = np.zeros([300, 300]) R = np.zeros([300, 300]) count = 0 suck = 0 for i in segmented:
# s = np.linspace(0, 2*np.pi, 400) # r = 100 + 100*np.sin(s) # c = 220 + 100*np.cos(s) # init = np.array([r, c]).T # snake = active_contour(gaussian(a8, 3), # init, alpha=0.015, beta=10, gamma=0.001, # coordinates='rc') # fig, ax = plt.subplots(figsize=(7, 7)) # ax.imshow(a8, cmap=plt.cm.gray) # ax.plot(init[:, 0], init[:, 1], '--r', lw=3) # ax.plot(snake[:, 0], snake[:, 1], '-b', lw=3) # ax.set_xticks([]), ax.set_yticks([]) # ax.axis([0, a8.shape[1], a8.shape[0], 0]) # plt.show() # Feel free to play around with the parameters to see how they impact the result cv = chan_vese(img_scaled.astype(float), mu=0.3, lambda1=1, lambda2=1, tol=1e-3, max_iter=200, dt=0.5, init_level_set="checkerboard", extended_output=True) fig, axes = plt.subplots(1, 2, figsize=(8, 8)) ax = axes.flatten() ax[0].imshow(img_scaled, cmap="gray") ax[0].set_axis_off() ax[0].set_title("Original Image", fontsize=12) ax[1].imshow(cv[0], cmap="gray") ax[1].set_axis_off() title = "Chan-Vese segmentation - {} iterations".format(len(cv[2])) ax[1].set_title(title, fontsize=12) plt.show()
img_f_t = Local_Homogeneity_Filter(img_f) #Save_as_tiff('save_path', img_filtered, 'CR2_LHF') #Save_as_tiff('save_path', img_f, 'CR2_LHF_Average') #Save_as_tiff('save_path', img_f_t, 'CR2_LHF_Average_threshold') img_f_t_average = Average_Filter(img_f_t, 50) #Save_as_tiff('save_path', img_f_t_average, 'CR2_LHF_Average_threshold_average') img_f_t_average_cv = chan_vese(img_f_t_average, mu=0.01, lambda1=1, lambda2=1, tol=1e-3, max_iter=200, dt=0.5, init_level_set="checkerboard", extended_output=True) img_f_t_average_cv_1 = img_f_t_average_cv[0] img_f_t_average_cv_1 = img_f_t_average_cv_1[extend_window:extend_window + X + 1, extend_window:extend_window + Y + 1] #Save_as_tiff('save_path', img_f_t_average_cv_1, 'CR2_LHF_Average_threshold_average_CV') img_f_t_average_cv_rm = mor.remove_small_objects(img_f_t_average_cv_1, min_size=600, connectivity=1, in_place=False)
def test_chan_vese_blank_image(): img = np.zeros((10, 10)) level_set = np.random.rand(10, 10) ref = level_set > 0 result = chan_vese(img, mu=0.0, tol=0.0, init_level_set=level_set) assert_array_equal(result, ref)
def test_chan_vese_incorrect_image_type(): img = np.zeros((10, 10, 3)) ls = np.zeros((10, 9)) with pytest.raises(ValueError): chan_vese(img, mu=0.0, init_level_set=ls)
def test_chan_vese_small_disk_level_set(): img = np.zeros((10, 10)) img[3:6, 3:6] = np.ones((3, 3)) result = chan_vese(img, mu=0.0, tol=1e-3, init_level_set="small disk") assert_array_equal(result.astype(np.float), img)
Luminita Vese, Scale-Space Theories in Computer Vision, 1999, :DOI:`10.1007/3-540-48236-9_13` .. [2] Chan-Vese Segmentation, Pascal Getreuer, Image Processing On Line, 2 (2012), pp. 214-224, :DOI:`10.5201/ipol.2012.g-cv` .. [3] The Chan-Vese Algorithm - Project Report, Rami Cohen, 2011 :arXiv:`1107.2782` """ import numpy as np import matplotlib.pyplot as plt from skimage import data, img_as_float from skimage.segmentation import chan_vese image = img_as_float(data.camera()) # Feel free to play around with the parameters to see how they impact the result cv = chan_vese(image, mu=0.25, lambda1=1, lambda2=1, tol=1e-3, max_iter=200, dt=0.5, init_level_set="checkerboard", extended_output=True) fig, axes = plt.subplots(2, 2, figsize=(8, 8)) ax = axes.flatten() ax[0].imshow(image, cmap="gray") ax[0].set_axis_off() ax[0].set_title("Original Image", fontsize=12) ax[1].imshow(cv[0], cmap="gray") ax[1].set_axis_off() title = "Chan-Vese segmentation - {} iterations".format(len(cv[2])) ax[1].set_title(title, fontsize=12) ax[2].imshow(cv[1], cmap="gray") ax[2].set_axis_off()