Python cv2 模块,INTER_NEAREST 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.INTER_NEAREST

项目:DoNotSnap    作者:AVGInnovationLabs    | 项目源码 | 文件源码
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
项目:card-scanner    作者:RFVenter    | 项目源码 | 文件源码
def filter_image(image, canny1=5, canny2=5, show=False):
    # compute the ratio of the old height to the new height, and resize it
    image = imutils.resize(image, height=scale_factor, interpolation=cv2.INTER_NEAREST)

    # convert the image to grayscale, blur it, and find edges in the image
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(blurred, canny1, canny2)

    # show the image(s)
    if show:
        cv2.imshow("Edged", edged)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    return edged
项目:card-scanner    作者:RFVenter    | 项目源码 | 文件源码
def filter_image(image, canny1=10, canny2=10, show=False):
    # compute the ratio of the old height to the new height, and resize it
    image = imutils.resize(image, height=scale_factor, interpolation=cv2.INTER_NEAREST)

    # convert the image to grayscale, blur it, and find edges in the image
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, canny1, canny2)

    # show the image(s)
    if show:
        cv2.imshow("Edged", edged)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    return edged
项目:deep-prior    作者:moberweger    | 项目源码 | 文件源码
def resizeCrop(self, crop, sz):
        """
        Resize cropped image
        :param crop: crop
        :param sz: size
        :return: resized image
        """
        if self.resizeMethod == self.RESIZE_CV2_NN:
            rz = cv2.resize(crop, sz, interpolation=cv2.INTER_NEAREST)
        elif self.resizeMethod == self.RESIZE_BILINEAR:
            rz = self.bilinearResize(crop, sz, self.getNDValue())
        elif self.resizeMethod == self.RESIZE_CV2_LINEAR:
            rz = cv2.resize(crop, sz, interpolation=cv2.INTER_LINEAR)
        else:
            raise NotImplementedError("Unknown resize method!")
        return rz
项目:traffic_detection_yolo2    作者:wAuner    | 项目源码 | 文件源码
def create_heatmaps(img, pred):
    """
    Uses objectness probability to draw a heatmap on the image and returns it
    """
    # find anchors with highest prediction
    best_pred = np.max(pred[..., 0], axis=-1)
    # convert probabilities to colormap scale
    best_pred = np.uint8(best_pred * 255)
    # apply color map
    # cv2 colormaps create BGR, not RGB
    cmap = cv2.cvtColor(cv2.applyColorMap(best_pred, cv2.COLORMAP_JET), cv2.COLOR_BGR2RGB)
    # resize the color map to fit image
    cmap = cv2.resize(cmap, img.shape[1::-1], interpolation=cv2.INTER_NEAREST)

    # overlay cmap with image
    return cv2.addWeighted(cmap, 1, img, 0.5, 0)
项目:blitznet    作者:dvornikita    | 项目源码 | 文件源码
def draw_seg(self, img, seg_gt, segmentation, name):
        """Applies generated segmentation mask to an image"""
        palette = np.load('Extra/palette.npy').tolist()
        img_size = (img.shape[1], img.shape[0])

        segmentation = cv2.resize(segmentation, dsize=img_size,
                                  interpolation=cv2.INTER_NEAREST)

        image = Image.fromarray((img * 255).astype('uint8'))
        segmentation_draw = Image.fromarray((segmentation).astype('uint8'), 'P')
        segmentation_draw.putpalette(palette)
        segmentation_draw.save(self.directory + '/%s_segmentation.png' % name, 'PNG')
        image.save(self.directory + '/%s.jpg' % name, 'JPEG')

        if seg_gt:
            seg_gt_draw = Image.fromarray((seg_gt).astype('uint8'), 'P')
            seg_gt_draw.putpalette(palette)
            seg_gt_draw.save(self.directory + '/%s_seg_gt.png' % name, 'PNG')
项目:deer    作者:VinF    | 项目源码 | 文件源码
def reset(self, mode):
        if mode == MyEnv.VALIDATION_MODE:
            if self._mode != MyEnv.VALIDATION_MODE:
                self._mode = MyEnv.VALIDATION_MODE
                self._mode_score = 0.0
                self._mode_episode_count = 0
            else:
                self._mode_episode_count += 1
        elif self._mode != -1: # and thus mode == -1
            self._mode = -1

        self._ple.reset_game()
        for _ in range(self._random_state.randint(15)):
            self._ple.act(self._ple.NOOP)
        self._screen = self._ple.getScreenGrayscale()
        cv2.resize(self._screen, (48, 48), self._reduced_screen, interpolation=cv2.INTER_NEAREST)

        return [2 * [48 * [48 * [0]]]]
项目:deer    作者:VinF    | 项目源码 | 文件源码
def reset(self, mode):
        if mode == MyEnv.VALIDATION_MODE:
            if self._mode != MyEnv.VALIDATION_MODE:
                self._mode = MyEnv.VALIDATION_MODE
                self._mode_score = 0.0
                self._mode_episode_count = 0
            else:
                self._mode_episode_count += 1
        elif self._mode != -1: # and thus mode == -1
            self._mode = -1

        self._ale.reset_game()
        for _ in range(self._random_state.randint(15)):
            self._ale.act(0)
        self._ale.getScreenGrayscale(self._screen)
        cv2.resize(self._screen, (84, 84), self._reduced_screen, interpolation=cv2.INTER_NEAREST)

        return [4 * [84 * [84 * [0]]]]
项目:ms-pacman    作者:anassinator    | 项目源码 | 文件源码
def to_image(self):
        """Converts map to a viewable image.

        Returns:
            OpenCV image.
        """
        height, width = self._map.shape
        image = np.zeros((height, width, 3), dtype=np.uint8)
        for i in range(width):
            for j in range(height):
                classification = self._map[j, i]
                image[j, i] = GameMapObjects.to_color(classification)

        upscaled_image = cv2.resize(image, (100, 100),
                                    interpolation=cv2.INTER_NEAREST)
        return upscaled_image
项目:semi-auto-anno    作者:moberweger    | 项目源码 | 文件源码
def resizeCrop(self, crop, sz):
        """
        Resize cropped image
        :param crop: crop
        :param sz: size
        :return: resized image
        """
        if self.resizeMethod == self.RESIZE_CV2_NN:
            rz = cv2.resize(crop, sz, interpolation=cv2.INTER_NEAREST)
        elif self.resizeMethod == self.RESIZE_BILINEAR:
            rz = self.bilinearResize(crop, sz, self.getNDValue())
        elif self.resizeMethod == self.RESIZE_CV2_LINEAR:
            rz = cv2.resize(crop, sz, interpolation=cv2.INTER_LINEAR)
        else:
            raise NotImplementedError("Unknown resize method!")
        return rz
项目:caffe-tools    作者:davidstutz    | 项目源码 | 文件源码
def visualize_weights(net, layer, zoom = 2):
    """
    Visualize weights in a fully conencted layer.

    :param net: caffe network
    :type net: caffe.Net
    :param layer: layer name
    :type layer: string
    :param zoom: the number of pixels (in width and height) per weight
    :type zoom: int
    :return: image visualizing the kernels in a grid
    :rtype: numpy.ndarray
    """

    assert layer in get_layers(net), "layer %s not found" % layer

    weights = net.params[layer][0].data
    weights = (weights - numpy.min(weights))/(numpy.max(weights) - numpy.min(weights))
    return cv2.resize(weights, (weights.shape[0]*zoom, weights.shape[1]*zoom), weights, 0, 0, cv2.INTER_NEAREST)
项目:HandwritingRecognition    作者:eng-tsmith    | 项目源码 | 文件源码
def squeeze(image, width_max, border):
    """
    This function squeezes images in the width.
    :param image: Numpy array of image
    :param width_max: max image width
    :param border: border left and right of squeezed image
    :return: Squeezed Image
    """
    image_wd = image.shape[1]
    image_ht = image.shape[0]
    basewidth = width_max - border

    wpercent = basewidth / image_wd
    dim = (int(wpercent*image_wd), image_ht)
    img_squeeze = cv.resize(image, dim, interpolation=cv.INTER_NEAREST)
    return img_squeeze
项目:deep-prior-pp    作者:moberweger    | 项目源码 | 文件源码
def resizeCrop(self, crop, sz):
        """
        Resize cropped image
        :param crop: crop
        :param sz: size
        :return: resized image
        """
        if self.resizeMethod == self.RESIZE_CV2_NN:
            rz = cv2.resize(crop, sz, interpolation=cv2.INTER_NEAREST)
        elif self.resizeMethod == self.RESIZE_BILINEAR:
            rz = self.bilinearResize(crop, sz, self.getNDValue())
        elif self.resizeMethod == self.RESIZE_CV2_LINEAR:
            rz = cv2.resize(crop, sz, interpolation=cv2.INTER_LINEAR)
        else:
            raise NotImplementedError("Unknown resize method!")
        return rz
项目:car-detection    作者:mmetcalfe    | 项目源码 | 文件源码
def resize_sample(sample, shape=None, use_interp=True, scale=None):
    if (shape and scale) or (not shape and not scale):
        raise ValueError('Must specify exactly one of shape or scale, but got shape=\'{}\', scale=\'{}\''.format(shape, scale))

    # Use INTER_AREA for shrinking and INTER_LINEAR for enlarging:
    interp = cv2.INTER_NEAREST
    if use_interp:
        target_is_smaller = (shape and shape[1] < sample.shape[1]) or (scale and scale < 1) # targetWidth < sampleWidth
        interp = cv2.INTER_AREA if target_is_smaller else cv2.INTER_LINEAR

    if shape:
        resized = cv2.resize(sample, (shape[1], shape[0]), interpolation=interp)
    else:
        resized = cv2.resize(sample, None, fx=scale, fy=scale, interpolation=interp)

    return resized
项目:chainercv    作者:chainer    | 项目源码 | 文件源码
def _resize(img, size, interpolation):
        img = img.transpose((1, 2, 0))
        if interpolation == PIL.Image.NEAREST:
            cv_interpolation = cv2.INTER_NEAREST
        elif interpolation == PIL.Image.BILINEAR:
            cv_interpolation = cv2.INTER_LINEAR
        elif interpolation == PIL.Image.BICUBIC:
            cv_interpolation = cv2.INTER_CUBIC
        elif interpolation == PIL.Image.LANCZOS:
            cv_interpolation = cv2.INTER_LANCZOS4
        H, W = size
        img = cv2.resize(img, dsize=(W, H), interpolation=cv_interpolation)

        # If input is a grayscale image, cv2 returns a two-dimentional array.
        if len(img.shape) == 2:
            img = img[:, :, np.newaxis]
        return img.transpose((2, 0, 1))
项目:pynephoscope    作者:neXyon    | 项目源码 | 文件源码
def renderStarGauss(image, cov, mu, first, scale = 5):
    num_circles = 3
    num_points = 64

    cov = sqrtm(cov)

    num = num_circles * num_points
    pos = np.ones((num, 2))

    for c in range(num_circles):
        r = c + 1
        for p in range(num_points):
            angle = p / num_points * 2 * np.pi
            index = c * num_points + p

            x = r * np.cos(angle)
            y = r * np.sin(angle)

            pos[index, 0] = x * cov[0, 0] + y * cov[0, 1] + mu[0]
            pos[index, 1] = x * cov[1, 0] + y * cov[1, 1] + mu[1]

    #image = image.copy()
    #image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

    if first:
        image = cv2.resize(image, (0, 0), None, scale, scale, cv2.INTER_NEAREST)

    for c in range(num_circles):
        pts = np.array(pos[c * num_points:(c + 1) * num_points, :] * scale + scale / 2, np.int32)
        pts = pts.reshape((-1,1,2))
        cv2.polylines(image, [pts], True, (255, 0, 0))

    return image
项目:deep-learning-for-human-part-discovery-in-images    作者:shiba24    | 项目源码 | 文件源码
def make_mask(self, matfile):
        d = sio.loadmat(matfile)
        if "image" in matfile:
            parts_mask = np.transpose(np.expand_dims(d["M"], 0), (1, 2, 0))
        else:
            objects = d["anno"][0, 0][1]
            object_name = [objects[0, i][0][0] for i in range(objects.shape[1])]
            img_shape = objects[0, 0][2].shape
            parts_mask = np.zeros(img_shape + (1, ))
            for index, obj in enumerate(object_name):
                if obj == "person":
                    if not objects[0, index][3].shape == (0, 0):
                        for j in range(objects[0, index][3].shape[1]):
                            parts_mask[:, :, 0] = np.where(parts_mask[:, :, 0] == 0, merged_parts_list[objects[0, index][3][0, j][0][0]] * np.array(objects[0, index][3][0, j][1]), parts_mask[:, :, 0])
        parts_mask = cv2.resize(parts_mask.astype(np.uint8), (self.insize, self.insize), interpolation = cv2.INTER_NEAREST)
        # parts_mask = (parts_mask > 0).astype(np.uint8)
        return parts_mask
项目:pyku    作者:dubvulture    | 项目源码 | 文件源码
def morph(roi):
    ratio = min(28. / np.size(roi, 0), 28. / np.size(roi, 1))
    roi = cv2.resize(roi, None, fx=ratio, fy=ratio,
                     interpolation=cv2.INTER_NEAREST)
    dx = 28 - np.size(roi, 1)
    dy = 28 - np.size(roi, 0)
    px = ((int(dx / 2.)), int(np.ceil(dx / 2.)))
    py = ((int(dy / 2.)), int(np.ceil(dy / 2.)))
    squared = np.pad(roi, (py, px), 'constant', constant_values=0)
    return squared
项目:pybot    作者:spillai    | 项目源码 | 文件源码
def rectify(self, l, r): 
        """
        Rectify frames passed as (left, right) 
        Remapping is done with nearest neighbor for speed.
        """
        return [cv2.remap(l, self.undistortion_map[cidx], self.rectification_map[cidx], cv2.INTER_NEAREST)
                for cidx in range(len(self.cams))]
项目:lsun-room    作者:leVirve    | 项目源码 | 文件源码
def load(self, name):
        image_path = os.path.join(self.dataset.image, '%s.jpg' % name)
        label_path = os.path.join(self.dataset.layout_image, '%s.png' % name)

        img = cv2.imread(image_path)
        lbl = cv2.imread(label_path, 0)

        img = cv2.resize(img, self.target_size, cv2.INTER_LINEAR)
        lbl = cv2.resize(lbl, self.target_size, cv2.INTER_NEAREST)

        img = self.transform(img)
        lbl = np.clip(lbl, 1, 5) - 1
        lbl = torch.from_numpy(np.expand_dims(lbl, axis=0)).long()

        return img, lbl
项目:TC-Lung_nodules_detection    作者:Shicoder    | 项目源码 | 文件源码
def rescale_patient_images2(images_zyx, target_shape, verbose=False):
    if verbose:
        print("Target: ", target_shape)
        print("Shape: ", images_zyx.shape)

    # print "Resizing dim z"
    resize_x = 1.0
    interpolation = cv2.INTER_NEAREST if False else cv2.INTER_LINEAR
    res = cv2.resize(images_zyx, dsize=(target_shape[1], target_shape[0]), interpolation=interpolation)  # opencv assumes y, x, channels umpy array, so y = z pfff
    # print "Shape is now : ", res.shape

    res = res.swapaxes(0, 2)
    res = res.swapaxes(0, 1)

    # cv2 can handle max 512 channels..
    if res.shape[2] > 512:
        res = res.swapaxes(0, 2)
        res1 = res[:256]
        res2 = res[256:]
        res1 = res1.swapaxes(0, 2)
        res2 = res2.swapaxes(0, 2)
        res1 = cv2.resize(res1, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation)
        res2 = cv2.resize(res2, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation)
        res1 = res1.swapaxes(0, 2)
        res2 = res2.swapaxes(0, 2)
        res = numpy.vstack([res1, res2])
        res = res.swapaxes(0, 2)
    else:
        res = cv2.resize(res, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation)

    res = res.swapaxes(0, 2)
    res = res.swapaxes(2, 1)
    if verbose:
        print("Shape after: ", res.shape)
    return res
项目:simple-pix2pix-pytorch    作者:Eiji-Kb    | 项目源码 | 文件源码
def __init__(self, dataDir='./datasets/facade/base', data_start = 1, data_end = 300):

        self.dataset = []
        self.shufflelist = []

        for i in range (data_start, data_end + 1):
            real_image = cv2.imread(dataDir+"/cmp_b%04d.jpg"%i)
            real_image = self.resize(real_image, ipl_alg = cv2.INTER_CUBIC)

            input_x_image = cv2.imread(dataDir+"/cmp_b%04d.png"%i)
            input_x_image = self.resize(input_x_image, ipl_alg = cv2.INTER_NEAREST)

            self.dataset.append((input_x_image, real_image))

            self.shufflelist = list(range(self.len()))
项目:deer    作者:VinF    | 项目源码 | 文件源码
def act(self, action):
        action = self._actions[action]

        reward = 0
        for _ in range(self._frame_skip):
            reward += self._ple.act(action)
            if self.inTerminalState():
                break

        self._screen = self._ple.getScreenGrayscale()
        cv2.resize(self._screen, (48, 48), self._reduced_screen, interpolation=cv2.INTER_NEAREST)

        self._mode_score += reward
        return np.sign(reward)
项目:deer    作者:VinF    | 项目源码 | 文件源码
def act(self, action):
        action = self._actions[action]

        reward = 0
        for _ in range(self._frame_skip):
            reward += self._ale.act(action)
            if self.inTerminalState():
                break

        self._ale.getScreenGrayscale(self._screen)
        cv2.resize(self._screen, (84, 84), self._reduced_screen, interpolation=cv2.INTER_NEAREST)

        self._mode_score += reward
        return np.sign(reward)
项目:DocumentSegmentation    作者:SeguinBe    | 项目源码 | 文件源码
def save_and_resize(img, filename, nearest=False):
    resized = cv2.resize(img, (TARGET_WIDTH, (img.shape[0]*TARGET_WIDTH)//img.shape[1]),
                         interpolation=cv2.INTER_NEAREST if nearest else None)
    imsave(filename, resized)
项目:DocumentSegmentation    作者:SeguinBe    | 项目源码 | 文件源码
def save_and_resize(img, filename, nearest=False):
    resized = cv2.resize(img, (TARGET_WIDTH, (img.shape[0]*TARGET_WIDTH)//img.shape[1]),
                         interpolation=cv2.INTER_NEAREST if nearest else cv2.INTER_LINEAR)
    imsave(filename, resized)
项目:ms-pacman    作者:anassinator    | 项目源码 | 文件源码
def to_image(self):
        """Converts map to a viewable image.

        Returns:
            OpenCV image.
        """
        image = np.zeros((self.HEIGHT, self.WIDTH, 3), dtype=np.uint8)
        for i in range(self.WIDTH):
            for j in range(self.HEIGHT):
                classification = self._map[j, i]
                image[j, i] = GameMapObjects.to_color(classification)

        upscaled_image = cv2.resize(image, (160, 168),
                                    interpolation=cv2.INTER_NEAREST)
        return upscaled_image
项目:focal-loss    作者:unsky    | 项目源码 | 文件源码
def get_segmentation_image(segdb, config):
    """
    propocess image and return segdb
    :param segdb: a list of segdb
    :return: list of img as mxnet format
    """
    num_images = len(segdb)
    assert num_images > 0, 'No images'
    processed_ims = []
    processed_segdb = []
    processed_seg_cls_gt = []
    for i in range(num_images):
        seg_rec = segdb[i]
        assert os.path.exists(seg_rec['image']), '%s does not exist'.format(seg_rec['image'])
        im = np.array(cv2.imread(seg_rec['image']))

        new_rec = seg_rec.copy()

        scale_ind = random.randrange(len(config.SCALES))
        target_size = config.SCALES[scale_ind][0]
        max_size = config.SCALES[scale_ind][1]
        im, im_scale = resize(im, target_size, max_size, stride=config.network.IMAGE_STRIDE)
        im_tensor = transform(im, config.network.PIXEL_MEANS)
        im_info = [im_tensor.shape[2], im_tensor.shape[3], im_scale]
        new_rec['im_info'] = im_info

        seg_cls_gt = np.array(Image.open(seg_rec['seg_cls_path']))
        seg_cls_gt, seg_cls_gt_scale = resize(
            seg_cls_gt, target_size, max_size, stride=config.network.IMAGE_STRIDE, interpolation=cv2.INTER_NEAREST)
        seg_cls_gt_tensor = transform_seg_gt(seg_cls_gt)

        processed_ims.append(im_tensor)
        processed_segdb.append(new_rec)
        processed_seg_cls_gt.append(seg_cls_gt_tensor)

    return processed_ims, processed_seg_cls_gt, processed_segdb
项目:focal-loss    作者:unsky    | 项目源码 | 文件源码
def _py_evaluate_segmentation(self):
        """
        This function is a wrapper to calculte the metrics for given pred_segmentation results
        :param pred_segmentations: the pred segmentation result
        :return: the evaluation metrics
        """
        confusion_matrix = np.zeros((self.num_classes,self.num_classes))
        result_dir = os.path.join(self.result_path, 'results', 'VOC' + self.year, 'Segmentation')

        for i, index in enumerate(self.image_set_index):
            seg_gt_info = self.load_pascal_segmentation_annotation(index)
            seg_gt_path = seg_gt_info['seg_cls_path']
            seg_gt = np.array(PIL.Image.open(seg_gt_path)).astype('float32')
            seg_pred_path = os.path.join(result_dir, '%s.png'%(index))
            seg_pred = np.array(PIL.Image.open(seg_pred_path)).astype('float32')

            seg_gt = cv2.resize(seg_gt, (seg_pred.shape[1], seg_pred.shape[0]), interpolation=cv2.INTER_NEAREST)
            ignore_index = seg_gt != 255
            seg_gt = seg_gt[ignore_index]
            seg_pred = seg_pred[ignore_index]

            confusion_matrix += self.get_confusion_matrix(seg_gt, seg_pred, self.num_classes)

        pos = confusion_matrix.sum(1)
        res = confusion_matrix.sum(0)
        tp = np.diag(confusion_matrix)

        IU_array = (tp / np.maximum(1.0, pos + res - tp))
        mean_IU = IU_array.mean()

        return {'meanIU':mean_IU, 'IU_array':IU_array}
项目:chainer-fcis    作者:knorth55    | 项目源码 | 文件源码
def __call__(self, in_data):
        orig_img, bboxes, whole_mask, labels = in_data
        _, orig_H, orig_W = orig_img.shape
        img = self.model.prepare(
            orig_img, self.target_height, self.max_width)
        img = img.astype(np.float32)
        del orig_img
        _, H, W = img.shape
        scale = H / orig_H

        bboxes = chainercv.transforms.resize_bbox(
            bboxes, (orig_H, orig_W), (H, W))

        indices = get_keep_indices(bboxes)
        bboxes = bboxes[indices, :]
        whole_mask = whole_mask[indices, :, :]
        labels = labels[indices]

        whole_mask = whole_mask.transpose((1, 2, 0))
        whole_mask = cv2.resize(
            whole_mask.astype(np.uint8), (W, H),
            interpolation=cv2.INTER_NEAREST)
        if whole_mask.ndim < 3:
            whole_mask = whole_mask.reshape((H, W, 1))
        whole_mask = whole_mask.transpose((2, 0, 1))

        if self.flip:
            img, params = chainercv.transforms.random_flip(
                img, x_random=True, return_param=True)
            whole_mask = fcis.utils.flip_mask(
                whole_mask, x_flip=params['x_flip'])
            bboxes = chainercv.transforms.flip_bbox(
                bboxes, (H, W), x_flip=params['x_flip'])

        return img, bboxes, whole_mask, labels, scale
项目:chainer-fcis    作者:knorth55    | 项目源码 | 文件源码
def __call__(self, in_data):
        orig_img, bboxes, whole_mask, labels = in_data
        _, orig_H, orig_W = orig_img.shape
        img = self.model.prepare(
            orig_img, self.target_height, self.max_width)
        del orig_img
        _, H, W = img.shape
        scale = H / orig_H

        bboxes = chainercv.transforms.resize_bbox(
            bboxes, (orig_H, orig_W), (H, W))

        indices = get_keep_indices(bboxes)
        bboxes = bboxes[indices, :]
        whole_mask = whole_mask[indices, :, :]
        labels = labels[indices]

        whole_mask = whole_mask.transpose((1, 2, 0))
        whole_mask = cv2.resize(
            whole_mask.astype(np.uint8), (W, H),
            interpolation=cv2.INTER_NEAREST)
        if whole_mask.ndim < 3:
            whole_mask = whole_mask.reshape((H, W, 1))
        whole_mask = whole_mask.transpose((2, 0, 1))

        if self.flip:
            img, params = chainercv.transforms.random_flip(
                img, x_random=True, return_param=True)
            whole_mask = fcis.utils.flip_mask(
                whole_mask, x_flip=params['x_flip'])
            bboxes = chainercv.transforms.flip_bbox(
                bboxes, (H, W), x_flip=params['x_flip'])

        return img, bboxes, whole_mask, labels, scale
项目:trainer    作者:nutszebra    | 项目源码 | 文件源码
def cv2_interpolation(num):
        """Each number corresponds to the way of interpolation

        Note:
           | INTER_NEAREST: 0 - a nearest-neighbor interpolation
           | INTER_LINEAR: 1 - a bilinear interpolation (used by default)
           | INTER_CUBIC: 2 - a bicubic interpolation over 4x4 pixel neighborhood
           | INTER_AREA 3 - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
           | INTER_LANCZOS4: 4 - a Lanczos interpolation over 8x8 pixel neighborhood

        Example:

        ::

            num = cv2.INTER_NEAREST
            >>> print(num)
            0

            self.cv2_interpolation(cv2.INTER_NEAREST)

        Args:
            num (int): int

        Returns:
            str: the name of interpolation
        """
        if num == 0:
            return 'INTER_NEAREST'
        if num == 1:
            return 'INTER_LINEAR'
        if num == 2:
            return 'INTER_CUBIC'
        if num == 3:
            return 'INTER_AREA'
        if num == 4:
            return 'INTER_LANCZOS4'
        return str(num)
项目:Deformable-ConvNets    作者:msracver    | 项目源码 | 文件源码
def get_segmentation_image(segdb, config):
    """
    propocess image and return segdb
    :param segdb: a list of segdb
    :return: list of img as mxnet format
    """
    num_images = len(segdb)
    assert num_images > 0, 'No images'
    processed_ims = []
    processed_segdb = []
    processed_seg_cls_gt = []
    for i in range(num_images):
        seg_rec = segdb[i]
        assert os.path.exists(seg_rec['image']), '%s does not exist'.format(seg_rec['image'])
        im = np.array(cv2.imread(seg_rec['image']))

        new_rec = seg_rec.copy()

        scale_ind = random.randrange(len(config.SCALES))
        target_size = config.SCALES[scale_ind][0]
        max_size = config.SCALES[scale_ind][1]
        im, im_scale = resize(im, target_size, max_size, stride=config.network.IMAGE_STRIDE)
        im_tensor = transform(im, config.network.PIXEL_MEANS)
        im_info = [im_tensor.shape[2], im_tensor.shape[3], im_scale]
        new_rec['im_info'] = im_info

        seg_cls_gt = np.array(Image.open(seg_rec['seg_cls_path']))
        seg_cls_gt, seg_cls_gt_scale = resize(
            seg_cls_gt, target_size, max_size, stride=config.network.IMAGE_STRIDE, interpolation=cv2.INTER_NEAREST)
        seg_cls_gt_tensor = transform_seg_gt(seg_cls_gt)

        processed_ims.append(im_tensor)
        processed_segdb.append(new_rec)
        processed_seg_cls_gt.append(seg_cls_gt_tensor)

    return processed_ims, processed_seg_cls_gt, processed_segdb
项目:Deformable-ConvNets    作者:msracver    | 项目源码 | 文件源码
def _py_evaluate_segmentation(self):
        """
        This function is a wrapper to calculte the metrics for given pred_segmentation results
        :param pred_segmentations: the pred segmentation result
        :return: the evaluation metrics
        """
        confusion_matrix = np.zeros((self.num_classes,self.num_classes))
        result_dir = os.path.join(self.result_path, 'results', 'VOC' + self.year, 'Segmentation')

        for i, index in enumerate(self.image_set_index):
            seg_gt_info = self.load_pascal_segmentation_annotation(index)
            seg_gt_path = seg_gt_info['seg_cls_path']
            seg_gt = np.array(PIL.Image.open(seg_gt_path)).astype('float32')
            seg_pred_path = os.path.join(result_dir, '%s.png'%(index))
            seg_pred = np.array(PIL.Image.open(seg_pred_path)).astype('float32')

            seg_gt = cv2.resize(seg_gt, (seg_pred.shape[1], seg_pred.shape[0]), interpolation=cv2.INTER_NEAREST)
            ignore_index = seg_gt != 255
            seg_gt = seg_gt[ignore_index]
            seg_pred = seg_pred[ignore_index]

            confusion_matrix += self.get_confusion_matrix(seg_gt, seg_pred, self.num_classes)

        pos = confusion_matrix.sum(1)
        res = confusion_matrix.sum(0)
        tp = np.diag(confusion_matrix)

        IU_array = (tp / np.maximum(1.0, pos + res - tp))
        mean_IU = IU_array.mean()

        return {'meanIU':mean_IU, 'IU_array':IU_array}
项目:Deformable-ConvNets    作者:msracver    | 项目源码 | 文件源码
def _py_evaluate_segmentation(self):
        """
        This function is a wrapper to calculte the metrics for given pred_segmentation results
        :return: the evaluation metrics
        """
        res_file_folder = os.path.join(self.result_path, 'results')

        confusion_matrix = np.zeros((self.num_classes,self.num_classes))
        for i, index in enumerate(self.image_set_index):
            seg_gt_info = self.load_segdb_from_index(index)

            seg_gt = np.array(Image.open(seg_gt_info['seg_cls_path'])).astype('float32')

            seg_pathes = os.path.split(seg_gt_info['seg_cls_path'])
            res_image_name = seg_pathes[1][:-len('_gtFine_labelTrainIds.png')]
            res_subfolder_name = os.path.split(seg_pathes[0])[-1]
            res_save_folder = os.path.join(res_file_folder, res_subfolder_name)
            res_save_path = os.path.join(res_save_folder, res_image_name + '.png')

            seg_pred = np.array(Image.open(res_save_path)).astype('float32')
            #seg_pred = np.squeeze(pred_segmentations[i])

            seg_pred = cv2.resize(seg_pred, (seg_gt.shape[1], seg_gt.shape[0]), interpolation=cv2.INTER_NEAREST)
            ignore_index = seg_gt != 255
            seg_gt = seg_gt[ignore_index]
            seg_pred = seg_pred[ignore_index]

            confusion_matrix += self.get_confusion_matrix(seg_gt, seg_pred, self.num_classes)

        pos = confusion_matrix.sum(1)
        res = confusion_matrix.sum(0)
        tp = np.diag(confusion_matrix)

        IU_array = (tp / np.maximum(1.0, pos + res - tp))
        mean_IU = IU_array.mean()

        return {'meanIU':mean_IU, 'IU_array':IU_array}
项目:kaggle-carvana    作者:ematvey    | 项目源码 | 文件源码
def create_checkpoint_mask(img, mask, predicted_mask):
    p_mask = predicted_mask
    assert p_mask.shape[0] < p_mask.shape[1]
    if p_mask.shape == (CARVANA_H, CARVANA_W + 2):
        p_mask = p_mask[:, 1:-1]
    else:
        p_mask = cv2.resize(p_mask, (CARVANA_W, CARVANA_H),
                            interpolation=cv2.INTER_NEAREST)
    p_mask = (p_mask > 0.5).astype(np.uint8)
    true_mask = mask_to_bgr(mask, 0, 255, 0)
    p_mask = mask_to_bgr(p_mask, 0, 0, 255)
    w = cv2.addWeighted(img, 1.0, true_mask, 0.3, 0)
    w = cv2.addWeighted(w, 1.0, p_mask, 0.5, 0)
    return w
项目:Master-R-CNN    作者:Mark110    | 项目源码 | 文件源码
def decode(mask_targets, rois, classes, ih, iw):
  """Decode outputs into final masks
  Params
  ------
  mask_targets: of shape (N, h, w, K)
  rois: of shape (N, 4) [x1, y1, x2, y2]
  classes: of shape (N, 1) the class-id of each roi
  height: image height
  width:  image width

  Returns
  ------
  M: a painted image with all masks, of shape (height, width), in [0, K]
  """
  Mask = np.zeros((ih, iw), dtype=np.float32)
  assert rois.shape[0] == mask_targets.shape[0], \
    '%s rois vs %d masks' %(rois.shape[0], mask_targets.shape[0])
  num = rois.shape[0]
  rois = clip_boxes(rois, (ih, iw))
  for i in np.arange(num):
    k = classes[i]
    mask = mask_targets[i, :, :, k]
    h, w = rois[i, 3] - rois[i, 1] + 1, rois[i, 2] - rois[i, 0] + 1
    x, y = rois[i, 0], rois[i, 1]
    mask = cv2.resize(mask, (w, h), interpolation=cv2.INTER_NEAREST)
    mask *= k

    # paint
    Mask[y:y+h, x:x+w] = mask

  return Mask
项目:caffe-tools    作者:davidstutz    | 项目源码 | 文件源码
def visualize_kernels(net, layer, zoom = 5):
    """
    Visualize kernels in the given layer.

    :param net: caffe network
    :type net: caffe.Net
    :param layer: layer name
    :type layer: string
    :param zoom: the number of pixels (in width and height) per kernel weight
    :type zoom: int
    :return: image visualizing the kernels in a grid
    :rtype: numpy.ndarray
    """

    assert layer in get_layers(net), "layer %s not found" % layer

    num_kernels = net.params[layer][0].data.shape[0]
    num_channels = net.params[layer][0].data.shape[1]
    kernel_height = net.params[layer][0].data.shape[2]
    kernel_width = net.params[layer][0].data.shape[3]

    image = numpy.zeros((num_kernels*zoom*kernel_height, num_channels*zoom*kernel_width))
    for k in range(num_kernels):
        for c in range(num_channels):
            kernel = net.params[layer][0].data[k, c, :, :]
            kernel = cv2.resize(kernel, (zoom*kernel_height, zoom*kernel_width), kernel, 0, 0, cv2.INTER_NEAREST)
            kernel = (kernel - numpy.min(kernel))/(numpy.max(kernel) - numpy.min(kernel))
            image[k*zoom*kernel_height:(k + 1)*zoom*kernel_height, c*zoom*kernel_width:(c + 1)*zoom*kernel_width] = kernel

    return image
项目:Pytorch-Deeplab    作者:speedinghzl    | 项目源码 | 文件源码
def generate_scale_label(self, image, label):
        f_scale = 0.5 + random.randint(0, 11) / 10.0
        image = cv2.resize(image, None, fx=f_scale, fy=f_scale, interpolation = cv2.INTER_LINEAR)
        label = cv2.resize(label, None, fx=f_scale, fy=f_scale, interpolation = cv2.INTER_NEAREST)
        return image, label
项目:Pytorch-Deeplab    作者:speedinghzl    | 项目源码 | 文件源码
def generate_scale_label(self, image, label):
        f_scale = 0.5 + random.randint(0, 11) / 10.0
        image = cv2.resize(image, None, fx=f_scale, fy=f_scale, interpolation = cv2.INTER_LINEAR)
        label = cv2.resize(label, None, fx=f_scale, fy=f_scale, interpolation = cv2.INTER_NEAREST)
        return image, label
项目:deeptracking    作者:lvsn    | 项目源码 | 文件源码
def normalize_scale(color, depth, boundingbox, camera, output_size=(100, 100)):

    left = np.min(boundingbox[:, 1])
    right = np.max(boundingbox[:, 1])
    top = np.min(boundingbox[:, 0])
    bottom = np.max(boundingbox[:, 0])

    # Compute offset if bounding box goes out of the frame (0 padding)
    h, w, c = color.shape
    crop_w = right - left
    crop_h = bottom - top
    color_crop = np.zeros((crop_h, crop_w, 3), dtype=color.dtype)
    depth_crop = np.zeros((crop_h, crop_w), dtype=np.float)
    top_offset = abs(min(top, 0))
    bottom_offset = min(crop_h - (bottom - h), crop_h)
    right_offset = min(crop_w - (right - w), crop_w)
    left_offset = abs(min(left, 0))

    if top < 0:
        top = 0
    if left < 0:
        left = 0
    color_crop[top_offset:bottom_offset, left_offset:right_offset, :] = color[top:bottom, left:right, :]
    depth_crop[top_offset:bottom_offset, left_offset:right_offset] = depth[top:bottom, left:right]

    resized_rgb = cv2.resize(color_crop, output_size, interpolation=cv2.INTER_NEAREST)
    resized_depth = cv2.resize(depth_crop, output_size, interpolation=cv2.INTER_NEAREST)

    mask_rgb = resized_rgb != 0
    mask_depth = resized_depth != 0
    resized_depth = resized_depth.astype(np.int16)
    final_rgb = resized_rgb * mask_rgb
    final_depth = resized_depth * mask_depth
    return final_rgb, final_depth
项目:deeptracking    作者:lvsn    | 项目源码 | 文件源码
def cv_normalize_scale(color, depth, pose, camera, output_size=(100, 100), scale_size=230):
    pose = pose.inverse()
    pixels = compute_2Dboundingbox(pose, camera, scale_size)

    # pad zeros if the crop happens outside of original image
    lower_x = 0
    lower_y = 0
    higher_x = 0
    higher_y = 0
    if pixels[0, 0] < 0:
        lower_x = -pixels[0, 0]
        pixels[:, 0] += lower_x
    if pixels[0, 1] < 0:
        lower_y = -pixels[0, 1]
        pixels[:, 1] += lower_y
    if pixels[1, 0] > camera.width:
        higher_x = pixels[1, 0] - camera.width
    if pixels[1, 1] > camera.height:
        higher_y = pixels[1, 1] - camera.height

    color = np.pad(color, ((lower_y, higher_y), (lower_x, higher_x), (0, 0)), mode="constant", constant_values=0)
    depth = np.pad(depth, ((lower_y, higher_y), (lower_x, higher_x)), mode="constant", constant_values=0)
    color_crop = color[pixels[0, 0]:pixels[1, 0], pixels[0, 1]:pixels[2, 1], :]
    depth_crop = depth[pixels[0, 0]:pixels[1, 0], pixels[0, 1]:pixels[2, 1]].astype(np.float)
    mask_depth = cv2.resize(depth_crop, output_size, interpolation=cv2.INTER_NEAREST) != 0
    mask_rgb = cv2.resize(color_crop, output_size, interpolation=cv2.INTER_NEAREST) != 0
    resized_depth_crop = cv2.resize(depth_crop, output_size, interpolation=cv2.INTER_NEAREST)
    resized_color_crop = cv2.resize(color_crop, output_size, interpolation=cv2.INTER_NEAREST)
    return resized_color_crop * mask_rgb, resized_depth_crop * mask_depth
项目:DQN    作者:boluoweifenda    | 项目源码 | 文件源码
def Scale(img):
    imgResize = img.reshape([210, 160], order=0)[32:196, 8:152]
    imgScale = (cv2.resize(imgResize, (72, 82), interpolation=cv2.INTER_NEAREST)) > 0
    imgScale.dtype = 'uint8'
    return imgScale.reshape([5904], order=0)
项目:DQN    作者:boluoweifenda    | 项目源码 | 文件源码
def Scale(img):
    # cv2.imshow("Imagetest", img.reshape([210, 160], order=0))
    # cv2.waitKey (100)
    imgResize = img.reshape([210, 160], order=0)[32:196, 8:152]
    imgScale = (cv2.resize(imgResize, (width, height), interpolation=cv2.INTER_NEAREST)) / 255.

    return imgScale
项目:DQN    作者:boluoweifenda    | 项目源码 | 文件源码
def Scale(img):
    imgResize = img.reshape([210, 160], order=0)[32:196, 8:152]
    imgScale = (cv2.resize(imgResize, (36, 41), interpolation=cv2.INTER_NEAREST)) > 0
    imgScale.dtype = 'uint8'
    return imgScale.reshape([1476],order = 0)
项目:DQN    作者:boluoweifenda    | 项目源码 | 文件源码
def Scale(img):
    imgResize = img.reshape([210, 160], order=0)[32:196, 8:152]
    imgScale = (cv2.resize(imgResize, (72, 82), interpolation=cv2.INTER_NEAREST)) > 0
    imgScale.dtype = 'uint8'
    return imgScale.reshape([5904],order = 0)
项目:DQN    作者:boluoweifenda    | 项目源码 | 文件源码
def Scale(img):
    imgResize = img.reshape([210, 160], order=0)[32:196, 8:152]
    imgScale = (cv2.resize(imgResize, (72, 82), interpolation=cv2.INTER_NEAREST)) > 0
    imgScale.dtype = 'uint8'
    return imgScale.reshape([5904],order = 0)
项目:DQN    作者:boluoweifenda    | 项目源码 | 文件源码
def Scale(img):
    imgResize = img.reshape([210, 160], order=0)[32:196, 8:152]
    imgScale = (cv2.resize(imgResize, (72, 82), interpolation=cv2.INTER_NEAREST)) > 0
    imgScale.dtype = 'uint8'
    return imgScale.reshape([5904],order = 0)
项目:HandwritingRecognition    作者:eng-tsmith    | 项目源码 | 文件源码
def squeeze(image, maxlen, border):
    image_wd = image.shape[1]
    image_ht = image.shape[0]
    basewidth = maxlen - border

    wpercent = basewidth / image_wd
    dim = (int(wpercent*image_wd), image_ht)
    img_squeeze= cv.resize(image, dim, interpolation=cv.INTER_NEAREST)
    return img_squeeze
项目:HandwritingRecognition    作者:eng-tsmith    | 项目源码 | 文件源码
def skew(img):
    """
    This function detects skew in images. It turn the image so that the baseline of image is straight.
    :param img: the image
    :return: rotated image
    """
    # coordinates of bottom black pixel in every column
    black_pix = np.zeros((2, 1))

    # Look at image column wise and in every column from bottom to top pixel. It stores the location of the first black
    # pixel in every column
    for columns in range(img.shape[1]):
        for pixel in np.arange(img.shape[0]-1, -1, -1):
            if img[pixel][columns] == 255:
                black_pix = np.concatenate((black_pix, np.array([[pixel], [columns]])), axis=1)
                break

    # Calculate linear regression to detect baseline
    mean_x = np.mean(black_pix[1][:])
    mean_y = np.mean(black_pix[0][:])
    k = black_pix.shape[1]
    a = (np.sum(black_pix[1][:] * black_pix[0][:]) - k * mean_x * mean_y) / (np.sum(black_pix[1][:] * black_pix[1][:]) - k * mean_x * mean_x)

    # Calculate angle by looking at gradient of linear function + data augmentation
    angle = np.arctan(a) * 180 / np.pi #+ random.uniform(-1, 1) #TODO dataug

    # Rotate image and use Nearest Neighbour for interpolation of pixel
    rows, cols = img.shape
    M = cv.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
    img_rot = cv.warpAffine(img, M, (cols, rows), flags=cv.INTER_NEAREST)

    return img_rot