def test_pad_too_many_axes(self): arr = np.arange(30).reshape(5, 6) # Attempt to pad using a 3D array equivalent bad_shape = (((3,), (4,), (5,)), ((0,), (1,), (2,))) with testing.raises(ValueError): pad(arr, bad_shape, mode='constant')
def test_check_wrong_pad_amount(self): arr = np.arange(30) arr = np.reshape(arr, (6, 5)) kwargs = dict(mode='mean', stat_length=(3, )) with pytest.raises(TypeError): pad(arr, ((2, 3, 4), (3, 2)), **kwargs)
def prefilt(img, fc=4): """ assume greyscale (c==1), individual image(N==1) """ w = 5 s1 = fc/np.sqrt(np.log(2.0)) # pad images to reduce boundary artifacts img = np.log(img+1) img = util.pad(img, w, mode='symmetric') sn, sm = img.shape n = max(sn, sm) n += np.mod(n, 2) img = util.pad(img, ((0, n-sn), (0, n-sm)), mode='symmetric') # filter fx, fy = np.meshgrid(np.arange(-n/2, n/2), np.arange(-n/2, n/2)) gf = np.fft.fftshift(np.exp(-(fx**2+fy**2)/(s1**2))) # whitening output = img - np.real(np.fft.ifft2(np.fft.fft2(img)*gf)) # local contrast normalization localstd = np.sqrt( np.abs(np.fft.ifft2(np.fft.fft2(output**2)*gf))) output = output / (0.2+localstd) # crop output to have the same size as the input output = output[w:sn-w, w:sm-w] return output
def test_check_simple(self): arr = np.arange(30) arr = np.reshape(arr, (6, 5)) kwargs = dict(mode='mean', stat_length=(3, )) with pytest.raises(ValueError): pad(arr, ((2, 3), (3, 2), (4, 5)), **kwargs)
def sliding_concentric_windows(self, img): n_rows, n_cols = img.shape[:2] y_a, x_a = 5, 5 y_b, x_b = 11, 11 new_img = np.zeros((n_rows, n_cols), dtype=np.uint) img_a = util.pad(img, ((y_a/2, y_a/2), (x_a/2, x_a/2)), mode='constant') img_b = util.pad(img, ((y_b/2, y_b/2), (x_b/2, x_b/2)), mode='constant') blocks_a = util.view_as_windows(img_a, (y_a, x_a), step=1) blocks_b = util.view_as_windows(img_b, (y_b, x_b), step=1) for row in xrange(n_rows): for col in xrange(n_cols): mean_a = blocks_a[row, col].mean() mean_b = blocks_b[row, col].mean() r_mean = 0 if mean_a != 0: r_mean = mean_b/float(mean_a) if r_mean > 1.0: new_img[row, col] = 0 else: new_img[row, col] = 255 return new_img
def test_check_negative_pad_amount(self): arr = np.arange(30) arr = np.reshape(arr, (6, 5)) kwargs = dict(mode='mean', stat_length=(3, )) with pytest.raises(ValueError): pad(arr, ((-2, 3), (3, 2)), **kwargs)
def test_shallow_statistic_range(self): test = np.arange(120).reshape(4, 5, 6) pad_amt = [(1, 1) for axis in test.shape] modes = ['maximum', 'mean', 'median', 'minimum', ] for mode in modes: assert_array_equal(pad(test, pad_amt, mode='edge'), pad(test, pad_amt, mode=mode, stat_length=1))
def test_clip_statistic_range(self): test = np.arange(30).reshape(5, 6) pad_amt = [(3, 3) for axis in test.shape] modes = ['maximum', 'mean', 'median', 'minimum', ] for mode in modes: assert_array_equal(pad(test, pad_amt, mode=mode), pad(test, pad_amt, mode=mode, stat_length=30))
def data_augmentation_test(img_id=1, crop_size=200, pad_size=100): Xb = np.array(Image.open('data/images/train/' + str(img_id) + '.jpg'), dtype=np.uint8) / np.float32(255.) im_size = Xb.shape[0] frame_size = im_size + 2 * pad_size print "X shape ", Xb.shape padded = np.zeros((3, frame_size, frame_size)) for i in range(3): padded[i] = pad(np.swapaxes(Xb, 0, 2)[i], (pad_size, pad_size), 'reflect') padded = np.swapaxes(padded, 0, 2) print "Padded shape ", padded.shape lower_cut = (im_size - crop_size) / 2 + pad_size upper_cut = (im_size + crop_size) / 2 + pad_size shift_x = frame_size / 2 shift_y = shift_x tf_shift = AffineTransform(translation=[-shift_x, -shift_y]) tf_shift_inv = AffineTransform(translation=[shift_x, shift_y]) scaling_factor = 0.2 * np.random.random() + 0.9 angle = 2 * pi * (np.random.random() - 0.5) trans_x = np.random.randint(-5, 5) trans_y = np.random.randint(-5, 5) tf = AffineTransform(scale=(scaling_factor, scaling_factor), rotation=angle, shear=None, translation=(trans_x, trans_y)) padded = warp(padded, (tf_shift + (tf + tf_shift_inv)).inverse) print "Padded shape after transform ", padded.shape # Crop to desired size tmp = padded[lower_cut:upper_cut, lower_cut:upper_cut, :] print "Finally, cuts and shape: ", lower_cut, upper_cut, padded.shape plt.imshow(tmp)
def gist_gabor(img, param): """ Assume single image Assume greyscale image """ w = param.number_blocks G = param.G be = param.boundary_extension n_rows, n_cols = img.shape c = 1 N = c ny, nx, n_filters = G.shape W = w*w g = np.zeros((W*n_filters, N)) # pad image img = util.pad(img, be, mode='symmetric') img = np.fft.fft2(img) k = 0 for n in range(n_filters): ig = np.abs(np.fft.ifft2(img*G[:,:,n])) ig = ig[be:ny-be, be:nx-be] v = downN(ig, w) g[k:k+W] = np.reshape(v.T, [W, N]) k = k + W return g
def test_check_median_stat_length(self): a = np.arange(100).astype('f') a[1] = 2. a[97] = 96. a = pad(a, (25, 20), 'median', stat_length=(3, 5)) b = np.array( [ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 0., 2., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54., 55., 56., 57., 58., 59., 60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76., 77., 78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92., 93., 94., 95., 96., 96., 98., 99., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96., 96.] ) assert_array_equal(a, b)
def test_check_large_pad(self): a = np.arange(12) a = np.reshape(a, (3, 4)) a = pad(a, (10, 12), 'wrap') b = np.array( [[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11], [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7], [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11], [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7], [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11], [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7], [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11], [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7], [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11], [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7], [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11], [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7], [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11], [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7], [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11], [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7], [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11]] ) assert_array_equal(a, b)
def image_padding(img, padding_value=0, crop_size=None, ismask=0): h, w = img.shape[:2] if crop_size is None: pad_h = h pad_w = w else: if h > w: pad_h = int(crop_size/2.) pad_w = int(1.*pad_h*w/h) else: pad_w = int(crop_size/2.) pad_h = int(1.*pad_w*h/w) if not ismask: return pad(img, ((pad_h, pad_h), (pad_w, pad_w), (0, 0)), mode='constant', constant_values=int(padding_value)), pad_h, pad_w else: return pad(img, ((pad_h, pad_h), (pad_w, pad_w)), mode='constant', constant_values=0), pad_h, pad_w
def adj_matrix(Graph, skel_mat): """ Returns adjacency matrix for the given skeleton binary matrix and the corresponding graph of nodes. Parameters -------- Graph : a graph of nodes; each node correspond to the nonzero element of skeleton matrix and has the attrubutes 'index' and 'neig' skel_mat : 3d binary matrix Returns ------- a_m : 2d array of the dimentions n by n, where n is equal to the number of nodes; matrix is symmetric, nonzero elements indicate the connections between the nodes. Examples ------- >>>b = np.zeros((3,3,3)) >>>b[1,1,:] = 1 >>>b array([[[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]], [[ 0., 0., 0.], [ 1., 1., 1.], [ 0., 0., 0.]], [[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]]) >>>G = label(b) >>>adj_m = adj_matrix(G,b) >>>adj_m array([[ 0., 1., 0.], [ 1., 0., 1.], [ 0., 1., 0.]]) """ point = nx.get_node_attributes(Graph, 'index') node_label = dict( (j,i) for i,j in point.iteritems()) a_pad = util.pad(skel_mat,((1,1),(1,1),(1,1)), 'constant') n = nx.number_of_nodes(Graph) a_m = np.zeros((n,n)) for i in Graph.nodes(): vol = np.zeros(a_pad.shape) co = point[i] vol[co[0]:co[0]+3, co[1]:co[1]+3, co[2]:co[2]+3] = a_pad[co[0]:co[0]+3, co[1]:co[1]+3, co[2]:co[2]+3] nz_vol = np.transpose(np.nonzero(vol)) - 1 el = tuple(map(tuple,nz_vol)) for elem in el: j = node_label[elem] a_m [i-1,j-1] = 1 a_m = a_m - np.eye(n) return a_m
def read_img_file_PIL(file_path, size=(32,32)): img = Image.open(file_path).convert('L') img.thumbnail(size, Image.NEAREST) data = np.array(img) shape = data.shape append_top = int(ceil(max(0, size[0] - shape[0])/2.0)) append_bot = int(floor(max(0, size[0] - shape[0])/2.0)) data = util.pad(data, ((append_top, append_bot), (0,0)), mode='constant', constant_values=0) return data
def center_crop_reflect(img, size): """Center crop with mirror padding if necessary""" a0 = max(0, size[0] - img.shape[0]) a1 = max(0, size[1] - img.shape[1]) v = ((a0//2, a0-a0//2), (a1//2, a1-a1//2)) if img.ndim == 3: v = v + ((0, 0),) pimg = util.pad(img, v, mode='reflect') return center_crop(pimg, size)
def test_check_median_02(self): a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]]) a = pad(a.T, 1, 'median').T b = np.array([ [5, 4, 5, 4, 5], [3, 3, 1, 4, 3], [5, 4, 5, 9, 5], [8, 9, 8, 2, 8], [5, 4, 5, 4, 5]]) assert_array_equal(a, b)
def transform(self, Xb, yb): Xb, yb = super(DataAugmentationBatchIterator, self).transform(Xb, yb) # Flip half of the images in this batch at random: bs = Xb.shape[0] indices = np.random.choice(bs, bs / 2, replace=False) Xb[indices] = Xb[indices, :, :, ::-1] # Divide pixels values by 255 to make it fit in [-1;1], for SimilarityTransform compatibility Xb /= np.float32(255.) # Change shape from [Batch_size, nb_channels, width, height] to [Batch_size, width, height, nb_channels] Xb = np.swapaxes(Xb, 1, 3) # Define relevant shapes im_size = Xb.shape[1] frame_size = im_size + 2 * self.pad_size lower_cut = (frame_size - self.crop_size) / 2 upper_cut = (frame_size + self.crop_size) / 2 shift_x = frame_size / 2 shift_y = shift_x # Necessary shifts to allow rotation around center tf_shift = SimilarityTransform(translation=[-shift_x, -shift_y]) tf_shift_inv = SimilarityTransform(translation=[shift_x, shift_y]) # Xb_new = np.zeros((bs,self.crop_size,self.crop_size,self.nb_channels)) for i in xrange(bs): pic = Xb[i] # Picture as a [width, height, nb_channels] np.array # Pad image to avoid black regions after zoom/rotation/translation padded = np.zeros((self.nb_channels, frame_size, frame_size)) for j in xrange(self.nb_channels): padded[j] = pad(np.swapaxes(pic, 0, 2)[j], (self.pad_size, self.pad_size), 'reflect') padded = np.swapaxes(padded, 0, 2) # Pick random values scaling_factor_tmp = 2 * np.random.random() * self.scale_delta + (1. - self.scale_delta) scaling_factor = random.choice([1./scaling_factor_tmp, 1., scaling_factor_tmp]) angle = 2 * pi * (np.random.random() - 0.5) * self.angle_factor trans_x = np.random.randint(-self.max_trans, self.max_trans) trans_y = np.random.randint(-self.max_trans, self.max_trans) # Apply similarity transform to zoom, rotate and translate tf = SimilarityTransform(scale=scaling_factor, rotation=angle, translation=(trans_x, trans_y)) padded = warp(padded, (tf_shift + (tf + tf_shift_inv)).inverse) # Crop to desired size Xb[i] = padded[lower_cut:upper_cut, lower_cut:upper_cut, :] Xb = np.swapaxes(Xb, 1, 3) Xb *= np.float32(255.) return Xb, yb
def test_check_2d(self): arr = np.arange(20).reshape(4, 5).astype(np.float64) test = pad(arr, (2, 2), mode='linear_ramp', end_values=(0, 0)) expected = np.array( [[0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0.5, 1., 1.5, 2., 1., 0.], [0., 0., 0., 1., 2., 3., 4., 2., 0.], [0., 2.5, 5., 6., 7., 8., 9., 4.5, 0.], [0., 5., 10., 11., 12., 13., 14., 7., 0.], [0., 7.5, 15., 16., 17., 18., 19., 9.5, 0.], [0., 3.75, 7.5, 8., 8.5, 9., 9.5, 4.75, 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0.]]) assert_allclose(test, expected)
def test_blob_dog(): r2 = math.sqrt(2) img = np.ones((512, 512)) xs, ys = circle(400, 130, 5) img[xs, ys] = 255 xs, ys = circle(100, 300, 25) img[xs, ys] = 255 xs, ys = circle(200, 350, 45) img[xs, ys] = 255 blobs = blob_dog(img, min_sigma=5, max_sigma=50) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 5 b = s[0] assert abs(b[0] - 400) <= thresh assert abs(b[1] - 130) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 300) <= thresh assert abs(radius(b) - 25) <= thresh b = s[2] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 350) <= thresh assert abs(radius(b) - 45) <= thresh # Testing no peaks img_empty = np.zeros((100,100)) assert blob_dog(img_empty).size == 0 # Testing 3D r = 10 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_dog(im3, min_sigma=3, max_sigma=10, sigma_ratio=1.2, threshold=0.1) b = blobs[0] assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1
def test_blob_overlap(): img = np.ones((256, 256), dtype=np.uint8) xs, ys = circle(100, 100, 20) img[xs, ys] = 255 xs, ys = circle(120, 100, 30) img[xs, ys] = 255 blobs = blob_doh( img, min_sigma=1, max_sigma=60, num_sigma=10, threshold=.05) assert len(blobs) == 1 r1, r2 = 7, 6 pad1, pad2 = 11, 12 blob1 = ellipsoid(r1, r1, r1) blob1 = util.pad(blob1, pad1, mode='constant') blob2 = ellipsoid(r2, r2, r2) blob2 = util.pad(blob2, [(pad2, pad2), (pad2 - 9, pad2 + 9), (pad2, pad2)], mode='constant') im3 = np.logical_or(blob1, blob2) blobs = blob_log(im3, min_sigma=2, max_sigma=10, overlap=0.1) assert len(blobs) == 1 # Two circles with distance between centers equal to radius overlap = _blob_overlap(np.array([0, 0, 10 / math.sqrt(2)]), np.array([0, 10, 10 / math.sqrt(2)])) assert_almost_equal(overlap, 1./math.pi * (2 * math.acos(1./2) - math.sqrt(3)/2.))
def test_zero_padding_shortcuts(self): test = np.arange(120).reshape(4, 5, 6) pad_amt = [(0, 0) for axis in test.shape] modes = ['constant', 'edge', 'linear_ramp', 'maximum', 'mean', 'median', 'minimum', 'reflect', 'symmetric', 'wrap', ] for mode in modes: assert_array_equal(test, pad(test, pad_amt, mode=mode))
def test_check_constant_odd_pad_amount(self): arr = np.arange(30).reshape(5, 6) test = pad(arr, ((1,), (2,)), mode='constant', constant_values=3) expected = np.array( [[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3], [ 3, 3, 0, 1, 2, 3, 4, 5, 3, 3], [ 3, 3, 6, 7, 8, 9, 10, 11, 3, 3], [ 3, 3, 12, 13, 14, 15, 16, 17, 3, 3], [ 3, 3, 18, 19, 20, 21, 22, 23, 3, 3], [ 3, 3, 24, 25, 26, 27, 28, 29, 3, 3], [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]] ) assert_allclose(test, expected)
def test_check_simple(self): a = np.arange(12) a = np.reshape(a, (4, 3)) a = pad(a, ((2, 3), (3, 2)), 'edge') b = np.array([ [0, 0, 0, 0, 1, 2, 2, 2], [0, 0, 0, 0, 1, 2, 2, 2], [0, 0, 0, 0, 1, 2, 2, 2], [3, 3, 3, 3, 4, 5, 5, 5], [6, 6, 6, 6, 7, 8, 8, 8], [9, 9, 9, 9, 10, 11, 11, 11], [9, 9, 9, 9, 10, 11, 11, 11], [9, 9, 9, 9, 10, 11, 11, 11], [9, 9, 9, 9, 10, 11, 11, 11]]) assert_array_equal(a, b)
def numb(skel_mat) : """ Counts the number of neighboring 1 for every nonzero element in 3D. Parameters ------ skel_mat : 3d binary array Returns ------ arr : The array of uint8 of the same size as skel_mat with the elements equal to the number of neighbors for the current point. Examples ------ >>> a = np.random.random_integers(0,1,(3,3,3)) >>> a array([[[0, 0, 1], [0, 1, 1], [1, 0, 1]], [[1, 0, 1], [1, 0, 1], [1, 1, 0]], [[1, 1, 1], [1, 0, 0], [1, 1, 0]]]) >>> neigh = numb(a) >>> neigh array([[[ 0, 0, 4], [ 0, 10, 6], [ 4, 0, 4]], [[ 5, 0, 6], [10, 0, 9], [ 7, 10, 0]], [[ 4, 7, 3], [ 8, 0, 0], [ 5, 6, 0]]], dtype=uint8) """ c_pad = util.pad(skel_mat,((1,1),(1,1),(1,1)), 'constant') mask = c_pad > 0 fil = 3**3 * ndimage.uniform_filter(c_pad.astype('float'), size = (3,3,3)) - 1 arr = (fil * mask)[1:-1,1:-1,1:-1].astype('uint8') return arr
def fill_holes_with_contour_filling(gray_mask,inverse=False): ''' Input: Grayscale image Returns: Image with holes filled by contour mapping ''' filled = gray_mask.copy() filled = pad(filled,((5,5),(0,0)),'constant',constant_values=255) if inverse: filled = cv2.bitwise_not(filled) image, contour, _ = cv2.findContours(filled, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) for cnt in contour: cv2.drawContours(filled,[cnt], 0, 255, -1) if inverse: filled = cv2.bitwise_not(filled) filled = crop(filled,((5,5),(0,0))) return filled
def test_check_simple(self): a = np.arange(30) a = np.reshape(a, (6, 5)) a = pad(a, ((2, 3), (3, 2)), mode='mean', stat_length=(3,)) b = np.array([[ 6, 6, 6, 5, 6, 7, 8, 9, 8, 8], [ 6, 6, 6, 5, 6, 7, 8, 9, 8, 8], [ 1, 1, 1, 0, 1, 2, 3, 4, 3, 3], [ 6, 6, 6, 5, 6, 7, 8, 9, 8, 8], [11, 11, 11, 10, 11, 12, 13, 14, 13, 13], [16, 16, 16, 15, 16, 17, 18, 19, 18, 18], [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], [26, 26, 26, 25, 26, 27, 28, 29, 28, 28], [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], [21, 21, 21, 20, 21, 22, 23, 24, 23, 23], [21, 21, 21, 20, 21, 22, 23, 24, 23, 23]]) assert_array_equal(a, b)
def test_legacy_vector_functionality(self): def _padwithtens(vector, pad_width, iaxis, kwargs): vector[:pad_width[0]] = 10 vector[-pad_width[1]:] = 10 return vector a = np.arange(6).reshape(2, 3) a = pad(a, 2, _padwithtens) b = np.array( [[10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10], [10, 10, 0, 1, 2, 10, 10], [10, 10, 3, 4, 5, 10, 10], [10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10]] ) assert_array_equal(a, b)
def test_check_constant_float(self): # If input array is int, but constant_values are float, the dtype of # the array to be padded is kept arr = np.arange(30).reshape(5, 6) test = pad(arr, (1, 2), mode='constant', constant_values=1.1) expected = np.array( [[ 1, 1, 1, 1, 1, 1, 1, 1, 1], [ 1, 0, 1, 2, 3, 4, 5, 1, 1], [ 1, 6, 7, 8, 9, 10, 11, 1, 1], [ 1, 12, 13, 14, 15, 16, 17, 1, 1], [ 1, 18, 19, 20, 21, 22, 23, 1, 1], [ 1, 24, 25, 26, 27, 28, 29, 1, 1], [ 1, 1, 1, 1, 1, 1, 1, 1, 1], [ 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) assert_allclose(test, expected)
def test_check_constant_float2(self): # If input array is float, and constant_values are float, the dtype of # the array to be padded is kept - here retaining the float constants arr = np.arange(30).reshape(5, 6) arr_float = arr.astype(np.float64) test = pad(arr_float, ((1, 2), (1, 2)), mode='constant', constant_values=1.1) expected = np.array( [[ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1], [ 1.1, 0. , 1. , 2. , 3. , 4. , 5. , 1.1, 1.1], [ 1.1, 6. , 7. , 8. , 9. , 10. , 11. , 1.1, 1.1], [ 1.1, 12. , 13. , 14. , 15. , 16. , 17. , 1.1, 1.1], [ 1.1, 18. , 19. , 20. , 21. , 22. , 23. , 1.1, 1.1], [ 1.1, 24. , 25. , 26. , 27. , 28. , 29. , 1.1, 1.1], [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1], [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]] ) assert_allclose(test, expected)
def test_blob_dog(): r2 = math.sqrt(2) img = np.ones((512, 512)) xs, ys = circle(400, 130, 5) img[xs, ys] = 255 xs, ys = circle(100, 300, 25) img[xs, ys] = 255 xs, ys = circle(200, 350, 45) img[xs, ys] = 255 blobs = blob_dog(img, min_sigma=5, max_sigma=50) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 5 b = s[0] assert abs(b[0] - 400) <= thresh assert abs(b[1] - 130) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 300) <= thresh assert abs(radius(b) - 25) <= thresh b = s[2] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 350) <= thresh assert abs(radius(b) - 45) <= thresh # Testing no peaks img_empty = np.zeros((100, 100)) assert blob_dog(img_empty).size == 0 # Testing 3D r = 10 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_dog(im3, min_sigma=3, max_sigma=10, sigma_ratio=1.2, threshold=0.1) b = blobs[0] assert b.shape == (4, ) assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1 # Testing 3D anisotropic r = 10 pad = 10 im3 = ellipsoid(r / 2, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_dog(im3, min_sigma=[1.5, 3, 3], max_sigma=[5, 10, 10], sigma_ratio=1.2, threshold=0.1) b = blobs[0] assert b.shape == (6, ) assert b[0] == r / 2 + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r / 2) < 1 assert abs(math.sqrt(3) * b[4] - r) < 1 assert abs(math.sqrt(3) * b[5] - r) < 1 # Testing exclude border # image where blob is 5 px from borders, radius 5 img = np.ones((512, 512)) xs, ys = circle(5, 5, 5) img[xs, ys] = 255 blobs = blob_dog( img, min_sigma=1.5, max_sigma=5, sigma_ratio=1.2, ) assert blobs.shape[0] == 1 b = blobs[0] assert b[0] == b[1] == 5, "blob should be 5 px from x and y borders" blobs = blob_dog(img, min_sigma=1.5, max_sigma=5, sigma_ratio=1.2, exclude_border=5) msg = "zero blobs should be detected, as only blob is 5 px from border" assert blobs.shape[0] == 0, msg
def rgb(folder): palm_location_file = "/s/red/a/nobackup/cwc/skeleton/ChaLearn17/frames/palm_location/valid/%03d/K_%05d.npy" image_root = "/s/red/a/nobackup/cwc/skeleton/ChaLearn17/frames/valid/%03d/M_%05d" hand_root = "/s/red/a/nobackup/cwc/skeleton/ChaLearn17/frames/hands/rgb/individual/valid/%03d/M_%05d" start = (folder - 1) * 200 + 1 end = start + 200 print start, end for video in range(start, end): hand_dir = hand_root % (folder, video) try: os.makedirs(hand_dir) except: pass palm_location = np.load(palm_location_file % (folder, video)) depth_images = get_all_frames(image_root % (folder, video),False) print folder, video, palm_location.shape, depth_images.shape, hand_dir image_name = ["%d_right.png", "%d_left.png"] for frame, (palm, image) in enumerate(zip(palm_location, depth_images)): for i in range(2): if np.sum(palm[i]) != -2: x_start = palm[i, 0] - 55 x_end = x_start + 110 y_start = palm[i, 1] - 55 y_end = y_start + 110 width = [[0, 0], [0, 0],[0,0]] #print frame, if x_end > 240: width[0][1] = x_end - 240 x_end = 240 if y_end > 320: width[1][1] = y_end - 320 y_end = 320 if x_start < 0: width[0][0] = -x_start x_start = 0 if y_start < 0: width[1][0] = -y_start y_start = 0 hand = np.copy(image[x_start:x_end, y_start:y_end]) hand = pad(hand, width, 'constant', constant_values=255) hand = hand.astype("uint8") skimage.io.imsave(os.path.join(hand_dir, image_name[i] % frame), hand) #plt.imshow(hand) #plt.show() print
def corner_subpix(image, corners, window_size=11, alpha=0.99): """Determine subpixel position of corners. Parameters ---------- image : ndarray Input image. corners : (N, 2) ndarray Corner coordinates `(row, col)`. window_size : int, optional Search window size for subpixel estimation. alpha : float, optional Significance level for point classification. Returns ------- positions : (N, 2) ndarray Subpixel corner positions. NaN for "not classified" corners. References ---------- .. [1] http://www.ipb.uni-bonn.de/uploads/tx_ikgpublication/\ foerstner87.fast.pdf .. [2] http://en.wikipedia.org/wiki/Corner_detection Examples -------- >>> from skimage.feature import corner_harris, corner_peaks, corner_subpix >>> img = np.zeros((10, 10)) >>> img[:5, :5] = 1 >>> img[5:, 5:] = 1 >>> img.astype(int) array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]]) >>> coords = corner_peaks(corner_harris(img), min_distance=2) >>> coords_subpix = corner_subpix(img, coords, window_size=7) >>> coords_subpix array([[ 4.5, 4.5]]) """ # window extent in one direction wext = (window_size - 1) // 2 image = pad(image, pad_width=wext, mode='constant', constant_values=0) # add pad width, make sure to not modify the input values in-place corners = corners + wext # normal equation arrays N_dot = np.zeros((2, 2), dtype=np.double) N_edge = np.zeros((2, 2), dtype=np.double) b_dot = np.zeros((2, ), dtype=np.double) b_edge = np.zeros((2, ), dtype=np.double) # critical statistical test values redundancy = window_size**2 - 2 t_crit_dot = stats.f.isf(1 - alpha, redundancy, redundancy) t_crit_edge = stats.f.isf(alpha, redundancy, redundancy) # coordinates of pixels within window y, x = np.mgrid[-wext:wext + 1, -wext:wext + 1] corners_subpix = np.zeros_like(corners, dtype=np.double) for i, (y0, x0) in enumerate(corners): # crop window around corner + border for sobel operator miny = y0 - wext - 1 maxy = y0 + wext + 2 minx = x0 - wext - 1 maxx = x0 + wext + 2 window = image[miny:maxy, minx:maxx] winx, winy = _compute_derivatives(window, mode='constant', cval=0) # compute gradient suares and remove border winx_winx = (winx * winx)[1:-1, 1:-1] winx_winy = (winx * winy)[1:-1, 1:-1] winy_winy = (winy * winy)[1:-1, 1:-1] # sum of squared differences (mean instead of gaussian filter) Axx = np.sum(winx_winx) Axy = np.sum(winx_winy) Ayy = np.sum(winy_winy) # sum of squared differences weighted with coordinates # (mean instead of gaussian filter) bxx_x = np.sum(winx_winx * x) bxx_y = np.sum(winx_winx * y) bxy_x = np.sum(winx_winy * x) bxy_y = np.sum(winx_winy * y) byy_x = np.sum(winy_winy * x) byy_y = np.sum(winy_winy * y) # normal equations for subpixel position N_dot[0, 0] = Axx N_dot[0, 1] = N_dot[1, 0] = -Axy N_dot[1, 1] = Ayy N_edge[0, 0] = Ayy N_edge[0, 1] = N_edge[1, 0] = Axy N_edge[1, 1] = Axx b_dot[:] = bxx_y - bxy_x, byy_x - bxy_y b_edge[:] = byy_y + bxy_x, bxx_x + bxy_y # estimated positions est_dot = np.linalg.solve(N_dot, b_dot) est_edge = np.linalg.solve(N_edge, b_edge) # residuals ry_dot = y - est_dot[0] rx_dot = x - est_dot[1] ry_edge = y - est_edge[0] rx_edge = x - est_edge[1] # squared residuals rxx_dot = rx_dot * rx_dot rxy_dot = rx_dot * ry_dot ryy_dot = ry_dot * ry_dot rxx_edge = rx_edge * rx_edge rxy_edge = rx_edge * ry_edge ryy_edge = ry_edge * ry_edge # determine corner class (dot or edge) # variance for different models var_dot = np.sum(winx_winx * ryy_dot - 2 * winx_winy * rxy_dot \ + winy_winy * rxx_dot) var_edge = np.sum(winy_winy * ryy_edge + 2 * winx_winy * rxy_edge \ + winx_winx * rxx_edge) # test value (F-distributed) t = var_edge / var_dot # 1 for edge, -1 for dot, 0 for "not classified" corner_class = (t < t_crit_edge) - (t > t_crit_dot) if corner_class == -1: corners_subpix[i, :] = y0 + est_dot[0], x0 + est_dot[1] elif corner_class == 0: corners_subpix[i, :] = np.nan, np.nan elif corner_class == 1: corners_subpix[i, :] = y0 + est_edge[0], x0 + est_edge[1] # subtract pad width corners_subpix -= wext return corners_subpix
def describe(self, image, test_image: bool, sigma_psd=70/255, cutoff_freq=10, a=0.75, b=1.0): if isinstance(image, DatasetManager.Image): if test_image: image_data = image.test_data else: image_data = image.data else: image_data = image.copy() # Perform BM3D Filter image_bm3d_filtered = SharedFunctions.bm3d_filter(image_data, 70/255) # Perform Homomorphic filter, Note: This requires images to be normalised in range [0, 255] image_scaled_255 = ImageUtils.convert_float32_image_uint8(image_data) cutoff, a, b = 10, 0.75, 0.1 image_homomorphic_filtered = SharedFunctions.homomorphic_filter(image_scaled_255, cutoff, a, b) image_homomorphic_filtered = ImageUtils.convert_uint8_image_float32(image_homomorphic_filtered) # Perform Median filter. Padding is required for median filter. image_padded = pad(array=image_data, pad_width=1, mode='constant', constant_values=0) image_median_filtered = np.zeros(image_padded.shape, dtype=np.float32) SharedFunctions.median_filter(image_padded, 3, 1, image_median_filtered) image_median_filtered = image_median_filtered[1:-1, 1:-1] # Remove padding now median filter done # Subtract original image from filtered image to get noise only bm3d_noise = image_data - image_bm3d_filtered homomorphic_noise = image_data - image_homomorphic_filtered median_noise = image_data - image_median_filtered if self.save_img: if isinstance(image, DatasetManager.Image): GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(image_bm3d_filtered), os.path.join('BM3DELBP', 'NoiseClassifier', 'Filtered Images'), '{}-{}-{}-BM3D-filtered.png'.format(image.name, image.test_noise, image.test_noise_val)) GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(image_homomorphic_filtered), os.path.join('BM3DELBP', 'NoiseClassifier', 'Filtered Images'), '{}-{}-{}-homomorphic-filtered-cutoff_{}-a_{}-b_{}.png'.format(image.name, image.test_noise, image.test_noise_val, cutoff, a, b)) GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(image_median_filtered), os.path.join('BM3DELBP', 'NoiseClassifier', 'Filtered Images'), '{}-{}-{}-median-filtered.png'.format(image.name, image.test_noise, image.test_noise_val)) GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(bm3d_noise), os.path.join('BM3DELBP', 'NoiseClassifier', 'Noise Estimates'), '{}-{}-{}-BM3D-noise-estimate.png'.format(image.name, image.test_noise, image.test_noise_val)) GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(homomorphic_noise), os.path.join('BM3DELBP', 'NoiseClassifier', 'Noise Estimates'), '{}-{}-{}-homomorphic-filtered-cutoff_{}-a_{}-b_{}.png'.format(image.name, image.test_noise, image.test_noise_val, cutoff, a, b)) GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(median_noise), os.path.join('BM3DELBP', 'NoiseClassifier', 'Noise Estimates'), '{}-{}-{}-median-noise-estimate.png'.format(image.name, image.test_noise, image.test_noise_val)) else: raise ValueError('save_img set but not passed as DatasetManager.Image or BM3DELBPImage') kurtosis_bm3d = kurtosis(a=bm3d_noise, axis=None, fisher=False, nan_policy='raise') skewness_bm3d = skew(a=bm3d_noise, axis=None, nan_policy='raise') kurtosis_homomorphic = kurtosis(a=homomorphic_noise, axis=None, fisher=False, nan_policy='raise') skewness_homomorphic = skew(a=homomorphic_noise, axis=None, nan_policy='raise') kurtosis_median = kurtosis(a=median_noise, axis=None, fisher=False, nan_policy='raise') skewness_median = skew(a=median_noise, axis=None, nan_policy='raise') if GlobalConfig.get('debug'): print("BM3D Filtered Kurtosis:", kurtosis_bm3d, ", Skewness: ", skewness_bm3d) print("Homomorphic Filtered Kurtosis:", kurtosis_homomorphic, ", Skewness: ", skewness_homomorphic) print("Median Filtered Kurtosis:", kurtosis_median, ", Skewness: ", skewness_median) # Generate image featurevector of 6 characteristics featurevector = np.array([kurtosis_bm3d, skewness_bm3d, kurtosis_homomorphic, skewness_homomorphic, kurtosis_median, skewness_median]) return featurevector
avg, std = image.mean(), image.std() logging.info(f"intensity: {avg:.4f}+-{std:.4f}") image = image > avg + std # remove small spot image = median(image, disk(1)) # rotate +90d image = rotate(image, 90, resize=True, preserve_range=True) imageio.imwrite("sobel.tif", image.astype(np.uint8)) # pad to destination shape src_shape = image.shape[:2] # potentially colored pad_shape = tuple(d - s for d, s in zip(dst_shape, src_shape)) # update pad shape to before/after pad_shape = tuple((p // 2, p - p // 2) for p in pad_shape) logging.info(f"pad width: {pad_shape}") if len(image.shape) == 3: # color dimension does not need padding pad_shape += ((0, 0), ) image = pad(image, pad_shape, mode="constant", constant_values=0) # gray scale image = rescale_intensity(image, out_range=np.uint8) image = image.astype(np.uint8) logging.info(f"{len(image > 0)} active pixels") imageio.imwrite("hashimoto_slm_simple.bmp", image) ##
# Read in image and convert it to boolean image = util.img_as_bool(io.imread(sys.argv[1])) # Read in the structuring element (SE) and convert it to boolean se = util.img_as_bool(io.imread(sys.argv[2])) # Get the height and width of the SE se_height, se_width = se.shape # Find the center indices pad_v = se_height // 2 pad_h = se_width // 2 # Pad the input image with numpy.util.pad padded = util.pad(image, (pad_v, pad_h), mode="constant") # Create output image out = numpy.zeros(image.shape, dtype=bool) # Loop through the image (follow image indices) for i in range(image.shape[0]): for j in range(image.shape[1]): # Retrieve subimage from padded image (not index-aligned with the image) subimage = padded[i:i + se_height, j:j + se_width] # Determine whether or not we have an overlap at the current position # We use numpy.array_equal to determine if product of the se and the subimage # is the same as the se out[i, j] = numpy.array_equal(subimage * se, se) # Save output image
model = load_model(path_to_model) # get input shape of model _, input_rows, input_cols, input_channels = model.layers[0].input_shape _, output_classes = model.layers[-1].output_shape in_rows_half = int(input_rows/2) in_cols_half = int(input_cols/2) # import correct preprocessing if input_channels is 3: from image_functions import preprocessing_image_rgb as preprocessing_image else: from image_functions import preprocessing_image_ms as preprocessing_image # pad image image = pad(image, ((input_rows, input_rows), (input_cols, input_cols), (0, 0)), 'symmetric') # don't forget to preprocess image = preprocessing_image(image) num_rows, num_cols, _ = image.shape # sliding window over image image_classified_prob = np.zeros((num_rows, num_cols, output_classes)) row_images = np.zeros((num_cols_unpadded, input_rows, input_cols, input_channels)) for row in tqdm(range(input_rows, num_rows-input_rows), desc="Processing..."): # get all images along one row for idx, col in enumerate(range(input_cols, num_cols-input_cols)): # cut smal image patch row_images[idx, ...] = image[row-in_rows_half:row+in_rows_half,
def save_canny_save_fit(path, sig, low, high): #sig=3,low = 0, high = 30 zstack = io.imread(path) image = img_as_ubyte(zstack[:, :, 0]) mask = image > 20 fprefix = os.path.basename(path).strip('.jpg') bn = fprefix + '_CANNY.jpg' current_dir = os.getcwd() cannysavepath = current_dir + '/' + plate_dir.strip('/') + '_canny/' + bn edges = canny(image, sigma=sig, low_threshold=low, high_threshold=high) #plt.imsave(fname=str(p + fprefix + '_ubyte.jpg'), arr=image) #plt.imsave(fname=str(p + fprefix + '_bounded.jpg'), arr=mask) bn = fprefix + '_dropfinder2.jpg' fitsavepath = current_dir + '/' + plate_dir.strip('/') + '_fit/' + bn try: plt.imsave(fname=cannysavepath, arr=edges) plt.close() except FileNotFoundError: print('FileNotFoundError: making ' + plate_dir.strip('/') + '_canny') os.mkdir(current_dir + '/' + plate_dir.strip('/') + '_canny/') print('writing ' + cannysavepath) plt.imsave(fname=cannysavepath, arr=edges) plt.close() print(cannysavepath) accum_d, cx_d, cy_d, radii_d = circular_hough_transform( 135, 145, 2, edges, 1) #edge detection on drop accum_w, cx_w, cy_w, radii_w = circular_hough_transform( 479, 495, 1, edges, 1) #edge detection on well try: im = image padsize = np.array([(0, 100), (0, 100)]) pad_im = pad(image, padsize, mode='constant') fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(20, 8)) draw_circles_on_image(pad_im, cx_d, cy_d, radii_d, 255, 2) draw_circles_on_image(pad_im, cx_w, cy_w, radii_w, 255, 2) ### Code to draw the inner circle r_inner = 0.73701 * radii_w r_inner = r_inner.astype(int) draw_circles_on_image(pad_im, cx_w, cy_w, r_inner, 255, 2) cx = 641 cy = 553 rad = 487 c, r = circle_perimeter(cx, cy, rad) #pad_im[r,c] = 255 #pad_im[cx-2:cx+2,cy-2:cy+2]=255 try: plt.imsave(fname=fitsavepath, arr=pad_im, cmap='Greys_r') plt.close() except FileNotFoundError: print('making ' + plate_dir.strip('/') + '_fit/') os.mkdir(current_dir + '/' + plate_dir.strip('/') + '_fit/') print('writing' + bn) plt.imsave(fname=fitsavepath, arr=pad_im, cmap='Greys_r') plt.close() print(fitsavepath) except IndexError: print('IndexError' + ' ' + fitsavepath) #plt.imshow(pad_im) return cx_d, cy_d, radii_d, cx_w, cy_w, radii_w
def iradonT(radon_image, theta=None, output_size=None, filter="ramp", interpolation="linear", circle=False): """ Inverse radon transform. Reconstruct an image from the radon transform, using the filtered back projection algorithm. Parameters ---------- radon_image : array_like, dtype=float Image containing radon transform (sinogram). Each column of the image corresponds to a projection along a different angle. The tomography rotation axis should lie at the pixel index ``radon_image.shape[0] // 2`` along the 0th dimension of ``radon_image``. theta : array_like, dtype=float, optional Reconstruction angles (in degrees). Default: m angles evenly spaced between 0 and 180 (if the shape of `radon_image` is (N, M)). output_size : int Number of rows and columns in the reconstruction. filter : str, optional (default ramp) Filter used in frequency domain filtering. Ramp filter used by default. Filters available: ramp, shepp-logan, cosine, hamming, hann. Assign None to use no filter. interpolation : str, optional (default 'linear') Interpolation method used in reconstruction. Methods available: 'linear', 'nearest', and 'cubic' ('cubic' is slow). circle : boolean, optional Assume the reconstructed image is zero outside the inscribed circle. Also changes the default output_size to match the behaviour of ``radon`` called with ``circle=True``. Returns ------- reconstructed : ndarray Reconstructed image. The rotation axis will be located in the pixel with indices ``(reconstructed.shape[0] // 2, reconstructed.shape[1] // 2)``. Notes ----- It applies the Fourier slice theorem to reconstruct an image by multiplying the frequency domain of the filter with the FFT of the projection data. This algorithm is called filtered back projection. """ if radon_image.ndim != 2: raise ValueError('The input image must be 2-D') if theta is None: m, n = radon_image.shape theta = np.linspace(0, 180, n, endpoint=False) else: theta = np.asarray(theta) if len(theta) != radon_image.shape[1]: raise ValueError("The given ``theta`` does not match the number of " "projections in ``radon_image``.") interpolation_types = ('linear', 'nearest', 'cubic') if not interpolation in interpolation_types: raise ValueError("Unknown interpolation: %s" % interpolation) if not output_size: # If output size not specified, estimate from input radon image if circle: output_size = radon_image.shape[0] else: output_size = int( np.floor(np.sqrt((radon_image.shape[0])**2 / 2.0))) if circle: radon_image = _sinogram_circle_to_square(radon_image) th = (np.pi / 180.0) * theta # resize image to next power of two (but no less than 64) for # Fourier analysis; speeds up Fourier and lessens artifacts projection_size_padded = \ max(64, int(2**np.ceil(np.log2(2 * radon_image.shape[0])))) pad_width = ((0, projection_size_padded - radon_image.shape[0]), (0, 0)) img = util.pad(radon_image, pad_width, mode='constant', constant_values=0) # Construct the Fourier filter #delta = 1 l1 = (2 * np.pi)**(-4 / 5) * (delta)**(8 / 5) / 5 l2 = (2 * np.pi)**(-4 / 5) * (delta)**(-2 / 5) * 4 / 5 f = fftfreq(projection_size_padded).reshape(-1, 1) # digital frequency omega = 2 * np.pi * f # angular frequency fourier_filter = 2 * np.abs(f) # ramp filter if filter == "ramp": pass elif filter == "tigran": g = fftfreq(projection_size_padded).reshape(-1, 1) w = abs(omega) g[1:] = l2 / (l1 * ( (w[1:])**5 / (2 * np.pi)) + l2) + np.sqrt(l1 * l2) * (w[1:])**2 * np.sqrt(l1 * ( (w[1:])**5 / (2 * np.pi)) + l2 - w[1:] / (2 * np.pi)) / (l1 * ((w[1:])**5 / (2 * np.pi)) + l2) fourier_filter[1:] = fourier_filter[1:] * g[1:] elif filter == "shepp-logan": # Start from first element to avoid divide by zero fourier_filter[1:] = fourier_filter[1:] * np.sin(omega[1:]) / omega[1:] elif filter == "cosine": fourier_filter *= np.cos(omega) elif filter == "hamming": fourier_filter *= (0.54 + 0.46 * np.cos(omega / 2)) elif filter == "hann": fourier_filter *= (1 + np.cos(omega / 2)) / 2 elif filter is None: fourier_filter[:] = 1 else: raise ValueError("Unknown filter: %s" % filter) # Apply filter in Fourier domain projection = fft(img, axis=0) * fourier_filter radon_filtered = np.real(ifft(projection, axis=0)) # Resize filtered image back to original size radon_filtered = radon_filtered[:radon_image.shape[0], :] reconstructed = np.zeros((output_size, output_size)) # Determine the center of the projections (= center of sinogram) mid_index = radon_image.shape[0] // 2 [X, Y] = np.mgrid[0:output_size, 0:output_size] xpr = X - int(output_size) // 2 ypr = Y - int(output_size) // 2 # Reconstruct image by interpolation for i in range(len(theta)): t = ypr * np.cos(th[i]) - xpr * np.sin(th[i]) x = np.arange(radon_filtered.shape[0]) - mid_index if interpolation == 'linear': backprojected = np.interp(t, x, radon_filtered[:, i], left=0, right=0) else: interpolant = interp1d(x, radon_filtered[:, i], kind=interpolation, bounds_error=False, fill_value=0) backprojected = interpolant(t) reconstructed += backprojected if circle: radius = output_size // 2 reconstruction_circle = (xpr**2 + ypr**2) <= radius**2 reconstructed[~reconstruction_circle] = 0. return reconstructed * np.pi / (2 * len(th))
def pad_clip(clip, h, w): im_h, im_w = clip[0].shape[:2] pad_h = (0, 0) if h < im_h else ((h - im_h) // 2, (h - im_h + 1) // 2) pad_w = (0, 0) if w < im_w else ((w - im_w) // 2, (w - im_w + 1) // 2) return pad(clip, ((0, 0), pad_h, pad_w, (0, 0)), mode='edge')
def nlm(img, t, f, h): # img = img_as_float(img) [m, n] = img.shape img_denoised = np.zeros((m, n)) h = h * h # Normalization kernel = np.zeros((2 * f + 1, 2 * f + 1)) for d in range(1, f + 1): value = 1. / ((2 * d + 1) * (2 * d + 1)) for i in range(-d, d + 1): for j in range(-d, d + 1): kernel[f - i, f - j] = kernel[f - i, f - j] + value kernel = kernel / f kernel = kernel / sum(sum(kernel)) vkernel = np.reshape(kernel, (2 * f + 1) * (2 * f + 1)) pdimg = util.pad(img, ((f, f)), mode='symmetric') # padding #Denoising for i in range(0, m): for j in range(0, n): i1 = i + f j1 = j + f W1 = pdimg[range(i1 - f, i1 + f + 1), :] W1 = W1[:, range(j1 - f, j1 + f + 1)] wmax = 0 average = 0 sweight = 0 rmin = max(i1 - t, f) rmax = min(i1 + t, m + f - 1) smin = max(j1 - t, f) smax = min(j1 + t, n + f - 1) # Find similarity between neighborhoods W1(center) and W2(surrounding) for r in range(rmin, rmax + 1): for s in range(smin, smax + 1): if ((r == i1) and (s == j1)): continue W2 = pdimg[range(r - f, r + f + 1), :] W2 = W2[:, range(s - f, s + f + 1)] # Use L2-norm temp = np.reshape(np.square(W1 - W2), (2 * f + 1) * (2 * f + 1)) d = np.dot(vkernel, temp) w = np.exp(-d / h) if (w > wmax): wmax = w sweight = sweight + w average = average + w * pdimg[r, s] average = average + wmax * pdimg[i1, j1] sweight = sweight + wmax # Calculation of the weight # Compute value of the denoised pixel if (sweight > 0): img_denoised[i, j] = average / sweight else: img_denoised[i, j] = img[i, j] return img_denoised
def test_zero_pad_width(self): arr = np.arange(30) arr = np.reshape(arr, (6, 5)) for pad_width in (0, (0, 0), ((0, 0), (0, 0))): assert_array_equal(arr, pad(arr, pad_width, mode='constant'))
def calc_training_patches(self, save=False): ps = self.conf.patch_size my_gaze = self.conf.myGaze_fg scale_factor = self.conf.scale_factor seen_patches = list() unseen_patches = list() selem = morphology.square(5) print('Getting seen patches') with progressbar.ProgressBar(maxval=len(self.conf.frameFileNames)) as bar: for i in range(len(self.conf.frameFileNames)): bar.update(i) img = utls.imread(self.conf.frameFileNames[i]) img = (color.rgb2gray(img)*255).astype(np.uint8) img = median(img, selem=selem) # Uses square of size 3 ci_seen, cj_seen = gaze.gazeCoord2Pixel( my_gaze[i, 3], my_gaze[i, 4], img.shape[1], img.shape[0]) i_, j_ = self.get_centers_negatives(ci_seen, cj_seen, ps, self.conf.overlap_ratio, img.shape) img_padded = pad(img,((ps,ps),),mode='symmetric') seen_patch = img_padded[int(ci_seen + ps/2):int(ci_seen + 3*ps/2),int(cj_seen + ps/2):int(cj_seen + 3*ps/2)] seen_patch_mean = np.mean(seen_patch) seen_patch_std = np.std(seen_patch) if(seen_patch_std == 0): seen_patch_std = 1 seen_patch = (seen_patch - seen_patch_mean) / seen_patch_std seen_patch = rescale( seen_patch, scale=scale_factor, order=1, mode='reflect', preserve_range=True) seen_patches.append((i, ci_seen, cj_seen, seen_patch.ravel())) for k in range(i_.shape[0]): unseen_patch = img[int(i_[k]- ps / 2):int(i_[k] + ps / 2), int( j_[k]- ps / 2):int(j_[k]+ ps / 2)] unseen_patch_mean = np.mean(unseen_patch) unseen_patch_std = np.std(unseen_patch) if(unseen_patch_std == 0): unseen_patch_std = 1 unseen_patch = (unseen_patch - unseen_patch_mean) / unseen_patch_std unseen_patch = rescale( unseen_patch, scale=scale_factor, order=1, mode='reflect', preserve_range=True) unseen_patches.append((i, i_[k], j_[k], unseen_patch.ravel())) if(save): seen_patches_df = pd.DataFrame(seen_patches, columns=['frame', 'c_i', 'c_j', 'patch']) unseen_patches_df = pd.DataFrame(unseen_patches, columns=['frame', 'c_i', 'c_j', 'patch']) save_path = os.path.join(self.conf.dataOutDir, 'vilar') if(not os.path.exists(save_path)): os.mkdir(save_path) seen_patches_df.to_pickle(os.path.join(save_path, 'vilar_seen_patches_df.p')) unseen_patches_df.to_pickle( os.path.join(save_path, 'vilar_unseen_patches_df.p')) return True
def denoising(noisy_image, learning_image, window_shape, window_step, sigma, learning_ratio=0.1, ksvd_iter=1): # 1. Form noisy patches. padded_noisy_image = pad(noisy_image, pad_width=window_shape, mode='symmetric') noisy_patches, noisy_patches_shape = patch_matrix_windows( padded_noisy_image, window_shape, window_step) padded_lea_image = pad(learning_image, pad_width=window_shape, mode='symmetric') lea_patches, lea_patches_shape = patch_matrix_windows( padded_lea_image, window_shape, window_step) print('Shape of dataset : ' + str(noisy_patches.shape)) # 2. Form explanatory dictionary. k = int(learning_ratio * lea_patches.shape[1]) indexes = np.random.random_integers(0, lea_patches.shape[1] - 1, k) basis = lea_patches[:, indexes] basis /= np.sum(basis.T.dot(basis), axis=-1) print('Shape of dictionary : ' + str(basis.shape) + '\n') # 3. Compute K-SVD. start = timeit.default_timer() #-------------------------------- basis_final, sparse_final, n_total = k_svd(basis, noisy_patches, sigma, single_channel_omp, ksvd_iter) txtHandle = open(r".\dictionary.txt", 'w+') print('dictionary saving ~') np.save(r".\TestTemp\dictionary.npy", sparse_final) np.save(r".\TestTemp\phi.npy", basis_final) #-------------------------------- stop = timeit.default_timer() print("Calculation time : " + str(stop - start) + ' seconds.') # 4. Reconstruct the image. patches_approx = basis_final.dot(sparse_final) tx = open(r".\size.txt", "w") tx.close() tx = open(r".\size.txt", "a") tx.write(str(padded_noisy_image.shape)) tx.write(str(noisy_patches_shape)) tx.write(str(window_step)) tx.write(str(patches_approx.shape)) tx.close() padded_denoised_image = image_reconstruction_windows( padded_noisy_image.shape, patches_approx, noisy_patches_shape, window_step) shrunk_0, shrunk_1 = tuple( map(sub, padded_denoised_image.shape, window_shape)) denoised_image = np.abs(padded_denoised_image)[window_shape[0]:shrunk_0, window_shape[1]:shrunk_1] plt.imshow(Image.fromarray(np.uint8(denoised_image))) plt.show() #recover info return denoised_image, stop - start, n_total
def select_random_augmentation(img): f = np.random.choice(augmenters) return f(img) if __name__ == '__main__': # TODO parse args parser = argparse.ArgumentParser() parser.add_argument('image', help='image name') parser.add_argument('--pad-size', default=10) args = parser.parse_args() images = [] original_img = io.imread(args.image) padded_img = util.pad(original_img, args.pad_size, 'constant', constant_values=255) images.append(padded_img) # iaa.GaussianBlur blur = iaa.GaussianBlur(sigma=(0, 1.0)) blur_img = blur.augment_image(padded_img) images.append(blur_img) # downsample (PIL) LANCZOS downsample_aug = iaa.Lambda(func_images=downsample_func(0.5)) downsampled = downsample_aug.augment_image(padded_img) images.append(downsampled) # downsample + stretch ??? # morphology.closing ???
def reflect_pad(img): return pad( resize(img, (101 * 2, 101 * 2), mode='constant', preserve_range=True), 27, 'reflect')
def ARC(kspace, AtA, kernel_size, c, lamda): '''ARC.''' sx, sy, ncoils = kspace.shape[:] kx, ky = kernel_size[:] # Zero-pad data px = int(kx / 2) py = int(ky / 2) kspace = pad( # pylint: disable=E1102 kspace, ((px, px), (py, py), (0, 0)), mode='constant') dummyK = np.zeros((kx, ky, ncoils)) dummyK[int(kx / 2), int(ky / 2), c] = 1 idxy = np.where(dummyK) res = np.zeros((sx, sy), dtype=kspace.dtype) MaxListLen = 100 # max number of kernels we'll store for lookup LIST = np.zeros((kx * ky * ncoils, MaxListLen), dtype=kspace.dtype) KEY = np.zeros((kx * ky * ncoils, MaxListLen)) count = 0 # current index for the next kernel to store in LIST for xy in np.ndindex((sx, sy)): x, y = xy[:] tmp = kspace[x:x + kx, y:y + ky, :] pat = np.abs(tmp) > 0 if pat[idxy]: # If we aquired this k-space sample, use it! res[x, y] = tmp[idxy].squeeze() else: # If we didn't aquire it, let's either look up the # kernel or compute a new one key = pat.flatten() # If we have a matching kernel, the key will exist # in our array of KEYs. This little loop looks through # the list to find if we have the kernel already idx = 0 for nn in range(1, KEY.shape[1] + 1): if np.sum(key == KEY[:, nn - 1]) == key.size: idx = nn break if idx == 0: # If we didn't find a matching kernel, compute one. # We'll only hold MaxListLen kernels in the lookup # at one time to save on memory and lookup time count += 1 kernel = calibrate(AtA, kernel_size, ncoils, c, lamda, pat)[0].flatten() KEY[:, np.mod(count, MaxListLen)] = key LIST[:, np.mod(count, MaxListLen)] = kernel else: # If we found it, use it! kernel = LIST[:, idx - 1] # Apply kernel weights to coil data for interpolation res[x, y] = np.sum(kernel * tmp.flatten()) return res
def test_check_02(self): a = pad([1, 2, 3], 4, 'wrap') b = np.array([3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1]) assert_array_equal(a, b)
def process_study(study_id, in_train_set, isotropic_volumes_folder, volumes_metadata, annotations_grouped, out_dir, config): dimensions, patchsize, scaling, multiple, padding, offcenter, rotation, view = config isometric_volume = np.load('../data_proc/stage1/{}/{}.npy'.format(isotropic_volumes_folder, study_id)) mean = np.mean(isometric_volume).astype(np.float32) std = np.std(isometric_volume).astype(np.float32) resize_factor = np.divide(volumes_metadata['volume_resampled_shape'], volumes_metadata['volume_shape']) nodules = [] for i, group in enumerate(annotations_grouped): data = [a['data'] for a in group] z0 = int(round(resize_factor[0] * min([a['sliceNum'] for a in group]))) z1 = int(round(resize_factor[0] * max([a['sliceNum'] for a in group])) + 1) y0 = int(round(resize_factor[1] * min([d['y'] for d in data]))) y1 = int(round(resize_factor[1] * max([(d['y'] + d['height']) for d in data])) + 1) x0 = int(round(resize_factor[2] * min([d['x'] for d in data]))) x1 = int(round(resize_factor[2] * max([(d['x'] + d['width']) for d in data])) + 1) # add padding to patch if specified z0 = max(0, int(z0 - ((z1 - z0) * padding / 100))) z1 = min(isometric_volume.shape[0], int(z1 + ((z1 - z0) * padding / 100))) y0 = max(0, int(y0 - ((y1 - y0) * padding / 100))) y1 = min(isometric_volume.shape[1], int(y1 + ((y1 - y0) * padding / 100))) x0 = max(0, int(x0 - ((x1 - x0) * padding / 100))) x1 = min(isometric_volume.shape[2], int(x1 + ((x1 - x0) * padding / 100))) if scaling == 'original': if (z1 - z0) < patchsize: z0 -= (patchsize - (z1 - z0)) // 2 z1 += (patchsize - (z1 - z0)) - (patchsize - (z1 - z0)) // 2 if (y1 - y0) < patchsize: y0 -= (patchsize - (y1 - y0)) // 2 y1 += (patchsize - (y1 - y0)) - (patchsize - (y1 - y0)) // 2 if (x1 - x0) < patchsize: x0 -= (patchsize - (x1 - x0)) // 2 x1 += (patchsize - (x1 - x0)) - (patchsize - (x1 - x0)) // 2 shape = (z1-z0, y1-y0, x1-x0) nodule = isometric_volume[z0:z1, y0:y1, x0:x1] nodules.append(nodule) if len(nodules) == 0: return 0, [] if multiple == 'largest': nodule_sizes_sorted = np.argsort([np.prod(nodule.shape) for nodule in nodules]) nodules = [nodules[nodule_sizes_sorted[-1]]] if scaling == 'stretch': nodules = [resize(nodule, [patchsize, patchsize, patchsize], mode='edge', clip=True, preserve_range=True) for nodule in nodules] elif scaling == 'original': nodules_reshaped = [] for nodule in nodules: pad_width = [] for s in nodule.shape: if s < patchsize: pad_width.append(((patchsize - s) // 2, (patchsize - s) - (patchsize - s) // 2)) else: pad_width.append((0, 0)) crop_width = [] for s in nodule.shape: if s > patchsize: crop_width.append(((s - patchsize) // 2, (s - patchsize) - (s - patchsize) // 2)) else: crop_width.append((0, 0)) nodules_reshaped.append(crop(pad(nodule, pad_width, mode='minimum'), crop_width)) nodules = nodules_reshaped nodules = [(nodule.astype(np.float32) - mean) / (std + 1e-7) for nodule in nodules] if offcenter > 0: offcenter_range = range(-int(patchsize * offcenter / 100), int(patchsize * offcenter / 100)) else: offcenter_range = [0] patches = [] if dimensions == '2d': for nodule in nodules: for i in offcenter_range: if view == 'axial': patches.append(np.expand_dims(nodule[int(round(i + nodule.shape[0] / 2)), :, :], axis=2)) else: patches.append(np.expand_dims(nodule[int(round(i + nodule.shape[0] / 2)), :, :], axis=2)) patches.append(np.expand_dims(nodule[:, int(round(i + nodule.shape[1] / 2)), :], axis=2)) patches.append(np.expand_dims(nodule[:, :, int(round(i + nodule.shape[2] / 2))], axis=2)) elif dimensions == '3d': patches = [np.expand_dims(nodule, axis=3) for nodule in nodules] if in_train_set: dirname1 = 'train' else: dirname1 = 'val' dirname2 = str(labels.ix[study_id, 'cancer']) for patch in patches: out_filepath = os.path.join(out_dir, dirname1, dirname2, '{}.npy'.format(uuid4())) np.save(out_filepath, patch) return len(patches), [nodule.shape for nodule in nodules]
def test_check_large_pad(self): a = np.arange(12) a = np.reshape(a, (3, 4)) a = pad(a, (10, 12), 'wrap') b = np.array([[ 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11 ], [ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 ], [ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7 ], [ 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11 ], [ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 ], [ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7 ], [ 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11 ], [ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 ], [ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7 ], [ 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11 ], [ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 ], [ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7 ], [ 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11 ], [ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 ], [ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7 ], [ 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11 ], [ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 ], [ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7 ], [ 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11 ], [ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 ], [ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7 ], [ 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11 ], [ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 ], [ 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7 ], [ 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11 ]]) assert_array_equal(a, b)
def test_check_03(self): a = pad([1, 2, 3], 4, 'reflect') b = np.array([1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3]) assert_array_equal(a, b)
def test_check_median_02(self): a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]]) a = pad(a.T, 1, 'median').T b = np.array([[5, 4, 5, 4, 5], [3, 3, 1, 4, 3], [5, 4, 5, 9, 5], [8, 9, 8, 2, 8], [5, 4, 5, 4, 5]]) assert_array_equal(a, b)
def depth_segment(): palm_location_file = "/s/red/a/nobackup/cwc/skeleton/ChaLearn17/frames/palm_location/valid/%03d/K_%05d.npy" image_root = "/s/red/a/nobackup/cwc/skeleton/ChaLearn17/frames/valid/%03d/K_%05d" hand_root = "/s/red/a/nobackup/cwc/skeleton/ChaLearn17/frames/hands/individual/valid/%03d/K_%05d" for video in range(5600,5600): folder = ((video - 1) / 200) + 1 hand_dir = hand_root % (folder, video) try: os.makedirs(hand_dir) except: pass palm_location = np.load(palm_location_file % (folder, video)) depth_images = get_all_frames(image_root % (folder, video)) print folder, video, palm_location.shape, depth_images.shape, hand_dir image_name = ["%d_right.png", "%d_left.png"] for frame, (palm, image) in enumerate(zip(palm_location, depth_images)): for i in range(2): if np.sum(palm[i]) != -2: x_start = palm[i, 0] - 50 x_end = x_start + 100 y_start = palm[i, 1] - 50 y_end = y_start + 100 width = [[0, 0], [0, 0]] #if y_end<=320: # continue print frame, if x_end > 240: width[0][1] = x_end - 240 x_end = 240 if y_end > 320: width[1][1] = y_end - 320 y_end = 320 if x_start < 0: width[0][0] = -x_start x_start = 0 if y_start < 0: width[1][0] = -y_start y_start = 0 palm_depth = image[palm[i, 0], palm[i, 1]] translate = 0.5 - palm_depth hand = np.copy(image[x_start:x_end, y_start:y_end]) + translate hand = pad(hand, width, 'constant', constant_values=1) palm_depth_max = 0.5 + 0.1 palm_depth_min = 0.5 - 0.2 hand[hand > palm_depth_max] = 1 hand[hand < palm_depth_min] = 1 binary_hand = np.copy(hand) binary_hand[binary_hand != 1] = 2 labels = measure.label(binary_hand) palm_label = labels[50, 50] labels[labels != palm_label] = 0 hand[labels == 0] = 1 else: hand = np.ones((100, 100)) hand *= 255 hand = hand.astype("uint8") skimage.io.imsave(os.path.join(hand_dir, image_name[i] % frame), hand) # plt.show() print
min_height = np.min(heights) to_crop_im = [np.ceil((((ims_flat[i].shape[0]-min_height)/2,(ims_flat[i].shape[0]-min_height)/2), ((ims_flat[i].shape[1]-min_width)/2,(ims_flat[i].shape[1]-min_width)/2),(0,0))) for i in range(len(ims_flat))] ims_crop = [util.crop(ims_flat[i], to_crop_im[i]) for i in range(len(ims_flat))] preds_my_crop = [util.crop(preds_my_flat[i], to_crop_im[i]) for i in range(len(ims_flat))] preds_true_crop = [util.crop(preds_true_flat[i], to_crop_im[i]) for i in range(len(ims_flat))] f_size = ims_crop[0].shape ims_res = [(transform.resize(ims_crop[i], f_size)*255).astype(np.uint8) for i in range(len(ims_crop))] preds_my_res = [(transform.resize(preds_my_crop[i], f_size)*255).astype(np.uint8) for i in range(len(ims_crop))] preds_true_res = [(transform.resize(preds_true_crop[i], f_size)*255).astype(np.uint8) for i in range(len(ims_crop))] pw = 10 ims_pad = [util.pad(ims_res[i], ((pw,pw),(pw,pw),(0,0)), mode='constant', constant_values=255) for i in range(len(ims_res))] preds_my_pad = [util.pad(preds_my_res[i], ((pw,pw),(pw,pw),(0,0)), mode='constant', constant_values=255) for i in range(len(ims_res))] preds_true_pad = [util.pad(preds_true_res[i], ((pw,pw),(pw,pw),(0,0)), mode='constant', constant_values=255) for i in range(len(ims_res))] all_ = np.concatenate([np.concatenate([ims_pad[i], preds_my_pad[i], preds_true_pad[i]], axis=1) for i in range(len(ims_pad))], axis=0) all_ = np.split(all_, 2, axis=0) all_ = np.concatenate(all_,axis=1) #Write methods on image header_height = 150 header = np.ones((header_height,all_.shape[1],3)).astype(np.uint8)*255 all_with_header = np.concatenate((header, all_),axis=0) im_path = os.path.join(out_result_dir, 'all_learning.png') print('Saving to: ' + im_path) io.imsave(im_path, all_with_header)
def edge_pad(img): return pad( resize(img, (101 * 2, 101 * 2), mode='constant', preserve_range=True), 27, 'edge')
# imsave("./data/mask.png", mask.astype(np.float) ) # exit() # initialization image = img_as_float(imread('./data/image.png')[:, :, 0:3]) init_image = image.copy() init_image[mask, :] = resize( pyramid_expand(pyramid_reduce(image, 10, multichannel=True), 10, multichannel=True), image.shape)[mask, :] true_init = make_patch_collage([image, image, init_image], 3) w1 = true_init.shape[1] true_init = make_patch_collage([image, init_image], 2) w2 = true_init.shape[1] true_init = pad(true_init, ((0, 0), ((w1 - w2) // 2, (w1 - w2) // 2 + 1), (0, 0)), mode='edge') imsave(output_dir + "true_init.png", true_init) # exit() # # 3rd party solutions # im3rdparty = [] # # im3rdparty.append(img_as_float(imread('output/3rdparty/fedorov_means_p15.png'))) # im3rdparty.append(img_as_float(imread('output/3rdparty/fedorov_poisson_p15_lmd001.png'))) # im3rdparty.append(img_as_float(imread('output/3rdparty/crim_p9.png'))) # im3rdparty.append(img_as_float(imread('output/3rdparty/newson.png'))) # im3rdparty.append(img_as_float(imread('output/3rdparty/cntx_att_places2.png'))) # im3rdparty.append(img_as_float(imread('output/3rdparty/ec_places2.png'))) # im3rdparty.append(img_as_float(imread('output/3rdparty/gmcnn_places2.png'))) # imsave(output_dir+"3rdparty/3rdparty.png", make_patch_collage(im3rdparty, len(im3rdparty)) ) # exit()
# Let's look a a single image before padding. # In[ ]: plt.imshow(seis, cmap='gray') # And let's compare it with different padded ones # In[ ]: filter_size = 99 from skimage.util import pad plt.imshow(pad(seis, 7, mode='symmetric', reflect_type='odd'), cmap='gray') # In[ ]: filter_size = 99 from skimage.util import pad plt.imshow(pad(seis, 7, mode='symmetric', reflect_type='even'), cmap='gray') # In[ ]: filter_size = 99 from skimage.util import pad
def createContourFromMask(self, mask, bbox): """ It creates the contour (and the corrisponding polygon) from the blob mask. """ # NOTE: The mask is expected to be cropped around its bbox (!!) (see the __init__) self.inner_contours.clear() # we need to pad the mask to avoid to break the contour that touches the borders PADDED_SIZE = 4 img_padded = pad(mask, (PADDED_SIZE, PADDED_SIZE), mode="constant", constant_values=(0, 0)) contours = measure.find_contours(img_padded, 0.6) inner_contours = measure.find_contours(img_padded, 0.4) number_of_contours = len(contours) threshold = 20 #min number of points in a small hole if number_of_contours > 1: # search the longest contour npoints_max = 0 longest = 0 for i, contour in enumerate(contours): npoints = contour.shape[0] if npoints > npoints_max: npoints_max = npoints longest = i npoints_max = 0 inner_longest = 0 for i, contour in enumerate(inner_contours): npoints = contour.shape[0] if npoints > npoints_max: npoints_max = npoints inner_longest = i # divide the contours in OUTER contour and INNER contours for i, contour in enumerate(contours): if i == longest: self.contour = np.array(contour) for i, contour in enumerate(inner_contours): if i != inner_longest: if contour.shape[0] > threshold: coordinates = np.array(contour) self.inner_contours.append(coordinates) # adjust the coordinates of the outer contour # (NOTE THAT THE COORDINATES OF THE BBOX ARE IN THE GLOBAL MAP COORDINATES SYSTEM) for i in range(self.contour.shape[0]): ycoor = self.contour[i, 0] xcoor = self.contour[i, 1] self.contour[i, 0] = xcoor - PADDED_SIZE + bbox[1] self.contour[i, 1] = ycoor - PADDED_SIZE + bbox[0] # adjust coordinates of the INNER contours for j, contour in enumerate(self.inner_contours): for i in range(contour.shape[0]): ycoor = contour[i, 0] xcoor = contour[i, 1] self.inner_contours[j][i, 0] = xcoor - PADDED_SIZE + bbox[1] self.inner_contours[j][i, 1] = ycoor - PADDED_SIZE + bbox[0] elif number_of_contours == 1: coords = measure.approximate_polygon(contours[0], tolerance=0.2) self.contour = np.array(coords) # adjust the coordinates of the outer contour # (NOTE THAT THE COORDINATES OF THE BBOX ARE IN THE GLOBAL MAP COORDINATES SYSTEM) for i in range(self.contour.shape[0]): ycoor = self.contour[i, 0] xcoor = self.contour[i, 1] self.contour[i, 0] = xcoor - PADDED_SIZE + bbox[1] self.contour[i, 1] = ycoor - PADDED_SIZE + bbox[0] else: raise Exception("Empty contour") #TODO optimize the bbox self.bbox = bbox
def test_blob_log(): r2 = math.sqrt(2) img = np.ones((256, 256)) xs, ys = circle(200, 65, 5) img[xs, ys] = 255 xs, ys = circle(80, 25, 15) img[xs, ys] = 255 xs, ys = circle(50, 150, 25) img[xs, ys] = 255 xs, ys = circle(100, 175, 30) img[xs, ys] = 255 blobs = blob_log(img, min_sigma=5, max_sigma=20, threshold=1) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 3 b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing log scale blobs = blob_log(img, min_sigma=5, max_sigma=20, threshold=1, log_scale=True) b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing no peaks img_empty = np.zeros((100, 100)) assert blob_log(img_empty).size == 0 # Testing 3D r = 6 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_log(im3, min_sigma=3, max_sigma=10) b = blobs[0] assert b.shape == (4, ) assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1 # Testing 3D anisotropic r = 6 pad = 10 im3 = ellipsoid(r / 2, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_log( im3, min_sigma=[1, 2, 2], max_sigma=[5, 10, 10], ) b = blobs[0] assert b.shape == (6, ) assert b[0] == r / 2 + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r / 2) < 1 assert abs(math.sqrt(3) * b[4] - r) < 1 assert abs(math.sqrt(3) * b[5] - r) < 1 # Testing exclude border # image where blob is 5 px from borders, radius 5 img = np.ones((512, 512)) xs, ys = circle(5, 5, 5) img[xs, ys] = 255 blobs = blob_log( img, min_sigma=1.5, max_sigma=5, ) assert blobs.shape[0] == 1 b = blobs[0] assert b[0] == b[1] == 5, "blob should be 5 px from x and y borders" blobs = blob_dog(img, min_sigma=1.5, max_sigma=5, exclude_border=5) msg = "zero blobs should be detected, as only blob is 5 px from border" assert blobs.shape[0] == 0, msg
def detect_ads(self): #doesn't work for dataset3 def adjust_time(start_stop_time): (t1, t2) = start_stop_time if t2 - t1 - 15 > 1: t2 = t1 + 15 elif t2 - t1 <15: t2 = t1 + 15 return [t1, t2] print('Detecting ads for {}...'.format(self.name)) self.arr = self.audio.to_soundarray()[:, 0] self.blocksize = np.array([self.Fs][0]).reshape(1, ) # np.array(44100).lreshape((-1,1)) self.w = 8 # window size if self.arr.shape[0] % self.Fs != 0: # self.arr=audiosegment.from_file(self.source).resample(sample_rate_Hz=48000, sample_width=2, channels=1).to_numpy_array() padding = int(np.abs((self.arr.shape[0] % self.Fs - self.Fs))) # self.padding = (padding,0) self.padding = (padding + self.Fs, self.Fs) else: # self.padding = 0 self.padding = (self.Fs, self.Fs) #padded = util.pad(self.arr, self.padding, mode='constant') #blocks = util.view_as_blocks(padded, tuple(self.blocksize, )) blocks = util.view_as_blocks(self.arr, tuple(self.blocksize,)) blocked_dct = np.zeros(blocks.shape[0]) fft_blocks=np.zeros((blocks.shape[0],24000)) '''for i in np.arange(blocks.shape[0]): # - 1): blocked_dct[i] = np.sum(np.abs(fftpack.fft(blocks[i][11000:12000]))) fft_blocks.append(fftpack.fft(blocks[i]) self.fft=fft_blocks''' for i in np.arange(blocks.shape[0]): # - 1): blocked_dct[i] = np.abs(fftpack.dct(blocks[i])[0]) fft_blocks[i]=np.abs(fftpack.fft(blocks[i]))[0:24000] self.dct = blocked_dct self.fft=fft_blocks diff_dct = np.diff(blocked_dct) # ,append=blocked_dct[-2])#,prepend=blocked_dct[1])#,append=blocked_dct[-1]) diff2_dct = np.diff(diff_dct, n=2) # ,append=blocked_dct[-2])#prepend=diff_dct[1])#,append=diff_dct[-1]) x, y = smooth_approx(diff2_dct) xthreshold, xpeaks, xpeak_idxs = self.get_peaks(y) shift = 0 diff2_dct_slices = util.view_as_windows(y, window_shape=(self.w,), step=1) sliced_peaks = np.zeros(diff2_dct_slices.shape[0] + shift) self.Tmax = sliced_peaks.shape[0] self.scale = self.Tmax / self.duration diff2_dct_thresh = np.max(diff2_dct) * .1 for i in np.arange(sliced_peaks.shape[0] - shift): sliced_peaks[i + shift] = np.max(diff2_dct_slices[i]) if np.abs(sliced_peaks[i]) < diff2_dct_thresh: sliced_peaks[i] = 0 sliced_peaks = util.pad(sliced_peaks, (self.w, 0), 'symmetric') threshold, peaks, peak_idxs = self.get_peaks(sliced_peaks) self.sliced_peaks = sliced_peaks self.threshold = threshold '''plt.figure() plt.plot(x, y, ':') plt.title('{}: Peaks for smooth_approx(diff2_dct)'.format(self.name)) plt.ylabel('Magnitude of 2nd Order Diff') plt.xlabel('Time (s)') plt.plot(np.arange(len(sliced_peaks)), sliced_peaks, '-.') plt.scatter(peak_idxs, peaks)''' #thresholded = group_consecutive(np.where(self.sliced_peaks > 0)[0].tolist()) #thresholded = [grp for grp in thresholded if len(grp) >= 15] idxs = group_consecutive(peak_idxs) start_stop_times = [[np.min(x), np.max(x)] for x in idxs] start_stop_times = [adjust_time(x) for x in start_stop_times] self.ad_times = start_stop_times return start_stop_times#, start_stop_times_rev # use reversed to make adjustments to intervals if needed