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
python类flip()的实例源码
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
BoundaryExtraction.py 文件源码
项目:SummerProject_MacularDegenerationDetection
作者: WDongYuan
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
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
BoundaryExtraction.py 文件源码
项目:SummerProject_MacularDegenerationDetection
作者: WDongYuan
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
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 _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 _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 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 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 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 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 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, 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