我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.INTER_LINEAR。
def downscale(old_file_name): img = cv2.imread(os.path.join(old_file_name)) new_file_name = (old_file_name .replace('training', 'training_' + str(min_size)) .replace('validation', 'validation_' + str(min_size)) .replace('testing', 'testing_' + str(min_size)) ) height, width, _ = img.shape if width > height: new_width = int(1.0 * width / height * min_size) new_height = min_size else: new_height = int(1.0 * height / width * min_size) new_width = min_size img_new = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR) cv2.imwrite(new_file_name, img_new)
def RandomCrop(rand_seed,img, top,left,height=224, width=224,u=0.5,aug_factor=9/8): #first zoom in by a factor of aug_factor of input img,then random crop by(height,width) # if rand_seed < u: if 1: # h,w,c = img.shape # img = cv2.resize(img, (round(aug_factor*w), round(aug_factor*h)), interpolation=cv2.INTER_LINEAR) # h, w, c = img.shape new_h, new_w = height,width # top = np.random.randint(0, h - new_h) # left = np.random.randint(0, w - new_w) img = img[top: top + new_h, left: left + new_w] return img
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
def resize(im, target_size, max_size): """ only resize input image to target size and return scale :param im: BGR image input by opencv :param target_size: one dimensional size (the short side) :param max_size: one dimensional max size (the long side) :return: """ im_shape = im.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_min) # prevent bigger axis from being more than max_size: if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) return im, im_scale
def resize(im, target_size, max_size): """ only resize input image to target size and return scale :param im: BGR image input by opencv :param target_size: one dimensional size (the short side) :param max_size: one dimensional max size (the long side) :return: """ im_shape = im.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_min) if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) return im, im_scale
def prep_im_for_blob(im, pixel_means, target_size, max_size): """Mean subtract and scale an image for use in a blob.""" im = im.astype(np.float32, copy=False) im -= pixel_means im = im / 127.5 im_shape = im.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) return im, im_scale
def _get_image_blob(roidb, scale_inds): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) processed_ims = [] im_scales = [] for i in xrange(num_images): im = cv2.imread(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] im_orig = im.astype(np.float32, copy=True) im_orig -= cfg.PIXEL_MEANS im_scale = cfg.TRAIN.SCALES_BASE[scale_inds[i]] im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob, im_scales
def _get_image_blob_multiscale(roidb): """Builds an input blob from the images in the roidb at multiscales. """ num_images = len(roidb) processed_ims = [] im_scales = [] scales = cfg.TRAIN.SCALES_BASE for i in xrange(num_images): im = cv2.imread(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] im_orig = im.astype(np.float32, copy=True) im_orig -= cfg.PIXEL_MEANS for im_scale in scales: im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob, im_scales
def _get_image_blob(ims, target_size): """Converts an image into a network input. Arguments: im (ndarray): a color image in BGR order Returns: blob (ndarray): a data blob holding an image pyramid im_infos(ndarray): a data blob holding input size pyramid """ processed_ims = [] for im in ims: im = im.astype(np.float32, copy = False) im = im - cfg.PIXEL_MEANS im_shape = im.shape[0:2] im = cv2.resize(im, None, None, fx = float(target_size) / im_shape[1], \ fy = float(target_size) / im_shape[0], interpolation = cv2.INTER_LINEAR) processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob
def get_image_blob_noscale(self, im): im_orig = im.astype(np.float32, copy=True) im_orig -= self.PIXEL_MEANS im_shape = im_orig.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) processed_ims = [] im_scale_factors = [] target_size = self.SCALES[0] im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > self.MAX_SIZE: im_scale = float(self.MAX_SIZE) / float(im_size_max) im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scale_factors.append(im_scale) processed_ims.append(im) blob = im_list_to_blob(processed_ims) return blob, np.array(im_scale_factors)
def prep_im_for_blob(im, pixel_means, target_size, max_size): """Mean subtract and scale an image for use in a blob.""" im = im.astype(np.float32, copy=False) im -= pixel_means im_shape = im.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) if cfg.TRAIN.RANDOM_DOWNSAMPLE: r = 0.6 + np.random.rand() * 0.4 im_scale *= r im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) return im, im_scale
def _aligned(im_ref, im, im_to_align=None, key=None): w, h = im.shape[:2] im_ref = cv2.resize(im_ref, (h, w), interpolation=cv2.INTER_CUBIC) im_ref = _preprocess_for_alignment(im_ref) if im_to_align is None: im_to_align = im im_to_align = _preprocess_for_alignment(im_to_align) assert im_ref.shape[:2] == im_to_align.shape[:2] try: cc, warp_matrix = _get_alignment(im_ref, im_to_align, key) except cv2.error as e: logger.info('Error getting alignment: {}'.format(e)) return im, False else: im = cv2.warpAffine(im, warp_matrix, (h, w), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP) im[im == 0] = np.mean(im) return im, True
def _get_image_blob(roidb, scale_inds): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) processed_ims = [] im_scales = [] for i in range(num_images): im = cv2.imread(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] im_orig = im.astype(np.float32, copy=True) im_orig -= cfg.PIXEL_MEANS im_scale = cfg.TRAIN.SCALES_BASE[scale_inds[i]] im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob, im_scales
def _get_image_blob_multiscale(roidb): """Builds an input blob from the images in the roidb at multiscales. """ num_images = len(roidb) processed_ims = [] im_scales = [] scales = cfg.TRAIN.SCALES_BASE for i in range(num_images): im = cv2.imread(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] im_orig = im.astype(np.float32, copy=True) im_orig -= cfg.PIXEL_MEANS for im_scale in scales: im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob, im_scales
def _feature(image): """ It's faster but still accurate enough with DSIZE = 14. ~0.9983 precision and recall :param image: :return: raw pixels as feature vector """ image = cv2.resize(image, None, fx=DSIZE/28, fy=DSIZE/28, interpolation=cv2.INTER_LINEAR) ret = image.astype(np.float32) / 255 return ret.ravel()
def prep_im_for_blob(im): target_size = 224 pixel_means = np.array([[[102.9801, 115.9465, 122.7717]]]) im = im.astype(np.float32, copy=False) im -= pixel_means im = cv2.resize(im, (224,224), interpolation=cv2.INTER_LINEAR) return im
def resize_blob(blob, dest_shape=None, im_scale=None, method=None): assert dest_shape != None or im_scale != None img = blob.transpose((1, 2, 0)) if method is None: method = cv2.INTER_LINEAR if dest_shape is not None: dest_shape = dest_shape[1], dest_shape[0] img = cv2.resize(img, dest_shape, interpolation=method) else: img = cv2.resize(img, None, None, fx=im_scale, fy=im_scale, interpolation=method) if len(img.shape) == 2: img = img[:, :, np.newaxis] blob = img.transpose((2, 0, 1)) return blob
def _get_image_blob(im): """Converts an image into a network input. Arguments: im (ndarray): a color image in BGR order Returns: blob (ndarray): a data blob holding an image pyramid im_scale_factors (list): list of image scales (relative to im) used in the image pyramid """ im_orig = im.astype(np.float32, copy=True) im_orig -= cfg.PIXEL_MEANS im_shape = im_orig.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) processed_ims = [] im_scale_factors = [] for target_size in cfg.TEST.SCALES: im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE: im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max) im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scale_factors.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob, np.array(im_scale_factors)
def prep_im_for_blob(im, pixel_means, target_size, max_size): """Mean subtract and scale an image for use in a blob.""" im = im.astype(np.float32, copy=False) im -= pixel_means im_shape = im.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) return im, im_scale
def warp_flow(img, flow): h, w = flow.shape[:2] flow = -flow flow[:,:,0] += np.arange(w) flow[:,:,1] += np.arange(h)[:,np.newaxis] res = cv2.remap(img, flow, None, cv2.INTER_LINEAR) return res
def apply_affine(img, mat): return cv.warpAffine(img, mat, img.shape, flags=cv.INTER_LINEAR)
def downscale(old_file_name): img = cv2.imread(os.path.join(old_file_name)) new_file_name = (old_file_name .replace('training', 'training_new') .replace('validation', 'validation_new') .replace('testing', 'testing_new') ) if 'instances' in new_file_name: img_new = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR) else: img_new = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC) cv2.imwrite(new_file_name, img_new)
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
def _get_image_blob(im): """Converts an image into a network input. Arguments: im (ndarray): a color image in BGR order Returns: blob (ndarray): a data blob holding an image pyramid im_scale_factors (list): list of image scales (relative to im) used in the image pyramid im_shapes: the list of image shapes """ im_orig = im.astype(np.float32, copy=True) im_orig -= cfg.PIXEL_MEANS im_shape = im_orig.shape im_size_max = np.max(im_shape[0:2]) processed_ims = [] im_scale_factors = [] im_shapes = [] for target_size in cfg.TEST.SCALES: im_scale = float(target_size) / float(im_size_max) im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scale_factors.append(im_scale) processed_ims.append(im_list_to_blob([im])) im_shapes.append(np.array(im.shape[1::-1], dtype=np.float32).reshape(1, -1)) blob = processed_ims return blob, np.array(im_scale_factors), im_shapes
def prep_im_for_blob(im, pixel_means, target_size): """Mean subtract and scale an image for use in a blob.""" im = im.astype(np.float32, copy=False) im -= pixel_means im_shape = im.shape im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_max) im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) return im, im_scale, np.array(im.shape[1::-1], dtype=np.float32)
def _process_frame84(frame): img = np.reshape(frame, [210, 160, 3]).astype(np.float32) img = img[:, :, 0] * 0.299 + img[:, :, 1] * 0.587 + img[:, :, 2] * 0.114 resized_screen = cv2.resize(img, (84, 110), interpolation=cv2.INTER_LINEAR) x_t = resized_screen[18:102, :] x_t = np.reshape(x_t, [84, 84, 1]) return x_t.astype(np.uint8)
def _get_image_blob(im): """Converts an image into a network input. Arguments: im (ndarray): a color image in BGR order Returns: blob (ndarray): a data blob holding an image pyramid im_scale_factors (list): list of image scales (relative to im) used in the image pyramid """ im_orig = im.astype(np.float32, copy=True) im_orig -= cfg.PIXEL_MEANS im_shape = im_orig.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) processed_ims = [] assert len(cfg.TEST.SCALES) == 1 target_size = cfg.TEST.SCALES[0] im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE: im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max) im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_info = np.hstack((im.shape[:2], im_scale))[np.newaxis, :] processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob, im_info
def _get_image_blob(im): """Converts an image into a network input. Arguments: im (ndarray): a color image in BGR order Returns: blob (ndarray): a data blob holding an image pyramid im_scale_factors (list): list of image scales (relative to im) used in the image pyramid """ im_orig = im.astype(np.float32, copy=True) im_orig -= 127.5 im_orig = im_orig / 127.5 im_shape = im_orig.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) processed_ims = [] im_scale_factors = [] for target_size in cfg.TEST.SCALES: im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE: im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max) im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scale_factors.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob, np.array(im_scale_factors)
def rotate_image(img, angle, interp=cv2.INTER_LINEAR): rows, cols = img.shape[:2] rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1) return cv2.warpAffine(img, rotation_matrix, (cols, rows), flags=interp)
def resize_image(im, size, interp=cv2.INTER_LINEAR): im_resized = cv2.resize(im, (size[1], size[0]), interpolation=interp) # swap sizes to account for weird OCV API return im_resized
def remap(self, src): """ :param src: source image :type src: :class:`cvMat` Apply the post-calibration undistortion to the source image """ return cv2.remap(src, self.mapx, self.mapy, cv2.INTER_LINEAR)
def im_resize(im, landmark, ang): """Resize the image according to the distance between eyes or mouth. Args: rot_landmark: rotated 5 landmark points im: rotated image mode: resize mode Return: A resized image. Resize scale. resized landmark points. """ if cfg.resize_mode == 0: resize_scale = float(cfg.eye_dist) / float(landmark[2] - landmark[0]) resize_x = int(round(im.shape[1] * resize_scale)) resize_y = int(round(im.shape[0] * resize_scale)) im_resize = cv2.resize(im, (resize_x, resize_y), interpolation=cv2.INTER_LINEAR) rez_landmark = np.round(landmark.astype(np.float) * resize_scale).astype(np.int) # return im_resize, resize_scale, rez_landmark elif cfg.resize_mode == 1: eye_c_y = int(round((landmark[1] + landmark[3]) / 2.0)) mouth_c_y = int(round((landmark[7] + landmark[9]) / 2.0)) resize_scale = float(cfg.ec_mc_y) / float(mouth_c_y - eye_c_y) resize_x = int(round(im.shape[1] * resize_scale)) resize_y = int(round(im.shape[0] * resize_scale)) im_resize = cv2.resize(im, (resize_x, resize_y), interpolation=cv2.INTER_LINEAR) rez_landmark = np.round(landmark.astype(np.float) * resize_scale).astype(np.int) return im_resize, resize_scale, rez_landmark
def upsample_image(im, sz): h = im.shape[0] w = im.shape[1] s = np.float(max(h, w)) I_out = np.zeros((sz, sz, 3), dtype=np.float); I = cv2.resize(im, None, None, fx=np.float(sz) / s, fy=np.float(sz) / s, interpolation=cv2.INTER_LINEAR); SZ = I.shape; I_out[0:I.shape[0], 0:I.shape[1], :] = I; return I_out, I, SZ