當前位置: 首頁>>代碼示例>>Python>>正文


Python cv2.copyMakeBorder方法代碼示例

本文整理匯總了Python中cv2.copyMakeBorder方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.copyMakeBorder方法的具體用法?Python cv2.copyMakeBorder怎麽用?Python cv2.copyMakeBorder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在cv2的用法示例。


在下文中一共展示了cv2.copyMakeBorder方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: step

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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])) 
開發者ID:ManiacalLabs,項目名稱:BiblioPixelAnimations,代碼行數:21,代碼來源:ScreenGrab.py

示例2: copyMakeBorder

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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) 
開發者ID:awslabs,項目名稱:dynamic-training-with-apache-mxnet-on-aws,代碼行數:23,代碼來源:opencv.py

示例3: split

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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 
開發者ID:lRomul,項目名稱:argus-freesound,代碼行數:22,代碼來源:tiles.py

示例4: cut_patch

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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 
開發者ID:lRomul,項目名稱:argus-freesound,代碼行數:19,代碼來源:tiles.py

示例5: __getitem__

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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 
開發者ID:speedinghzl,項目名稱:pytorch-segmentation-toolbox,代碼行數:19,代碼來源:datasets.py

示例6: _resize_image

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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

示例7: __apply_template_matching

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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) 
開發者ID:microsoft,項目名稱:AI-Robot-Challenge-Lab,代碼行數:20,代碼來源:cv_detection_right_hand.py

示例8: load_label

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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 
開發者ID:Andrew-Qibin,項目名稱:DSS,代碼行數:21,代碼來源:sal_data_layer.py

示例9: load_region

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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 
開發者ID:Andrew-Qibin,項目名稱:DSS,代碼行數:21,代碼來源:sal_data_layer.py

示例10: PreprocessImage

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [as 別名]
def PreprocessImage(name, args):
    """Preprocess according to the original author's code."""

    image = cv2.imread(name, 1).astype(np.float32) - args.mean

    if args.resize_dims is not None:
        image = cv2.resize(image, dsize = (args.resize_dims[0], args.resize_dims[1]))

    im_height = image.shape[0]
    im_width = image.shape[1]

    label_margin = 186
    input_image = cv2.copyMakeBorder(image, label_margin, label_margin,
                                     label_margin, label_margin, cv2.BORDER_REFLECT_101)
    input_size = [args.pad_size[1], args.pad_size[0]] # Order is H x W
    margin = [0, input_size[0] - input_image.shape[0],
              0, input_size[1] - input_image.shape[1]]

    input_image = cv2.copyMakeBorder(input_image, margin[0], margin[1], margin[2],
                                     margin[3], cv2.BORDER_REFLECT_101)

    input_image = input_image.transpose([2,0,1]) # To make it C x H x W
    return input_image, im_height, im_width, image 
開發者ID:hmph,項目名稱:adversarial-attacks,代碼行數:25,代碼來源:dilated.py

示例11: label_images

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [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) 
開發者ID:ermongroup,項目名稱:generative_adversary,代碼行數:22,代碼來源:utils.py

示例12: pad_width

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [as 別名]
def pad_width(img,
                  stride,
                  pad_value,
                  min_dims):
        h, w, _ = img.shape
        h = min(min_dims[0], h)
        min_dims[0] = math.ceil(min_dims[0] / float(stride)) * stride
        min_dims[1] = max(min_dims[1], w)
        min_dims[1] = math.ceil(min_dims[1] / float(stride)) * stride
        top = int(math.floor((min_dims[0] - h) / 2.0))
        left = int(math.floor((min_dims[1] - w) / 2.0))
        bottom = int(min_dims[0] - h - top)
        right = int(min_dims[1] - w - left)
        pad = [top, left, bottom, right]
        padded_img = cv2.copyMakeBorder(
            src=img,
            top=top,
            bottom=bottom,
            left=left,
            right=right,
            borderType=cv2.BORDER_CONSTANT,
            value=pad_value)
        return padded_img, pad

# --------------------------------------------------------------------------------------------------------------------- 
開發者ID:osmr,項目名稱:imgclsmob,代碼行數:27,代碼來源:coco_hpe2_dataset.py

示例13: inpaint

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [as 別名]
def inpaint(self, missing_value=0):
        """
        Inpaint missing values in depth image.
        :param missing_value: Value to fill in teh depth image.
        """
        # cv2 inpainting doesn't handle the border properly
        # https://stackoverflow.com/questions/25974033/inpainting-depth-map-still-a-black-image-border
        self.img = cv2.copyMakeBorder(self.img, 1, 1, 1, 1, cv2.BORDER_DEFAULT)
        mask = (self.img == missing_value).astype(np.uint8)

        # Scale to keep as float, but has to be in bounds -1:1 to keep opencv happy.
        scale = np.abs(self.img).max()
        self.img = self.img.astype(np.float32) / scale  # Has to be float32, 64 not supported.
        self.img = cv2.inpaint(self.img, mask, 1, cv2.INPAINT_NS)

        # Back to original size and value range.
        self.img = self.img[1:-1, 1:-1]
        self.img = self.img * scale 
開發者ID:dougsm,項目名稱:ggcnn,代碼行數:20,代碼來源:image.py

示例14: _pad_input_image

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [as 別名]
def _pad_input_image(img, max_steps):
        """pad image to suitable shape"""

        logger.info(msg="_pad_input_image called")
        img_h, img_w, _ = img.shape

        img_pad_h = 0
        if img_h % max_steps > 0:
            img_pad_h = max_steps - img_h % max_steps

        img_pad_w = 0
        if img_w % max_steps > 0:
            img_pad_w = max_steps - img_w % max_steps

        padd_val = np.mean(img, axis=(0, 1)).astype(np.uint8)
        img = cv2.copyMakeBorder(img, 0, img_pad_h, 0, img_pad_w,
                                 cv2.BORDER_CONSTANT, value=padd_val.tolist())
        pad_params = [img_h, img_w, img_pad_h, img_pad_w]

        return img, pad_params 
開發者ID:pymit,項目名稱:Rekognition,代碼行數:22,代碼來源:retina_net.py

示例15: step

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import copyMakeBorder [as 別名]
def step(self, amt=1):
        ret, frame = self._vid.read()

        image = cv2.cvtColor(frame, cv2.COLOR_RGB2BGRA)

        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_CUBIC)
        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]))

        if not isinstance(self.videoSource, int):
            self._frameCount += 1
            if self._frameCount >= self._frameTotal:
                self._vid.set(1, 0)  # CV_CAP_PROP_POS_FRAMES
                self._frameCount = 0
                self.animComplete = True 
開發者ID:ManiacalLabs,項目名稱:BiblioPixelAnimations,代碼行數:30,代碼來源:opencv_video.py


注:本文中的cv2.copyMakeBorder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。