我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.flip()。
def _centre_crop_and_transform(self, input_img, scale=1.0, trans=False, vflip=False, hflip=False): h, w = input_img.shape[:2] cx = w // 2 cy = h // 2 crop_w, crop_h = utils.calc_crop_size(self.img_size[0], self.img_size[1], scale=scale) input_img = utils.crop_center(input_img, cx, cy, crop_w, crop_h) if trans: input_img = cv2.transpose(input_img) if hflip or vflip: if hflip and vflip: c = -1 else: c = 0 if vflip else 1 input_img = cv2.flip(input_img, flipCode=c) if scale != 1.0: input_img = cv2.resize(input_img, self.img_size, interpolation=cv2.INTER_LINEAR) return input_img
def videoize(func, args, src = 0, win_name = "Cam", delim_wait = 1, delim_key = 27): cap = cv2.VideoCapture(src) while(1): ret, frame = cap.read() # To speed up processing; Almost real-time on my PC frame = cv2.resize(frame, dsize=None, fx=0.5, fy=0.5) frame = cv2.flip(frame, 1) out = func(frame, args) if out is None: continue out = cv2.resize(out, dsize=None, fx=1.4, fy=1.4) cv2.imshow(win_name, out) cv2.moveWindow(win_name, (s_w - out.shape[1])/2, (s_h - out.shape[0])/2) k = cv2.waitKey(delim_wait) if k == delim_key: cv2.destroyAllWindows() cap.release() return
def applyTransform(self): self.framing(self.path) self.height,self.width=cv2.imread("Frames/1.jpg").shape[:2] # write transformed video out = cv2.VideoWriter("changedOutput.mp4",cv.CV_FOURCC('a','v','c','1'), 30.0, (self.width, self.height)) folder=self.sort_files() # write Transformed video frames for i in folder: pic="Frames/"+str(i)+".jpg" Newpic=cv2.imread(pic,0) frame=cv2.Canny(Newpic,100,200) cv2.imwrite(pic,frame) Newpic=cv2.imread(pic) img=cv2.flip(Newpic,0) out.write(img) out.release() # Writing output video file
def transformations(src, choice): if choice == 0: # Rotate 90 src = cv2.rotate(src, rotateCode=cv2.ROTATE_90_CLOCKWISE) if choice == 1: # Rotate 90 and flip horizontally src = cv2.rotate(src, rotateCode=cv2.ROTATE_90_CLOCKWISE) src = cv2.flip(src, flipCode=1) if choice == 2: # Rotate 180 src = cv2.rotate(src, rotateCode=cv2.ROTATE_180) if choice == 3: # Rotate 180 and flip horizontally src = cv2.rotate(src, rotateCode=cv2.ROTATE_180) src = cv2.flip(src, flipCode=1) if choice == 4: # Rotate 90 counter-clockwise src = cv2.rotate(src, rotateCode=cv2.ROTATE_90_COUNTERCLOCKWISE) if choice == 5: # Rotate 90 counter-clockwise and flip horizontally src = cv2.rotate(src, rotateCode=cv2.ROTATE_90_COUNTERCLOCKWISE) src = cv2.flip(src, flipCode=1) return src
def random_crop_and_flip(batch_data, padding_size): ''' Helper to random crop and random flip a batch of images :param padding_size: int. how many layers of 0 padding was added to each side :param batch_data: a 4D batch array :return: randomly cropped and flipped image ''' cropped_batch = np.zeros(len(batch_data) * IMG_HEIGHT * IMG_WIDTH * IMG_DEPTH).reshape( len(batch_data), IMG_HEIGHT, IMG_WIDTH, IMG_DEPTH) for i in range(len(batch_data)): x_offset = np.random.randint(low=0, high=2 * padding_size, size=1)[0] y_offset = np.random.randint(low=0, high=2 * padding_size, size=1)[0] cropped_batch[i, ...] = batch_data[i, ...][x_offset:x_offset+IMG_HEIGHT, y_offset:y_offset+IMG_WIDTH, :] cropped_batch[i, ...] = horizontal_flip(image=cropped_batch[i, ...], axis=1) return cropped_batch
def _thread(cls): # frame grabber loop while cfg.camera_active: sbuffer = StringIO.StringIO() camtest = False while camtest == False: camtest, rawimg = cfg.camera.read() if cfg.cv_hflip: rawimg = cv2.flip(rawimg, 1) if cfg.cv_vflip: rawimg = cv2.flip(rawimg, 0) imgRGB=cv2.cvtColor(rawimg, cv2.COLOR_BGR2RGB) img = Image.fromarray(imgRGB) img.save(sbuffer, 'JPEG') cls.frame = sbuffer.getvalue() # if there hasn't been any clients asking for frames in # the last 10 seconds stop the thread if time.time() - cls.last_access > 10: break
def FindUpperBoundary(path,mode,origin_img=False): # flag,up_bd,preprocess_img = UpperBoundary(path,mode) flag,up_bd,preprocess_img = UpperBoundaryUpdate(path,mode,origin_img=origin_img) crop_margin = 50 up_bd_flip = [] if flag==False: # flag_flip,up_bd_flip,preprocess_img_flip = UpperBoundary(path,mode,flip=True) flag_flip,up_bd_flip,preprocess_img_flip = UpperBoundaryUpdate(path,mode,flip=True,origin_img=origin_img) if up_bd_flip==False: return row,col = preprocess_img.shape bd_img = np.zeros((row,col),dtype=np.uint8) for pos in up_bd_flip: pos[1] = col-1-pos[1] final_up_bd = LineUpPixels(up_bd,up_bd_flip,col) # print(final_up_bd) for i in range(len(final_up_bd)): bd_img[int(final_up_bd[i])][i] = 255 return final_up_bd
def augment_image(rgbImg): augmented_images = [] # original image augmented_images.append(rgbImg) # fliped x-axis rimg = rgbImg.copy() cv2.flip(rimg, 1, rimg) augmented_images.append(rimg) # add gaussian noise for _ in range(10): gaussian_noise = rgbImg.copy() cv2.randn(gaussian_noise, 0, 150) augmented_images.append(rgbImg + gaussian_noise) augmented_images.append(rimg + gaussian_noise) for _ in range(10): uniform_noise = rgbImg.copy() cv2.randu(uniform_noise, 0, 1) augmented_images.append(rgbImg + uniform_noise) augmented_images.append(rimg + uniform_noise) return augmented_images
def _append_to_list(self, path, angle, flip = False, brightness_adjust = 1.0): """ Adds one image to the X/y_train list Parameters ---------- path : string The path to the image angle : float Steering angle flip : boolean Should we flip the image when reading the list brightness_adjusts: float Brightness adjust factor """ self.X_train.append((self._prepend_path(path), flip, brightness_adjust)) if flip: #Flipped images angle needs correction angle = angle * -1 self.y_train.append(angle)
def _append_brightness_adjusts(self, path, angle, flip, brightness_adjusts = [1.0]): """ Specifies what the needed brightness adjusts for a specific image are and appends them to X/y_train Parameters ---------- path : string The path to the image angle : float Steering angle flip : boolean Should we flip the image when reading the list brightness_adjusts: list List of floats specifying what brightness adjusts to add """ for brightness in brightness_adjusts: self._append_to_list(path, angle, flip, brightness)
def _append_adjusted_images(self, row): """ Specifies what the needed transformations for a specific row are and appends them to X/y_train Parameters ---------- row : dict A row from the CSV file """ angle = float(row[3]) ANGLE_ADJUST = 0.25 # How much to adjust angle for left/right camera # Add the image from the central camera if abs(float(row[3])) > 0.01: #Do not flip images with angle close to 0. This helps balance the data self._append_brightness_adjusts(row[0], angle, False) self._append_brightness_adjusts(row[0], angle, True) else: self._append_to_list(row[0], angle) # Add images from left/right camera self._append_brightness_adjusts(row[1], angle + ANGLE_ADJUST, False) self._append_brightness_adjusts(row[2], angle - ANGLE_ADJUST, False) # Add images from left/right camera, flipped self._append_brightness_adjusts(row[1], angle + ANGLE_ADJUST, True) self._append_brightness_adjusts(row[2], angle - ANGLE_ADJUST, True)
def augment(self, Xb): # Random number 0-1 whether we flip or not random_flip = np.random.randint(2) == 1 # Translation shift shift_x = np.random.uniform(*params.AUGMENTATION_PARAMS['translation_range']) shift_y = np.random.uniform(*params.AUGMENTATION_PARAMS['translation_range']) # Rotation, zoom rotation = np.random.uniform(*params.AUGMENTATION_PARAMS['rotation_range']) log_zoom_range = [np.log(z) for z in params.AUGMENTATION_PARAMS['zoom_range']] zoom = np.exp(np.random.uniform(*log_zoom_range)) # Color AUGMENTATION_PARAMS random_hue = np.random.uniform(*params.AUGMENTATION_PARAMS['hue_range']) random_saturation = np.random.uniform(*params.AUGMENTATION_PARAMS['saturation_range']) random_value = np.random.uniform(*params.AUGMENTATION_PARAMS['value_range']) return self.augment_with_params(Xb, shift_x, shift_y, rotation, random_flip, zoom, random_hue, random_saturation, random_value)
def imcv2_affine_trans(im): # Scale and translate h, w, c = im.shape scale = np.random.uniform() / 10. + 1. max_offx = (scale - 1.) * w max_offy = (scale - 1.) * h offx = int(np.random.uniform() * max_offx) offy = int(np.random.uniform() * max_offy) im = cv2.resize(im, (0, 0), fx=scale, fy=scale) im = im[offy: (offy + h), offx: (offx + w)] flip = np.random.uniform() > 0.5 if flip: im = cv2.flip(im, 1) return im, [scale, [offx, offy], flip]
def display_video_stream(self): r , frame = self.capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = self.faceCascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40), flags=cv2.cv.CV_HAAR_SCALE_IMAGE ) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) frame = cv2.cvtColor(frame, cv2.cv.CV_BGR2RGB) frame = cv2.flip(frame, 1) image = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888) self.imageLabel.setPixmap(QPixmap.fromImage(image))
def rotation_complete(self): rospy.loginfo("Hey a rotation is done.") self.completed_image = deepcopy(self.img) cv2.imwrite("lidar.png", self.completed_image) cv2.imshow("My image", self.completed_image) pictures = self.zarj.eyes.get_images() rgb = cv2.flip(pictures[0], -1) cv2.imwrite("lefteye.png", rgb) cv2.imshow("Left eye", rgb) cv2.waitKey(0) #for y in range(0, 99): # sys.stdout.write(str(y) + ": ") # for x in range(0, 99): # sys.stdout.write(str(self.img[x, y]) + ' ') # sys.stdout.write(str(y) + "\n") self.blank_image() self.start_rotation = self.rotation
def process_image(self, inImg): (self.frame_width, self.frame_height) = (112, 92) frame = cv2.flip(inImg,1,0) grayImg = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cropped = cv2.resize(grayImg, (grayImg.shape[1] / self.size, grayImg.shape[0] / self.size)) faces = self.haar_cascade.detectMultiScale(cropped) faces = sorted(faces, key=lambda x: x[3]) if faces: face_i = faces[0] x = face_i[0] * self.size y = face_i[1] * self.size w = face_i[2] * self.size h = face_i[3] * self.size face = grayImg[y:y + h, x:x + w] face_resize = cv2.resize(face, (self.frame_width, self.frame_height)) img_no = sorted([int(fn[:fn.find('.')]) for fn in os.listdir(self.path) if fn[0]!='.' ]+[0])[-1] + 1 if self.count % self.cp_rate == 0: cv2.imwrite('%s/%s.png' % (self.path, img_no), face_resize) print "Captured Img: ", self.count/self.cp_rate + 1 cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) cv2.putText(frame, self.face_name, (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN, 1,(0, 255, 0)) self.count += 1 return frame
def add_positive_sets(self, image_dir, pattern, annotation_path, sample_ratio=1.0, padding=5, augment=True, label=1): features_set = [] image_files = self._get_image_files(image_dir, pattern, sample_ratio) for image_file in image_files: image = cv2.imread(image_file) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image_id = utils.get_file_id(image_file) annotation_file = "{}/annotation_{}.mat".format(annotation_path, image_id) bb = file_io.FileMat().read(annotation_file)["box_coord"][0] roi = utils.crop_bb(image, bb, padding=padding, dst_size=self._patch_size) patches = (roi, cv2.flip(roi, 1)) if augment else (roi,) # Todo : augment modulization features = self._desc.describe(patches) features_set += features.tolist() labels = np.zeros((len(features_set), 1)) + label dataset = np.concatenate([labels, np.array(features_set)], axis=1) self._dataset += dataset.tolist()
def run(self): global lastFrame global lockFrame # This method runs in a separate thread while not self.terminated: # Wait for an image to be written to the stream if self.event.wait(1): try: # Read the image and save globally self.stream.seek(0) flippedArray = cv2.flip(self.stream.array, -1) # Flips X and Y retval, thisFrame = cv2.imencode('.jpg', flippedArray) del flippedArray lockFrame.acquire() lastFrame = thisFrame lockFrame.release() finally: # Reset the stream and event self.stream.seek(0) self.stream.truncate() self.event.clear() # Image capture thread
def display(data_starboard, data_port, img_name, time2show): data_starboard = cv2.flip(data_starboard, 1) img = np.concatenate((data_starboard, data_port), axis=1) plt.imshow(img, cmap='plasma', interpolation='bicubic') # fig, ax = plt.subplots() # ax.imshow(img, cmap='bone', interpolation='bicubic') # ax.grid(True) plt.show() # dx, dy = 100, 100 # grid_color = [0, 0, 0] # img[:, ::dy, :] = grid_color # img[::dx, :, :] = grid_color # # cv2.imshow(img_name, img) # cv2.waitKey(time2show) return img
def augmentDataset(in_folder='cropped', out_folder='augmented_dataset'): """ Data augmentation (horizontal mirror) :param in_folder: (str) :param out_folder: (str) """ images = [name for name in os.listdir(in_folder)] for idx, name in enumerate(images): r = name.split('.jpg')[0][-2:] # Retrieve the ROI name cx, cy = map(int, name.split('_')[0].split('-')) image_path = '{}/{}'.format(in_folder, images[idx]) image = cv2.imread(image_path) height, width, n_channels = image.shape horizontal_flip = cv2.flip(image, 1) cv2.imwrite('{}/{}-{}_{}-{}.jpg'.format(out_folder, cx, cy, idx, r), image) cv2.imwrite('{}/{}-{}_hori_{}-{}.jpg'.format(out_folder, width - cx, cy, idx, r), horizontal_flip)
def getNext_batch(self): paths = self.images[self.pointer:self.pointer+self.batch_size] labels = self.labels[self.pointer:self.pointer+self.batch_size] self.pointer += self.batch_size images = np.ndarray([self.batch_size,self.scale_size[0],self.scale_size[1],3]) for i in range(len(paths)): image = cv2.imread(paths[i]) #print ('file name is {}'.format(paths[i])) #cv2.imshow(paths[i],image) #cv2.waitKey(0) if self.horizontal and np.random.random()<0.5: image = cv2.flip(image,1) image = cv2.resize(image,(self.scale_size[0],self.scale_size[1])) image = image.astype(np.float32) image -= self.mean images[i] = image one_hot_labels = np.zeros((self.batch_size,self.n_class)) for i in range(len(labels)): one_hot_labels[i][int(labels[i])] = 1 return images,one_hot_labels
def data_augmentation(self, image_paths, labels, mode='train', resize=False, jitter=0.2, flip=False, whiten=False): new_images, new_labels = [], [] for image_path, label in zip(image_paths, labels): image = cv2.imread(image_path) # ?????? if resize: image, label = self.image_resize( image, label, jitter=jitter, mode=mode) # ???? if flip: image, label = self.image_flip(image, label, mode=mode) # ???? if whiten: image = self.image_whitening(image) new_images.append(image) new_labels.append(label) new_images = numpy.array(new_images, dtype='uint8') new_labels = numpy.array(new_labels, dtype='float32') return new_images, new_labels
def image_flip(self, image, label, mode='train'): # ???? if mode == 'train': old_image = image if numpy.random.random() < 0.5: new_image = cv2.flip(old_image, 1) else: new_image = old_image # ????box label for j in range(len(label)): if sum(label[j]) == 0: break right = 1.0 - label[j][0] left = 1.0 - label[j][1] label[j][0] = left label[j][1] = right else: new_image = image return new_image, label
def data_augmentation(self, images, labels, mode='train', flip=False, crop=False, padding=20, whiten=False, noise=False, noise_mean=0, noise_std=0.01, resize=False, jitter=0.2): # ?????? if resize: images, labels = self.image_resize(images, labels, jitter=jitter, mode=mode) # ???? if crop: images = self.image_crop(images, padding=padding) # ???? if flip: images, labels = self.image_flip(images, labels) # ???? if whiten: images = self.image_whitening(images) # ???? if noise: images = self.image_noise(images, mean=noise_mean, std=noise_std) return numpy.array(images, dtype='uint8'), numpy.array(labels, dtype='float32')
def image_flip(self, images, labels): # ???? for i in range(len(images)): old_image = images[i] if numpy.random.random() < 0.5: new_image = cv2.flip(old_image, 1) else: new_image = old_image images[i] = new_image # ????box label for i in range(len(labels)): for j in range(len(labels[i])): if sum(labels[i][j]) == 0: break right = 1.0 - labels[i][j][0] left = 1.0 - labels[i][j][1] labels[i][j][0] = left labels[i][j][1] = right return images, labels
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 get_test_aug(factor): if not factor or factor == 1: return [ [False, False, False]] elif factor == 4: # transpose, v-flip, h-flip return [ [False, False, False], [False, False, True], [False, True, False], [True, True, True]] elif factor == 8: # return list of all combinations of flips and transpose return ((1 & np.arange(0, 8)[:, np.newaxis] // 2**np.arange(2, -1, -1)) > 0).tolist() else: print('Invalid augmentation factor') return [ [False, False, False]]
def catchOwner(): global frame_num cap = cv2.VideoCapture(0) while(True): ret, img = cap.read() img = cv2.flip(img, 1) # flip the image???????? show_image = face_detector(img, face_cascade) cv2.imshow('image',show_image) k = cv2.waitKey(2) if k == ord('s'): cv2.imwrite('/Users/gushixin/Desktop/OwnerSensor/data/owner/catch%s.jpg' % frame_num,img) frame_num += 1 elif k == 27: break cap.release() cv2.destroyAllWindows() #??py??????????????????py??????????????
def shape_points(img, nsteps, mirrow=False, only_upper=False): """ Simple formatting the shape_df output to be passed to the ShapeContext class """ if mirrow: im = cv2.flip(img, 2) else: im = img.copy() df_y = shape_df(im, 'y', nsteps) df_x = shape_df(im, 'x', nsteps) if (not df_y.empty) and (not df_x.empty): y_init = [(df_y.init[i], df_y.coord[i]) for i in xrange(df_y.shape[0])] y_end = [(df_y.end[i], df_y.coord[i]) for i in xrange(df_y.shape[0])] x_init = [(df_x.coord[i], df_x.init[i]) for i in xrange(df_x.shape[0])] x_end = [(df_x.coord[i], df_x.end[i]) for i in xrange(df_x.shape[0])] if only_upper: return x_init return y_init+y_end+x_init+x_end else: return []
def setCameraProperties(): name_window = 'Press esc after finish' cv2.namedWindow(name_window) cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_SETTINGS, 0); ret, frame_from = cap.read() while(cap.isOpened()): ret, frame_from = cap.read() frame = cv2.flip(frame_from, -1) if ret==True: cv2.imshow(name_window,frame) if cv2.waitKey(1) & 0xFF == 27: break else: break # Release everything if job is finished cap.release() cv2.destroyAllWindows()
def getFrameFromCamera(): name_window = 'camera window' cv2.namedWindow(name_window) cap = cv2.VideoCapture(0) ret, frame_from = cap.read() output = [] while(cap.isOpened()): ret, frame = cap.read() frame = cv2.flip(frame, -1) if ret==True: cv2.imshow(name_window,frame) cur_key = cv2.waitKey(1) if cur_key == 27: break if cur_key == ord('s'): output = frame break else: break # Release everything if job is finished cap.release() #out.release() cv2.destroyAllWindows() return output
def augmentation(image, org_width=160,org_height=224, width=190, height=262): max_angle=20 image=resize(image,(width,height)) angle=np.random.randint(max_angle) if np.random.randint(2): angle=-angle image=rotate(image,angle,resize=True) xstart=np.random.randint(width-org_width) ystart=np.random.randint(height-org_height) image=image[xstart:xstart+org_width,ystart:ystart+org_height] if np.random.randint(2): image=cv2.flip(image,1) if np.random.randint(2): image=cv2.flip(image,0) # image=resize(image,(org_width,org_height)) print(image.shape) plt.imshow(image) plt.show()
def render(self, markers): for marker in markers: rvecs, tvecs, marker_rotation, marker_name = marker # build view matrix rmtx = cv2.Rodrigues(rvecs)[0] view_matrix = np.array([[rmtx[0][0],rmtx[0][1],rmtx[0][2],tvecs[0]], [rmtx[1][0],rmtx[1][1],rmtx[1][2],tvecs[1]], [rmtx[2][0],rmtx[2][1],rmtx[2][2],tvecs[2]], [0.0 ,0.0 ,0.0 ,1.0 ]]) view_matrix = view_matrix * self.INVERSE_MATRIX view_matrix = np.transpose(view_matrix) # load view matrix and draw cube glPushMatrix() glLoadMatrixd(view_matrix) if marker_name == MARKER_ONE: self.marker_one_textures[TEXTURE_FRONT] = cv2.flip(self._get_video_frame(), 0) self._draw_cube(marker_rotation, self.marker_one_textures) elif marker_name == MARKER_TWO: self._draw_cube(marker_rotation, self.marker_two_textures) glColor3f(1.0, 1.0, 1.0) glPopMatrix()
def crop_image(image, contours, min_aspect_ratio=0.5): ratio = image.shape[0] / float(scale_factor) warped = four_point_transform(image, contours.reshape(4, 2) * ratio) # test to see if the box ratio is correct height, width, channels = warped.shape if height > width: aspect_ratio = width / height else: aspect_ratio = height / width if aspect_ratio < min_aspect_ratio: raise ImageNotReadable() # test to see if the orientation is correct, if not flip it if height > width: warped = cv2.transpose(warped) warped = cv2.flip(warped, 0) return warped
def load_next_image(self,loss_task): if loss_task == 0: if self.cls_cur == len(self.cls_list): self.cls_cur = 0 random.shuffle(self.cls_list) cur_data = self.cls_list[self.cls_cur] # Get the image index im = cur_data[0] label = cur_data[1] roi = [-1,-1,-1,-1] pts = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1] if random.choice([0,1])==1: im = cv2.flip(im,random.choice([-1,0,1])) self.cls_cur += 1 return im, label, roi, pts if loss_task == 1: if self.roi_cur == len(self.roi_list): self.roi_cur = 0 random.shuffle(self.roi_list) cur_data = self.roi_list[self.roi_cur] # Get the image index im = cur_data[0] label = -1 roi = cur_data[2] pts = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1] self.roi_cur += 1 return im, label, roi, pts if loss_task == 2: if self.pts_cur == len(self.pts_list): self.pts_cur = 0 random.shuffle(self.pts_list) cur_data = self.pts_list[self.pts_cur] # Get the image index im = cur_data[0] label = -1 roi = [-1,-1,-1,-1] pts = cur_data[3] self.pts_cur += 1 return im, label, roi, pts ################################################################################ ######################Regression Loss Layer By Python########################### ################################################################################
def data_augmentation(self, images, mode='train', flip=False, crop=False, crop_shape=(24,24,3), whiten=False, noise=False, noise_mean=0, noise_std=0.01): # ???? if crop: if mode == 'train': images = self._image_crop(images, shape=crop_shape) elif mode == 'test': images = self._image_crop_test(images, shape=crop_shape) # ???? if flip: images = self._image_flip(images) # ???? if whiten: images = self._image_whitening(images) # ???? if noise: images = self._image_noise(images, mean=noise_mean, std=noise_std) return images
def random_flip_img(img, horizontal_chance=0, vertical_chance=0): import cv2 flip_horizontal = False if random.random() < horizontal_chance: flip_horizontal = True flip_vertical = False if random.random() < vertical_chance: flip_vertical = True if not flip_horizontal and not flip_vertical: return img flip_val = 1 if flip_vertical: flip_val = -1 if flip_horizontal else 0 if not isinstance(img, list): res = cv2.flip(img, flip_val) # 0 = X axis, 1 = Y axis, -1 = both else: res = [] for img_item in img: img_flip = cv2.flip(img_item, flip_val) res.append(img_flip) return res
def __init__(self, horiz=False, vert=False, prob=0.5): """ Only one of horiz, vert can be set. :param horiz: whether or not apply horizontal flip. :param vert: whether or not apply vertical flip. :param prob: probability of flip. """ super(Flip, self).__init__() if horiz and vert: raise ValueError("Please use two Flip instead.") elif horiz: self.code = 1 elif vert: self.code = 0 else: raise ValueError("Are you kidding?") self.prob = prob self._init()