本文整理汇总了Python中cv2.BORDER_CONSTANT属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.BORDER_CONSTANT属性的具体用法?Python cv2.BORDER_CONSTANT怎么用?Python cv2.BORDER_CONSTANT使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.BORDER_CONSTANT属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: step
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def step(self, amt=1):
image = self._capFrame()
if self.crop:
image = image[self._cropY + self.yoff:self._ih - self._cropY +
self.yoff, self._cropX + self.xoff:self._iw - self._cropX + self.xoff]
else:
t, b, l, r = self._pad
image = cv2.copyMakeBorder(
image, t, b, l, r, cv2.BORDER_CONSTANT, value=[0, 0, 0])
resized = cv2.resize(image, (self.width, self.height),
interpolation=cv2.INTER_LINEAR)
if self.mirror:
resized = cv2.flip(resized, 1)
for y in range(self.height):
for x in range(self.width):
self.layout.set(x, y, tuple(resized[y, x][0:3]))
示例2: copyMakeBorder
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def copyMakeBorder(src, top, bot, left, right, border_type=cv2.BORDER_CONSTANT, value=0):
"""Pad image border
Wrapper for cv2.copyMakeBorder that uses mx.nd.NDArray
Parameters
----------
src : NDArray
Image in (width, height, channels).
Others are the same with cv2.copyMakeBorder
Returns
-------
img : NDArray
padded image
"""
hdl = NDArrayHandle()
check_call(_LIB.MXCVcopyMakeBorder(src.handle, ctypes.c_int(top), ctypes.c_int(bot),
ctypes.c_int(left), ctypes.c_int(right),
ctypes.c_int(border_type), ctypes.c_double(value),
ctypes.byref(hdl)))
return mx.nd.NDArray(hdl)
示例3: split
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def split(self, image, border_type=cv2.BORDER_CONSTANT, value=0):
assert image.shape[0] == self.image_height
assert image.shape[1] == self.image_width
orig_shape_len = len(image.shape)
image = cv2.copyMakeBorder(image, self.margin_top, self.margin_bottom, self.margin_left, self.margin_right, borderType=border_type, value=value)
# This check recovers possible lack of last dummy dimension for single-channel images
if len(image.shape) != orig_shape_len:
image = np.expand_dims(image, axis=-1)
tiles = []
for x, y, tile_width, tile_height in self.crops:
tile = image[y:y + tile_height, x:x + tile_width].copy()
assert tile.shape[0] == self.tile_size[0]
assert tile.shape[1] == self.tile_size[1]
tiles.append(tile)
return tiles
示例4: cut_patch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def cut_patch(self, image: np.ndarray, slice_index, border_type=cv2.BORDER_CONSTANT, value=0):
assert image.shape[0] == self.image_height
assert image.shape[1] == self.image_width
orig_shape_len = len(image.shape)
image = cv2.copyMakeBorder(image, self.margin_top, self.margin_bottom, self.margin_left, self.margin_right, borderType=border_type, value=value)
# This check recovers possible lack of last dummy dimension for single-channel images
if len(image.shape) != orig_shape_len:
image = np.expand_dims(image, axis=-1)
x, y, tile_width, tile_height = self.crops[slice_index]
tile = image[y:y + tile_height, x:x + tile_width].copy()
assert tile.shape[0] == self.tile_size[0]
assert tile.shape[1] == self.tile_size[1]
return tile
示例5: __getitem__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def __getitem__(self, index):
datafiles = self.files[index]
image = cv2.imread(datafiles["img"], cv2.IMREAD_COLOR)
size = image.shape
name = osp.splitext(osp.basename(datafiles["img"]))[0]
image = np.asarray(image, np.float32)
image -= self.mean
img_h, img_w, _ = image.shape
pad_h = max(self.crop_h - img_h, 0)
pad_w = max(self.crop_w - img_w, 0)
if pad_h > 0 or pad_w > 0:
image = cv2.copyMakeBorder(image, 0, pad_h, 0,
pad_w, cv2.BORDER_CONSTANT,
value=(0.0, 0.0, 0.0))
image = image.transpose((2, 0, 1))
return image, name, size
示例6: __init__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [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())
示例7: random_crop_pad_to_shape
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def random_crop_pad_to_shape(img, crop_pos, crop_size, pad_label_value):
h, w = img.shape[:2]
start_crop_h, start_crop_w = crop_pos
assert ((start_crop_h < h) and (start_crop_h >= 0))
assert ((start_crop_w < w) and (start_crop_w >= 0))
crop_size = get_2dshape(crop_size)
crop_h, crop_w = crop_size
img_crop = img[start_crop_h:start_crop_h + crop_h,
start_crop_w:start_crop_w + crop_w, ...]
img_, margin = pad_image_to_shape(img_crop, crop_size, cv2.BORDER_CONSTANT,
pad_label_value)
return img_, margin
示例8: _resize_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def _resize_image(img):
dst_width = CFG.ARCH.INPUT_SIZE[0]
dst_height = CFG.ARCH.INPUT_SIZE[1]
h_old, w_old, _ = img.shape
height = dst_height
width = int(w_old * height / h_old)
if width < dst_width:
left_padding = int((dst_width - width)/2)
right_padding = dst_width - width - left_padding
resized_img = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
resized_img = cv2.copyMakeBorder(resized_img, 0, 0, left_padding, right_padding,
cv2.BORDER_CONSTANT, value=[255, 255, 255])
else:
resized_img = cv2.resize(img, (dst_width, height), interpolation=cv2.INTER_CUBIC)
return resized_img
开发者ID:Mingtzge,项目名称:2019-CCF-BDCI-OCR-MCZJ-OCR-IdentificationIDElement,代码行数:18,代码来源:write_tfrecord.py
示例9: __apply_template_matching
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def __apply_template_matching(angle, template, image):
# Rotate the template
template_rotated = __rotate_image_size_corrected(template, angle)
# Apply template matching
image_templated = cv2.matchTemplate(image, template_rotated, cv2.TM_CCOEFF_NORMED)
# Correct template matching image size difference
template_rotated_height, template_rotated_width = template_rotated.shape
template_half_height = template_rotated_height // 2
template_half_width = template_rotated_width // 2
image_templated_inrange_size_corrected = cv2.copyMakeBorder(image_templated, template_half_height, template_half_height, template_half_width, template_half_width, cv2.BORDER_CONSTANT, value=0)
# Calculate maximum match coefficient
max_match = numpy.max(image_templated_inrange_size_corrected)
return (max_match, angle, template_rotated, image_templated_inrange_size_corrected)
示例10: __init__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def __init__(
self,
min_height=769,
min_width=769,
border_mode=cv2.BORDER_CONSTANT,
value=0,
ignore_index=255,
always_apply=False,
p=1.0,
):
super().__init__(always_apply, p)
self.min_height = min_height
self.min_width = min_width
self.border_mode = border_mode
self.value = value
self.ignore_index = ignore_index
示例11: load_label
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def load_label(self, idx):
"""
Load label image as 1 x height x width integer array of label indices.
The leading singleton dimension is required by the loss.
"""
im = Image.open(self.data_root + self.label_lst[idx])
label = np.array(im) / 255#cv2.imread(self.data_root + self.label_lst[idx], 0) / 255
#if self.scales != None:
# label = cv2.resize(label, None, None, fx=self.scales[self.scale_ind], fy=self.scales[self.scale_ind], \
# interpolation=cv2.INTER_NEAREST)
#height, width = label.shape[:2]
#h_off = self.crop_size - height
#w_off = self.crop_size - width
#label = cv2.copyMakeBorder(label, 0, max(0, h_off), 0, max(0, w_off), cv2.BORDER_CONSTANT, value=[-1,])
#label = label[self.h_off:self.h_off+self.height, self.w_off:self.w_off+self.width]
label = label[np.newaxis, ...]
if self.flip == 1:
label = label[:,:,::-1]
return label
示例12: load_region
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def load_region(self, idx):
"""
Load label image as 1 x height x width integer array of label indices.
The leading singleton dimension is required by the loss.
"""
im = Image.open(self.data_root + self.region_lst[idx])
region = np.array(im, dtype=np.float32) / 15.0
#print np.unique(region)
#if self.scales != None:
# label = cv2.resize(label, None, None, fx=self.scales[self.scale_ind], fy=self.scales[self.scale_ind], \
# interpolation=cv2.INTER_NEAREST)
#height, width = label.shape[:2]
#h_off = self.crop_size - height
#w_off = self.crop_size - width
#label = cv2.copyMakeBorder(label, 0, max(0, h_off), 0, max(0, w_off), cv2.BORDER_CONSTANT, value=[-1,])
region = region[np.newaxis, ...]
if self.flip == 1:
region = region[:,:,::-1]
return region
示例13: label_images
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def label_images(images, labels):
font = cv.FONT_HERSHEY_SIMPLEX
new_imgs = []
for i, img in enumerate(images):
new_img = ((img.copy() + 1.) * 127.5).astype(np.uint8)
if new_img.shape[-1] == 3:
new_img = new_img[..., ::-1]
new_img = cv.resize(new_img, (100, 100), interpolation=cv.INTER_LINEAR)
new_img = cv.putText(new_img, str(labels[i]), (10, 30), font, 1, (255, 255, 255), 2, cv.LINE_AA)
new_img = cv.copyMakeBorder(new_img, top=2, bottom=2, left=2, right=2, borderType=cv.BORDER_CONSTANT,
value=(255, 255, 255))
else:
new_img = np.squeeze(new_img)
new_img = cv.resize(new_img, (100, 100), interpolation=cv.INTER_LINEAR)
new_img = cv.putText(new_img, str(labels[i]), (10, 30), font, 1, (255), 2, cv.LINE_AA)
new_img = new_img[..., None]
new_img = (new_img / 127.5 - 1.0).astype(np.float32)
new_imgs.append(new_img[..., ::-1])
return np.stack(new_imgs, axis=0)
示例14: rotation
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def rotation(img, degrees, interpolation=cv2.INTER_LINEAR, value=0):
if isinstance(degrees, list):
if len(degrees) == 2:
degree = random.uniform(degrees[0], degrees[1])
else:
degree = random.choice(degrees)
else:
degree = degrees
h, w = img.shape[0:2]
center = (w / 2, h / 2)
map_matrix = cv2.getRotationMatrix2D(center, degree, 1.0)
img = cv2.warpAffine(
img,
map_matrix, (w, h),
flags=interpolation,
borderMode=cv2.BORDER_CONSTANT,
borderValue=value)
return img
示例15: filter2D
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BORDER_CONSTANT [as 别名]
def filter2D(input_arr, filter):
"""
2D filtering (i.e. convolution but without mirroring the filter). Mostly a convenience wrapper
around OpenCV.
Parameters
----------
input_arr : numpy array, HxW size
filter : numpy array, H1xW1 size
Returns
-------
result : numpy array, HxW size
"""
return cv2.filter2D(input_arr,
-1,
filter,
borderType=cv2.BORDER_CONSTANT)