本文整理汇总了Python中cv2.BORDER_REPLICATE属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.BORDER_REPLICATE属性的具体用法?Python cv2.BORDER_REPLICATE怎么用?Python cv2.BORDER_REPLICATE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.BORDER_REPLICATE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def __init__(self, max_deg, center_range=(0, 1),
interp=cv2.INTER_LINEAR,
border=cv2.BORDER_REPLICATE, step_deg=None, border_value=0):
"""
Args:
max_deg (float): max abs value of the rotation angle (in degree).
center_range (tuple): (min, max) range of the random rotation center.
interp: cv2 interpolation method
border: cv2 border method
step_deg (float): if not None, the stepping of the rotation
angle. The rotation angle will be a multiple of step_deg. This
option requires ``max_deg==180`` and step_deg has to be a divisor of 180)
border_value: cv2 border value for border=cv2.BORDER_CONSTANT
"""
assert step_deg is None or (max_deg == 180 and max_deg % step_deg == 0)
super(Rotation, self).__init__()
self._init(locals())
示例2: affine_skew
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def affine_skew(self, tilt, phi, img, mask=None):
h, w = img.shape[:2]
if mask is None:
mask = np.zeros((h, w), np.uint8)
mask[:] = 255
A = np.float32([[1, 0, 0], [0, 1, 0]])
if phi != 0.0:
phi = np.deg2rad(phi)
s, c = np.sin(phi), np.cos(phi)
A = np.float32([[c, -s], [s, c]])
corners = [[0, 0], [w, 0], [w, h], [0, h]]
tcorners = np.int32(np.dot(corners, A.T))
x, y, w, h = cv2.boundingRect(tcorners.reshape(1, -1, 2))
A = np.hstack([A, [[-x], [-y]]])
img = cv2.warpAffine(img, A, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE)
if tilt != 1.0:
s = 0.8*np.sqrt(tilt * tilt - 1)
img = cv2.GaussianBlur(img, (0, 0), sigmaX=s, sigmaY=0.01)
img = cv2.resize(img, (0, 0), fx=1.0 / tilt, fy=1.0, interpolation=cv2.INTER_NEAREST)
A[0] /= tilt
if phi != 0.0 or tilt != 1.0:
h, w = img.shape[:2]
mask = cv2.warpAffine(mask, A, (w, h), flags=cv2.INTER_NEAREST)
Ai = cv2.invertAffineTransform(A)
return img, mask, Ai
示例3: distort_affine_cv2
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def distort_affine_cv2(image, alpha_affine=10, random_state=None):
if random_state is None:
random_state = np.random.RandomState(None)
shape = image.shape
shape_size = shape[:2]
center_square = np.float32(shape_size) // 2
square_size = min(shape_size) // 3
pts1 = np.float32([
center_square + square_size,
[center_square[0] + square_size, center_square[1] - square_size],
center_square - square_size])
pts2 = pts1 + random_state.uniform(-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)
M = cv2.getAffineTransform(pts1, pts2)
distorted_image = cv2.warpAffine(
image, M, shape_size[::-1], borderMode=cv2.BORDER_REPLICATE) #cv2.BORDER_REFLECT_101)
return distorted_image
示例4: transform_state
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def transform_state(t, **kwargs):
if callable(t):
t_vars = vars(t)
if 'random_state' in kwargs and 'random' in t_vars:
t.__dict__['random'] = kwargs['random_state']
support = ['fillval', 'anchor', 'prob', 'mean', 'std', 'outside']
for arg in kwargs:
if arg in t_vars and arg in support:
t.__dict__[arg] = kwargs[arg]
if 'mode' in kwargs and 'mode' in t_vars:
t.__dict__['mode'] = kwargs['mode']
if 'border' in kwargs and 'border' in t_vars:
t.__dict__['border'] = BorderTypes.get(kwargs['border'], cv2.BORDER_REPLICATE)
if 'transforms' in t_vars:
t.__dict__['transforms'] = transforms_state(t.transforms, **kwargs)
return t
示例5: __init__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def __init__(self, tx=(-0.1, 0.1), ty=None, border='constant', fillval=0, anchor=None, random_state=np.random):
if isinstance(tx, numbers.Number):
tx = (-abs(tx), abs(tx))
assert isinstance(tx, tuple) and np.abs(tx).max() < 1
if ty is None:
ty = tx
elif isinstance(ty, numbers.Number):
ty = (-abs(ty), abs(ty))
assert isinstance(ty, tuple) and np.abs(ty).max() < 1
self.tx, self.ty = tx, ty
if isinstance(fillval, numbers.Number):
fillval = [fillval] * 3
self.border = BorderTypes.get(border, cv2.BORDER_REPLICATE)
self.fillval = fillval
self.anchor = anchor
self.random = random_state
示例6: padImg
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def padImg(img):
'''
pad image twice.
The first padding is to make sure the patches cover all image regions.
The second padding is used for cropping the global patch.
'''
H = img.shape[0]
W = img.shape[1]
globalFct = 4
patchRes = 256
ovlp = int(patchRes * 0.25)
padH = (int((H - patchRes)/(patchRes - ovlp) + 1) * (patchRes - ovlp) + patchRes) - H
padW = (int((W - patchRes)/(patchRes - ovlp) + 1) * (patchRes - ovlp) + patchRes) - W
padding = int(patchRes * (globalFct - 1) / 2.0)
padImg = cv2.copyMakeBorder(img, 0, padH, 0, padW, cv2.BORDER_REPLICATE)
padImg = cv2.copyMakeBorder(padImg, padding, padding, padding, padding, cv2.BORDER_REPLICATE)
return padImg
示例7: preprocess
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def preprocess(image, width, height):
# Grab the dimensions of the image, then initialize the padding values
(h, w) = image.shape[:2]
# If the width is greater than the height then resize along the width
if w > h:
image = imutils.resize(image, width=width)
# Otherwise, the height is greater than the width so resize along the height
else:
image = imutils.resize(image, height=height)
# Determine the padding values for the width and height to obtain the target dimensions
pad_w = int((width - image.shape[1]) / 2.0)
pad_h = int((height - image.shape[0]) / 2.0)
# Pad the image then apply one more resizing to handle any rounding issues
image = cv2.copyMakeBorder(image, pad_h, pad_h, pad_w, pad_w, cv2.BORDER_REPLICATE)
image = cv2.resize(image, (width, height))
# Return the pre-processed image
return image
示例8: _crop
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def _crop(self, image, center, size):
corners = np.zeros(4, dtype=int)
corners[:2] = np.floor(center - size / 2).astype(int)
corners[2:] = corners[:2] + size
pads = np.concatenate(
(-corners[:2], corners[2:] - image.shape[1::-1]))
pads = np.maximum(0, pads)
if np.any(pads > 0):
corners = np.concatenate((
corners[:2] + pads[:2],
corners[2:] - pads[2:])).astype(int)
patch = image[corners[1]:corners[3], corners[0]:corners[2]]
if np.any(pads > 0):
patch = cv2.copyMakeBorder(
patch, pads[1], pads[3], pads[0], pads[2],
borderType=cv2.BORDER_REPLICATE)
return patch
示例9: __init__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def __init__(self, p_flip=0.0, max_rotation=0.0, max_shear=0.0, max_scale=0.0, max_ar_factor=0.0,
border_mode='constant', pad_amount=0):
super().__init__()
self.p_flip = p_flip
self.max_rotation = max_rotation
self.max_shear = max_shear
self.max_scale = max_scale
self.max_ar_factor = max_ar_factor
if border_mode == 'constant':
self.border_flag = cv.BORDER_CONSTANT
elif border_mode == 'replicate':
self.border_flag == cv.BORDER_REPLICATE
else:
raise Exception
self.pad_amount = pad_amount
示例10: pad_img_to_fit_bbox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def pad_img_to_fit_bbox(img, x1, x2, y1, y2):
img = cv.copyMakeBorder(img, - min(0, y1), max(y2 - img.shape[0], 0),
-min(0, x1), max(x2 - img.shape[1], 0), cv.BORDER_REPLICATE)
y2 += -min(0, y1)
y1 += -min(0, y1)
x2 += -min(0, x1)
x1 += -min(0, x1)
return img, x1, x2, y1, y2
开发者ID:JACKYLUO1991,项目名称:Face-skin-hair-segmentaiton-and-skin-color-evaluation,代码行数:10,代码来源:pipline_test.py
示例11: _augment
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def _augment(self, img, prm):
size, sigma = prm
return np.reshape(cv2.GaussianBlur(img, size, sigmaX=sigma[0], sigmaY=sigma[1],
borderType=cv2.BORDER_REPLICATE), img.shape)
示例12: affine
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [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)
示例13: random_transform
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def random_transform( image, rotation_range, zoom_range, shift_range, random_flip ):
h,w = image.shape[0:2]
rotation = numpy.random.uniform( -rotation_range, rotation_range )
scale = numpy.random.uniform( 1 - zoom_range, 1 + zoom_range )
tx = numpy.random.uniform( -shift_range, shift_range ) * w
ty = numpy.random.uniform( -shift_range, shift_range ) * h
mat = cv2.getRotationMatrix2D( (w//2,h//2), rotation, scale )
mat[:,2] += (tx,ty)
result = cv2.warpAffine( image, mat, (w,h), borderMode=cv2.BORDER_REPLICATE )
if numpy.random.random() < random_flip:
result = result[:,::-1]
return result
示例14: random_transform
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [as 别名]
def random_transform( image, seed, rotation_range, zoom_range, shift_range, random_flip ):
h,w = image.shape[0:2]
numpy.random.seed( seed )
rotation = numpy.random.uniform( -rotation_range, rotation_range )
scale = numpy.random.uniform( 1 - zoom_range, 1 + zoom_range )
tx = numpy.random.uniform( -shift_range, shift_range ) * w
ty = numpy.random.uniform( -shift_range, shift_range ) * h
mat = cv2.getRotationMatrix2D( (w//2,h//2), rotation, scale )
mat[:,2] += (tx,ty)
result = cv2.warpAffine( image, mat, (w,h), borderMode=cv2.BORDER_REPLICATE )
if numpy.random.random() < random_flip:
result = result[:,::-1]
return result
示例15: affine
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_REPLICATE [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)