本文整理汇总了Python中skimage.util.pad函数的典型用法代码示例。如果您正苦于以下问题:Python pad函数的具体用法?Python pad怎么用?Python pad使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pad函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prefilt
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
示例2: test_check_negative_pad_amount
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)
示例3: test_check_wrong_pad_amount
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)
示例4: test_pad_too_many_axes
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')
示例5: test_check_simple
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)
示例6: sliding_concentric_windows
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
示例7: test_shallow_statistic_range
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))
示例8: test_clip_statistic_range
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))
示例9: data_augmentation_test
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)
示例10: test_check_median_stat_length
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)
示例11: gist_gabor
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
示例12: test_check_large_pad
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,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 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)
示例13: image_padding
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
示例14: adj_matrix
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
示例15: read_img_file_PIL
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