本文整理汇总了Python中cv2.BORDER_REFLECT属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.BORDER_REFLECT属性的具体用法?Python cv2.BORDER_REFLECT怎么用?Python cv2.BORDER_REFLECT使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.BORDER_REFLECT属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _elastic
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def _elastic(image, p, alpha=None, sigma=None, random_state=None):
"""Elastic deformation of images as described in [Simard2003]_ (with modifications).
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Proc. of the International Conference on Document Analysis and
Recognition, 2003.
Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5
From:
https://www.kaggle.com/bguberfain/elastic-transform-for-data-augmentation
"""
if random.random() > p:
return image
if alpha == None:
alpha = image.shape[0] * random.uniform(0.5,2)
if sigma == None:
sigma = int(image.shape[0] * random.uniform(0.5,1))
if random_state is None:
random_state = np.random.RandomState(None)
shape = image.shape[:2]
dx, dy = [cv2.GaussianBlur((random_state.rand(*shape) * 2 - 1) * alpha, (sigma|1, sigma|1), 0) for _ in range(2)]
x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
x, y = np.clip(x+dx, 0, shape[1]-1).astype(np.float32), np.clip(y+dy, 0, shape[0]-1).astype(np.float32)
return cv2.remap(image, x, y, interpolation=cv2.INTER_LINEAR, borderValue= 0, borderMode=cv2.BORDER_REFLECT)
示例2: get_mag_avg
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def get_mag_avg(img):
img = np.sqrt(img)
kernels = get_kernels()
mag = np.zeros(img.shape, dtype='float32')
for kernel_filter in kernels:
gx = cv2.filter2D(np.float32(img), cv2.CV_32F, kernel_filter[1], borderType=cv2.BORDER_REFLECT)
gy = cv2.filter2D(np.float32(img), cv2.CV_32F, kernel_filter[0], borderType=cv2.BORDER_REFLECT)
mag += cv2.magnitude(gx, gy)
mag /= len(kernels)
return np.uint8(mag)
示例3: conv_tri
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def conv_tri(src, radius):
"""
Image convolution with a triangle filter.
:param src: input image
:param radius: gradient normalization radius
:return: convolution result
"""
if radius == 0:
return src
elif radius <= 1:
p = 12.0 / radius / (radius + 2) - 2
kernel = N.asarray([1, p, 1], dtype=N.float64) / (p + 2)
return cv2.sepFilter2D(src, ddepth=-1, kernelX=kernel, kernelY=kernel,
borderType=cv2.BORDER_REFLECT)
else:
radius = int(radius)
kernel = range(1, radius + 1) + [radius + 1] + range(radius, 0, -1)
kernel = N.asarray(kernel, dtype=N.float64) / (radius + 1) ** 2
return cv2.sepFilter2D(src, ddepth=-1, kernelX=kernel, kernelY=kernel,
borderType=cv2.BORDER_REFLECT)
示例4: predict8tta
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def predict8tta(model, batch, sigmoid):
ret = []
for cls in TTA:
ret.append(cls(sigmoid)(model, batch))
scale_tta = False
if scale_tta:
for scale in [0.8, 1.25]:
data = np.moveaxis(np.squeeze(batch.numpy()[0]), 0, -1)
srows, scols = data.shape[:2]
data = cv2.resize(data, (0, 0), fx=scale, fy=scale)
rows, cols = data.shape[:2]
data = cv2.copyMakeBorder(data, 0, (32-rows%32), 0, (32-cols%32), cv2.BORDER_REFLECT)
data = np.expand_dims(np.moveaxis(data, -1, 0), 0)
data = torch.from_numpy(data)
for cls in TTA:
r = (cls(sigmoid)(model, data))
r = np.moveaxis(np.squeeze(r), 0, -1)
r = r[:rows, :cols, ...]
r = cv2.resize(r, (scols, srows))
r = np.expand_dims(np.moveaxis(r, -1, 0), 0)
ret.append(r)
return np.moveaxis(np.mean(ret, axis=0), 1, -1)
示例5: random_translate_img
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def random_translate_img(img, xy_range, border_mode="constant"):
if random.random() > xy_range.chance:
return img
import cv2
if not isinstance(img, list):
img = [img]
org_height, org_width = img[0].shape[:2]
translate_x = random.randint(xy_range.x_min, xy_range.x_max)
translate_y = random.randint(xy_range.y_min, xy_range.y_max)
trans_matrix = numpy.float32([[1, 0, translate_x], [0, 1, translate_y]])
border_const = cv2.BORDER_CONSTANT
if border_mode == "reflect":
border_const = cv2.BORDER_REFLECT
res = []
for img_inst in img:
img_inst = cv2.warpAffine(img_inst, trans_matrix, (org_width, org_height), borderMode=border_const)
res.append(img_inst)
if len(res) == 1:
res = res[0]
xy_range.last_x = translate_x
xy_range.last_y = translate_y
return res
示例6: affine
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def affine(img, angle, translate, scale, shear, interpolation=cv2.INTER_LINEAR, mode=cv2.BORDER_CONSTANT, fillcolor=0):
"""Apply affine transformation on the image keeping image center invariant
Args:
img (numpy ndarray): numpy ndarray to be transformed.
angle (float or int): rotation angle in degrees between -180 and 180, clockwise direction.
translate (list or tuple of integers): horizontal and vertical translations (post-rotation translation)
scale (float): overall scale
shear (float): shear angle value in degrees between -180 to 180, clockwise direction.
interpolation (``cv2.INTER_NEAREST` or ``cv2.INTER_LINEAR`` or ``cv2.INTER_AREA``, ``cv2.INTER_CUBIC``):
An optional resampling filter.
See `filters`_ for more information.
If omitted, it is set to ``cv2.INTER_CUBIC``, for bicubic interpolation.
mode (``cv2.BORDER_CONSTANT`` or ``cv2.BORDER_REPLICATE`` or ``cv2.BORDER_REFLECT`` or ``cv2.BORDER_REFLECT_101``)
Method for filling in border regions.
Defaults to cv2.BORDER_CONSTANT, meaning areas outside the image are filled with a value (val, default 0)
val (int): Optional fill color for the area outside the transform in the output image. Default: 0
"""
if not _is_numpy_image(img):
raise TypeError('img should be numpy Image. Got {}'.format(type(img)))
assert isinstance(translate, (tuple, list)) and len(translate) == 2, \
"Argument translate should be a list or tuple of length 2"
assert scale > 0.0, "Argument scale should be positive"
output_size = img.shape[0:2]
center = (img.shape[1] * 0.5 + 0.5, img.shape[0] * 0.5 + 0.5)
matrix = _get_affine_matrix(center, angle, translate, scale, shear)
if img.shape[2]==1:
return cv2.warpAffine(img, matrix, output_size[::-1],interpolation, borderMode=mode, borderValue=fillcolor)[:,:,np.newaxis]
else:
return cv2.warpAffine(img, matrix, output_size[::-1],interpolation, borderMode=mode, borderValue=fillcolor)
示例7: affine
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def affine(img, angle, translate, scale, shear, interpolation=cv2.INTER_LINEAR, mode=cv2.BORDER_CONSTANT, fillcolor=0):
"""Apply affine transformation on the image keeping image center invariant
Args:
img (numpy ndarray): numpy ndarray to be transformed.
angle (float or int): rotation angle in degrees between -180 and 180, clockwise direction.
translate (list or tuple of integers): horizontal and vertical translations (post-rotation translation)
scale (float): overall scale
shear (float): shear angle value in degrees between -180 to 180, clockwise direction.
interpolation (``cv2.INTER_NEAREST` or ``cv2.INTER_LINEAR`` or ``cv2.INTER_AREA``, ``cv2.INTER_CUBIC``):
An optional resampling filter.
See `filters`_ for more information.
If omitted, it is set to ``cv2.INTER_LINEAR``, for bilinear interpolation.
mode (``cv2.BORDER_CONSTANT`` or ``cv2.BORDER_REPLICATE`` or ``cv2.BORDER_REFLECT`` or ``cv2.BORDER_REFLECT_101``)
Method for filling in border regions.
Defaults to cv2.BORDER_CONSTANT, meaning areas outside the image are filled with a value (val, default 0)
val (int): Optional fill color for the area outside the transform in the output image. Default: 0
"""
if not _is_numpy_image(img):
raise TypeError('img should be numpy Image. Got {}'.format(type(img)))
assert isinstance(translate, (tuple, list)) and len(translate) == 2, \
"Argument translate should be a list or tuple of length 2"
assert scale > 0.0, "Argument scale should be positive"
output_size = img.shape[0:2]
center = (img.shape[1] * 0.5 + 0.5, img.shape[0] * 0.5 + 0.5)
matrix = _get_affine_matrix(center, angle, translate, scale, shear)
if img.shape[2]==1:
return cv2.warpAffine(img, matrix, output_size[::-1],interpolation, borderMode=mode, borderValue=fillcolor)[:,:,np.newaxis]
else:
return cv2.warpAffine(img, matrix, output_size[::-1],interpolation, borderMode=mode, borderValue=fillcolor)
示例8: rnd_warp
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def rnd_warp(a):
h, w = a.shape[:2]
T = np.zeros((2, 3))
coef = 0.2
ang = (np.random.rand()-0.5)*coef
c, s = np.cos(ang), np.sin(ang)
T[:2, :2] = [[c,-s], [s, c]]
T[:2, :2] += (np.random.rand(2, 2) - 0.5)*coef
c = (w/2, h/2)
T[:,2] = c - np.dot(T[:2, :2], c)
return cv2.warpAffine(a, T, (w, h), borderMode = cv2.BORDER_REFLECT)
示例9: border_frame
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def border_frame(frame, border_size, border_type):
"""Convenience wrapper of cv2.copyMakeBorder for how vidstab applies borders
:param frame: frame to apply border to
:param border_size: int border size in number of pixels
:param border_type: one of the following ['black', 'reflect', 'replicate']
:return: bordered version of frame with alpha layer for frame overlay options
"""
border_modes = {'black': cv2.BORDER_CONSTANT,
'reflect': cv2.BORDER_REFLECT,
'replicate': cv2.BORDER_REPLICATE}
border_mode = border_modes[border_type]
bordered_frame_image = cv2.copyMakeBorder(frame.image,
top=border_size,
bottom=border_size,
left=border_size,
right=border_size,
borderType=border_mode,
value=[0, 0, 0])
bordered_frame = Frame(bordered_frame_image, color_format=frame.color_format)
alpha_bordered_frame = bordered_frame.bgra_image
alpha_bordered_frame[:, :, 3] = 0
h, w = frame.image.shape[:2]
alpha_bordered_frame[border_size:border_size + h, border_size:border_size + w, 3] = 255
return alpha_bordered_frame, border_mode
示例10: __call__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def __call__(self, image, *args):
size=self.size
if self.type=='constant':
image = cv2.copyMakeBorder(image, size, size, size, size, cv2.BORDER_CONSTANT, value=self.constant_color)
elif self.type=='reflect':
image = cv2.copyMakeBorder(image, size, size, size, size, cv2.BORDER_REFLECT)
elif self.type=='replicate':
image = cv2.copyMakeBorder(image, size, size, size, size, cv2.BORDER_REPLICATE)
if len(args):
return (image, *args)
else:
return image
示例11: random_rotate_with_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def random_rotate_with_mask(image, mask, max_angle):
cols = image.shape[1]
rows = image.shape[0]
angle = random.uniform(-max_angle, max_angle)
M = cv2.getRotationMatrix2D((cols // 2, rows // 2), angle, 1)
dst = cv2.warpAffine(image, M, (cols, rows), borderMode=cv2.BORDER_REFLECT)
dst_msk = cv2.warpAffine(mask, M, (cols, rows), borderMode=cv2.BORDER_REFLECT)
return dst, dst_msk
示例12: get_features
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def get_features(self, src, smp_loc):
bottom, right = (4 - src.shape[0] % 4) % 4, (4 - src.shape[1] % 4) % 4
src = cv2.copyMakeBorder(src, 0, bottom, 0, right,
borderType=cv2.BORDER_REFLECT)
reg_ch, ss_ch = self.get_shrunk_channels(src)
smp_loc = self.get_shrunk_loc(smp_loc)
reg_ftr = self.get_reg_ftr(reg_ch, smp_loc)
ss_ftr = self.get_ss_ftr(ss_ch, smp_loc)
return reg_ftr, ss_ftr
示例13: _random_warp
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def _random_warp(self, img):
h, w = img.shape[:2]
T = np.zeros((2, 3))
coef = 0.2
ang = (np.random.rand() - 0.5) * coef
c, s = np.cos(ang), np.sin(ang)
T[:2, :2] = [[c, -s], [s, c]]
T[:2, :2] += (np.random.rand(2, 2) - 0.5) * coef
c = (w / 2, h / 2)
T[:, 2] = c - np.dot(T[:2, :2], c)
return cv2.warpAffine(img, T, (w, h), borderMode=cv2.BORDER_REFLECT)
示例14: reflect_border
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def reflect_border(self, image, b=12):
return cv2.copyMakeBorder(image, b, b, b, b, cv2.BORDER_REFLECT)
示例15: finalyze
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REFLECT [as 别名]
def finalyze(self, data):
rows, cols = data.shape[:2]
return cv2.copyMakeBorder(data, 0, (32-rows%32), 0, (32-cols%32), cv2.BORDER_REFLECT)