我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用cv2.INTER_LANCZOS4。
def get_data(image_id, a_size, m_size, p_size, sf): rgb_data = get_rgb_data(image_id) rgb_data = cv2.resize(rgb_data, (p_size*sf, p_size*sf), interpolation=cv2.INTER_LANCZOS4) # rgb_data = rgb_data.astype(np.float) / 2500. # print(np.max(rgb_data), np.mean(rgb_data)) # rgb_data[:, :, 0] = exposure.equalize_adapthist(rgb_data[:, :, 0], clip_limit=0.04) # rgb_data[:, :, 1] = exposure.equalize_adapthist(rgb_data[:, :, 1], clip_limit=0.04) # rgb_data[:, :, 2] = exposure.equalize_adapthist(rgb_data[:, :, 2], clip_limit=0.04) A_data = get_spectral_data(image_id, a_size*sf, a_size*sf, bands=['A']) M_data = get_spectral_data(image_id, m_size*sf, m_size*sf, bands=['M']) P_data = get_spectral_data(image_id, p_size*sf, p_size*sf, bands=['P']) # lab_data = cv2.cvtColor(rgb_data, cv2.COLOR_BGR2LAB) P_data = np.concatenate([rgb_data, P_data], axis=2) return A_data, M_data, P_data
def dispact_and_update(img, hack, base_im, x, y, w, h): try: myurl = "http://facejack.westeurope.cloudapp.azure.com:5001/imsend" headers = { 'content-type': "application/x-www-form-urlencoded", 'cache-control': "no-cache" } r = requests.post(url=myurl, data=img, headers=headers, params={'hack': str(hack)}).json() reply = 'authentication' in r and r['authentication'] == "ALLOWED" disp_face = cv2.resize(base_im[y:y + h, x:x + w], (224, 224), 0, 0, cv2.INTER_LANCZOS4) if reply: cv2.rectangle(disp_face, (0, 0), (222, 222), (0, 255, 0), 2) else: cv2.rectangle(disp_face, (0, 0), (222, 222), (0, 0, 255), 2) cv2.imshow("Face", disp_face) finally: myl.release()
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))
def augmentate(self): angles = [45, 90, 135, 180, 225, 270, 315] scale = 1.0 for img in self.images: print "image shape : ", img.shape w = img.shape[1] h = img.shape[0] img_vmirror = cv2.flip(img,1) skimage.io.imsave("testv"+".jpg", img_vmirror ) for angle in angles: #rangle = np.deg2rad(angle) # nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale # nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale rot_mat = cv2.getRotationMatrix2D((w*0.5, h*0.5), angle, scale) # rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0])) # rot_mat[0,2] += rot_move[0] # rot_mat[1,2] += rot_move[1] new_img = cv2.warpAffine(img, rot_mat, (int(math.ceil(w)), int(math.ceil(h))), flags=cv2.INTER_LANCZOS4) skimage.io.imsave("test"+str(angle)+".jpg", new_img) new_img_vmirror = cv2.flip(new_img, 1) skimage.io.imsave("testv"+str(angle)+".jpg", new_img_vmirror) # img_rmirror = cv2.flip(new_img, 0) # skimage.io.imsave("testh"+str(angle)+".jpg", img_rmirror)
def load_and_augmentate(self, root): angles = [45, 90, 135, 180, 225, 270, 315] scale = 1.0 for img_dir in os.listdir(root): img_dir_path = os.path.join(root, img_dir) for img in os.listdir(img_dir_path): img_path = os.path.join(img_dir_path, img) image = caffe.io.load_image(img_path,color=True) w = image.shape[1] h = image.shape[0] img_name = img.split(".")[0] img_type = img.split(".")[-1] img_vmirror = cv2.flip(image,1) img_vmirror_path = os.path.join(img_dir_path,img_name+"_v."+img_type) skimage.io.imsave(img_vmirror_path, img_vmirror ) for angle in angles: rot_mat = cv2.getRotationMatrix2D((w*0.5, h*0.5), angle, scale) new_img = cv2.warpAffine(image, rot_mat, (int(math.ceil(w)), int(math.ceil(h))), flags=cv2.INTER_LANCZOS4) new_img_path = os.path.join(img_dir_path,img_name+"_"+str(angle)+"."+img_type) skimage.io.imsave(new_img_path, new_img) new_img_vmirror = cv2.flip(new_img, 1) new_img_vmirror_path = os.path.join(img_dir_path, img_name+"_"+str(angle)+"_v."+img_type) skimage.io.imsave(new_img_vmirror_path, new_img_vmirror)
def rotate_about_center(src, angle, scale=1.): if angle == 0: return src w = src.shape[1] h = src.shape[0] rangle = np.deg2rad(angle) # angle in radians # now calculate new image width and height nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale # ask OpenCV for the rotation matrix rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale) # calculate the move from the old center to the new center combined # with the rotation rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0])) # the move only affects the translation, so update the translation # part of the transform rot_mat[0,2] += rot_move[0] rot_mat[1,2] += rot_move[1] return cv2.warpAffine(src, rot_mat, (int(math.ceil(nw)), int(math.ceil(nh))), flags=cv2.INTER_LANCZOS4) #????????
def read_image(file, cam, boxsize, type): # from file if type == 'IMAGE': oriImg = cv2.imread(file) # from webcam elif type == 'WEBCAM': _, oriImg = cam.read() # from video elif type == 'VIDEO': oriImg = cv2.cvtColor(file, cv2.COLOR_BGR2RGB) if oriImg is None: print('oriImg is None') return None scale = boxsize / (oriImg.shape[0] * 1.0) imageToTest = cv2.resize(oriImg, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_LANCZOS4) output_img = np.ones((boxsize, boxsize, 3)) * 128 img_h = imageToTest.shape[0] img_w = imageToTest.shape[1] if img_w < boxsize: offset = img_w % 2 # make the origin image be the center output_img[:, int(boxsize / 2 - math.floor(img_w / 2)):int( boxsize / 2 + math.floor(img_w / 2) + offset), :] = imageToTest else: # crop the center of the origin image output_img = imageToTest[:, int(img_w / 2 - boxsize / 2):int(img_w / 2 + boxsize / 2), :] return output_img
def rotate_about_center(src, angle, scale=1.): w = src.shape[1] h = src.shape[0] rangle = np.deg2rad(angle) # angle in radians nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale) rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0])) rot_mat[0,2] += rot_move[0] rot_mat[1,2] += rot_move[1] return cv2.warpAffine(src, rot_mat, (int(math.ceil(nw)), int(math.ceil(nh))), flags=cv2.INTER_LANCZOS4)
def make_img(self,size): c_w ,c_h = max(1,size[0]/30),max(1,size[1]/30) coarse = np.random.randint(0,200,size=(int(c_h),int(c_w),3)).astype(np.uint8) # coarse[:,:,1] /=5 # coarse[:,:,2] *=0 # coarse[:,:,1] /=30 # self._img = np.ones((size[1],size[0],3),dtype=np.uint8) self._img = cv2.resize(coarse,size,interpolation=cv2.INTER_LANCZOS4)
def get_spectral_data(img_id, h, w, bands=['A', 'M', 'P']): res = [] for waveband in bands: image_path = '{}/{}_{}.tif'.format(sixteen_band_path, img_id, waveband) image = tiff.imread(image_path) if len(image.shape) == 2: # for panchromatic band image.shape = (1,) + image.shape image = image.transpose((1, 2, 0)) image = cv2.resize(image, (w, h), interpolation=cv2.INTER_LANCZOS4) if len(image.shape) == 2: # for panchromatic band image.shape += (1,) res.append(image) image = np.concatenate(res, axis=2) image = image.astype(np.float32) return image
def get_rgb_image(img_id, h=None, w=None): image = get_rgb_data(img_id) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) for c in range(3): min_val, max_val = np.percentile(image[:, :, c], [2, 98]) image[:, :, c] = 255*(image[:, :, c] - min_val) / (max_val - min_val) image[:, :, c] = np.clip(image[:, :, c], 0, 255) image = (image).astype(np.uint8) if h and w: image = cv2.resize(image, (w, h), interpolation=cv2.INTER_LANCZOS4) return image
def get_polygon_train_image(img_id, h, w): im = cv2.imread('train_poly/{}.png'.format(img_id)) im = cv2.resize(im, (w, h), interpolation=cv2.INTER_LANCZOS4) return im
def shiftImage(u, v, t, img, interpolation=cv2.INTER_LANCZOS4): ''' remap an image using velocity field ''' ny,nx = u.shape sy, sx = np.mgrid[:float(ny):1,:float(nx):1] sx += u*t sy += v*t return cv2.remap(img.astype(np.float32), (sx).astype(np.float32), (sy).astype(np.float32), interpolation)
def fastFilter(arr, ksize=30, every=None, resize=True, fn='median', interpolation=cv2.INTER_LANCZOS4, smoothksize=0, borderMode=cv2.BORDER_REFLECT): ''' fn['nanmean', 'mean', 'nanmedian', 'median'] a fast 2d filter for large kernel sizes that also works with nans the computation speed is increased because only 'every'nsth position within the median kernel is evaluated ''' if every is None: every = max(ksize//3, 1) else: assert ksize >= 3*every s0,s1 = arr.shape[:2] ss0 = s0//every every = s0//ss0 ss1 = s1//every out = np.full((ss0+1,ss1+1), np.nan) c = {'median':_calcMedian, 'nanmedian':_calcNanMedian, 'nanmean':_calcNanMean, 'mean':_calcMean, }[fn] ss0,ss1 = c(arr, out, ksize, every) out = out[:ss0,:ss1] if smoothksize: out = gaussian_filter(out, smoothksize) if not resize: return out return cv2.resize(out, arr.shape[:2][::-1], interpolation=interpolation)
def correct(self, img): ''' ...from perspective distortion: --> perspective transformation --> apply tilt factor (view factor) correction ''' print("CORRECT PERSPECTIVE ...") self.img = imread(img) if not self._homography_is_fixed: self._homography = None h = self.homography if self.opts['do_correctIntensity']: tf = self.tiltFactor() self.img = np.asfarray(self.img) if self.img.ndim == 3: for col in range(self.img.shape[2]): self.img[..., col] /= tf else: self.img = self.img / tf warped = cv2.warpPerspective(self.img, h, self._newBorders[::-1], flags=cv2.INTER_LANCZOS4, **self.opts['cv2_opts']) return warped
def load_augmentate_and_label(img_dir_path, img_path, label_path, label): angles = [45, 90, 135, 180, 225, 270, 315] label_file = open(label_path, "a+") scale = 1.0 image = caffe.io.load_image(img_path,color=True) img = img_path.split("/")[-1] w = image.shape[1] h = image.shape[0] img_name = img.split(".")[0] img_type = img.split(".")[-1] img_vmirror = cv2.flip(image,1) label_str = img + " " + str(label) + "\n" label_file.write(label_str) img_vmirror_path = os.path.join(img_dir_path,img_name+"_v."+img_type) skimage.io.imsave(img_vmirror_path, img_vmirror ) label_str = img_name+"_v."+img_type + " " + str(label) + "\n" label_file.write(label_str) for angle in angles: rot_mat = cv2.getRotationMatrix2D((w*0.5, h*0.5), angle, scale) new_img = cv2.warpAffine(image, rot_mat, (int(math.ceil(w)), int(math.ceil(h))), flags=cv2.INTER_LANCZOS4) new_img_path = os.path.join(img_dir_path,img_name+"_"+str(angle)+"."+img_type) label_str = img_name + "_" + str(angle) + "." + img_type + " " + str(label) + "\n" label_file.write(label_str) skimage.io.imsave(new_img_path, new_img) new_img_vmirror = cv2.flip(new_img, 1) new_img_vmirror_path = os.path.join(img_dir_path, img_name+"_"+str(angle)+"_v."+img_type) label_str = img_name+"_"+str(angle)+"_v."+img_type+" "+str(label)+"\n" label_file.write(label_str) skimage.io.imsave(new_img_vmirror_path, new_img_vmirror)
def resize(image): """Resize a face image to the proper size for training and detection. """ return cv2.resize(image, (config.FACE_WIDTH, config.FACE_HEIGHT), interpolation=cv2.INTER_LANCZOS4)
def _data_augmentation(self, data, label): """ perform data augmentations: crop, mirror, resize, sub mean, swap channels... """ if self.is_train and self._rand_sampler: width = data.shape[1] height = data.shape[0] rand_crop = self._rand_sampler.sample(label, (height, width)) xmin, ymin, xmax, ymax = np.array(rand_crop[0]).astype(int) data = crop_roi_patch(data.asnumpy(), (xmin, ymin, xmax, ymax)) label = rand_crop[1] if self.is_train: interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, \ cv2.INTER_NEAREST, cv2.INTER_LANCZOS4] else: interp_methods = [cv2.INTER_LINEAR] interp_method = interp_methods[int(np.random.uniform(0, 1) * len(interp_methods))] data = mx.img.imresize(data, self._data_shape[1], self._data_shape[0], interp_method) if self._rand_eraser and self.is_train: label_scaler = np.array((self._data_shape[0], self._data_shape[1])) label_scaler = np.tile(np.reshape(label_scaler, (1, -1)), (1, 2)) data = mx.nd.array(self._rand_eraser.sample(data.asnumpy(), label[:, 1:] * label_scaler)) if self.is_train: valid_mask = np.where(np.any(label != -1, axis=1))[0] if self._rand_mirror: rr = rand_crop[2] if np.random.uniform(0, 1) > 0.5: data = mx.nd.flip(data, axis=1) tmp = rr - label[valid_mask, 1] label[valid_mask, 1] = rr - label[valid_mask, 3] label[valid_mask, 3] = tmp # label[valid_mask, 1::2] *= data.shape[1] # label[valid_mask, 2::2] *= data.shape[0] data = mx.nd.transpose(data, (2,0,1)) data = data.astype('float32') data = data - self._mean_pixels return data, label
def resize_image(image, params={}): if params.has_key('height') and params.has_key('width'): if np.random.uniform() < params.get('resize_prob', 1.0): interpolation_map = { 'LINEAR': cv2.INTER_LINEAR, 'AREA': cv2.INTER_AREA, 'NEAREST': cv2.INTER_NEAREST, 'CUBIC': cv2.INTER_CUBIC, 'LANCZOS4': cv2.INTER_LANCZOS4, } interp = interpolation_map[np.random.choice(params.get('interpolation', ['LINEAR']))] image,_ = resize_image_by_warp(image, params['width'], params['height'], interp) return image
def resize(image): """Resize a face image to the proper size for training and detection. """ return cv2.resize(image, (FACE_WIDTH, FACE_HEIGHT), interpolation=cv2.INTER_LANCZOS4)
def preprocess_images(): if os.path.isfile(IMG_PATH): print("%s exists, nothing to do." % IMG_PATH) return print("Reading images from img_align_celeba/ ...") raw_images = [] for i in range(1, N_IMAGES + 1): if i % 10000 == 0: print(i) raw_images.append(mpimg.imread('img_align_celeba/%06i.jpg' % i)[20:-20]) if len(raw_images) != N_IMAGES: raise Exception("Found %i images. Expected %i" % (len(raw_images), N_IMAGES)) print("Resizing images ...") all_images = [] for i, image in enumerate(raw_images): if i % 10000 == 0: print(i) assert image.shape == (178, 178, 3) if IMG_SIZE < 178: image = cv2.resize(image, (IMG_SIZE, IMG_SIZE), interpolation=cv2.INTER_AREA) elif IMG_SIZE > 178: image = cv2.resize(image, (IMG_SIZE, IMG_SIZE), interpolation=cv2.INTER_LANCZOS4) assert image.shape == (IMG_SIZE, IMG_SIZE, 3) all_images.append(image) data = np.concatenate([img.transpose((2, 0, 1))[None] for img in all_images], 0) data = torch.from_numpy(data) assert data.size() == (N_IMAGES, 3, IMG_SIZE, IMG_SIZE) print("Saving images to %s ..." % IMG_PATH) torch.save(data[:20000].clone(), 'images_%i_%i_20000.pth' % (IMG_SIZE, IMG_SIZE)) torch.save(data, IMG_PATH)
def resize(image): """Resize a face image to the proper size for training and detection. """ return cv2.resize(image, (config.get("face_width"), config.get("face_height")), interpolation=cv2.INTER_LANCZOS4)
def applyLinearTransformToImage(self, image, angle, shear_x, shear_y, scale, size_out): '''Apply the image transformation specified by three parameters. Time it takes warping a 256 x 256 RGB image with various affine warping functions: * 0.25ms cv2.warpImage, nearest interpolation * 0.26ms cv2.warpImage, linear interpolation * 5.11ms ndii.affine_transform, order=0 * 5.93ms skimage.transform._warps_cy._warp_fast, linear interpolation Args: x: 2D numpy array, a single image. angle: Angle by which the image is rotated. shear_x: Shearing factor along the x-axis by which the image is sheared. shear_y: Shearing factor along the x-axis by which the image is sheared. scale: Scaling factor by which the image is scaled. channel_axis: Index of axis for channels in the input tensor. Returns: A tuple of transformed version of the input and the correction scale factor. ''' # Positions of the image center before and after the transformation in # pixel coordinates. s_out = (size_out, size_out) c_in = .5 * np.asarray(image.shape[:2], dtype=np.float64).reshape((2, 1)) c_out = .5 * np.asarray(s_out, dtype=np.float64).reshape((2, 1)) angle = -angle M_rot_inv = np.asarray([[math.cos(angle), -math.sin(angle)], \ [math.sin(angle), math.cos(angle)]]) M_shear_inv = (1. / (shear_x * shear_y - 1.)) \ * np.asarray([[-1., shear_x], [shear_y, -1.]]) M_inv = np.dot(M_shear_inv, M_rot_inv) # First undo rotation, then shear. M_inv /= scale offset = c_in - np.dot(M_inv, c_out) # cv2.warpAffine transform according to dst(p) = src(M_inv * p + offset). # No need to reverse the channels because the channels are interpolated # separately. warped = cv2.warpAffine(image, np.concatenate((M_inv, offset), axis=1), s_out, flags=cv2.INTER_LANCZOS4 | cv2.WARP_INVERSE_MAP) # flags=cv2.INTER_CUBIC | cv2.WARP_INVERSE_MAP) return warped
def _data_augmentation(self, data, label): """ perform data augmentations: crop, mirror, resize, sub mean, swap channels... """ if self.is_train and self._rand_samplers: rand_crops = [] for rs in self._rand_samplers: rand_crops += rs.sample(label) num_rand_crops = len(rand_crops) # randomly pick up one as input data if num_rand_crops > 0: index = int(np.random.uniform(0, 1) * num_rand_crops) width = data.shape[1] height = data.shape[0] crop = rand_crops[index][0] xmin = int(crop[0] * width) ymin = int(crop[1] * height) xmax = int(crop[2] * width) ymax = int(crop[3] * height) if xmin >= 0 and ymin >= 0 and xmax <= width and ymax <= height: data = mx.img.fixed_crop(data, xmin, ymin, xmax-xmin, ymax-ymin) else: # padding mode new_width = xmax - xmin new_height = ymax - ymin offset_x = 0 - xmin offset_y = 0 - ymin data_bak = data data = mx.nd.full((new_height, new_width, 3), 128, dtype='uint8') data[offset_y:offset_y+height, offset_x:offset_x + width, :] = data_bak label = rand_crops[index][1] if self.is_train: interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, \ cv2.INTER_NEAREST, cv2.INTER_LANCZOS4] else: interp_methods = [cv2.INTER_LINEAR] interp_method = interp_methods[int(np.random.uniform(0, 1) * len(interp_methods))] data = mx.img.imresize(data, self._data_shape[1], self._data_shape[0], interp_method) if self.is_train and self._rand_mirror: if np.random.uniform(0, 1) > 0.5: data = mx.nd.flip(data, axis=1) valid_mask = np.where(label[:, 0] > -1)[0] tmp = 1.0 - label[valid_mask, 1] label[valid_mask, 1] = 1.0 - label[valid_mask, 3] label[valid_mask, 3] = tmp data = mx.nd.transpose(data, (2,0,1)) data = data.astype('float32') data = data - self._mean_pixels return data, label
def _data_augmentation(self, data, label): """ perform data augmentations: crop, mirror, resize, sub mean, swap channels... """ if self.is_train and self._rand_samplers: rand_crops = [] for rs in self._rand_samplers: rand_crops += rs.sample(label) num_rand_crops = len(rand_crops) # randomly pick up one as input data if num_rand_crops > 0: index = int(np.random.uniform(0, 1) * num_rand_crops) width = data.shape[1] height = data.shape[0] crop = rand_crops[index][0] xmin = int(crop[0] * width) ymin = int(crop[1] * height) xmax = int(crop[2] * width) ymax = int(crop[3] * height) if xmin >= 0 and ymin >= 0 and xmax <= width and ymax <= height: data = data[ymin:ymax, xmin:xmax, :] else: # padding mode new_width = xmax - xmin new_height = ymax - ymin offset_x = 0 - xmin offset_y = 0 - ymin data_bak = data data = np.full((new_height, new_width, 3), 128.) data[offset_y:offset_y+height, offset_x:offset_x + width, :] = data_bak label = rand_crops[index][1] if self.is_train and self._rand_mirror: if np.random.uniform(0, 1) > 0.5: data = cv2.flip(data, 1) valid_mask = np.where(label[:, 0] > -1)[0] tmp = 1.0 - label[valid_mask, 1] label[valid_mask, 1] = 1.0 - label[valid_mask, 3] label[valid_mask, 3] = tmp if self.is_train: interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, \ cv2.INTER_NEAREST, cv2.INTER_LANCZOS4] else: interp_methods = [cv2.INTER_LINEAR] interp_method = interp_methods[int(np.random.uniform(0, 1) * len(interp_methods))] data = resize(data, self._data_shape, interp_method) data = transform(data, self._mean_pixels) return data, label
def _data_augmentation(self, data, label): """ perform data augmentations: crop, mirror, resize, sub mean, swap channels... """ if self.is_train and self._rand_samplers: rand_crops = [] for rs in self._rand_samplers: rand_crops += rs.sample(label) num_rand_crops = len(rand_crops) # randomly pick up one as input data if num_rand_crops > 0: index = int(np.random.uniform(0, 1) * num_rand_crops) width = data.shape[1] height = data.shape[0] crop = rand_crops[index][0] xmin = int(crop[0] * width) ymin = int(crop[1] * height) xmax = int(crop[2] * width) ymax = int(crop[3] * height) if xmin >= 0 and ymin >= 0 and xmax <= width and ymax <= height: data = mx.img.fixed_crop(data, xmin, ymin, xmax-xmin, ymax-ymin)# if the new coordinate is greater than 0, then crop the image else: # padding mode new_width = xmax - xmin # if less than 0, then padding the image with 128 new_height = ymax - ymin offset_x = 0 - xmin offset_y = 0 - ymin data_bak = data data = mx.nd.full((new_height, new_width, 3), 128, dtype='uint8') data[offset_y:offset_y+height, offset_x:offset_x + width, :] = data_bak label = rand_crops[index][1] ######################### img resize and random horizontal flip and minus mean pixels if self.is_train: interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, \ cv2.INTER_NEAREST, cv2.INTER_LANCZOS4] else: interp_methods = [cv2.INTER_LINEAR] interp_method = interp_methods[int(np.random.uniform(0, 1) * len(interp_methods))] data = mx.img.imresize(data, self._data_shape[1], self._data_shape[0], interp_method) if self.is_train and self._rand_mirror: if np.random.uniform(0, 1) > 0.5: data = mx.nd.flip(data, axis=1) valid_mask = np.where(label[:, 0] > -1)[0] tmp = 1.0 - label[valid_mask, 1] label[valid_mask, 1] = 1.0 - label[valid_mask, 3] label[valid_mask, 3] = tmp data = mx.nd.transpose(data, (2,0,1)) data = data.astype('float32') data = data - self._mean_pixels return data, label