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类transpose()的实例源码
def _grabImage(self):
w = self.display.widget
rval, img = self.vc.read()
if rval:
# COLOR
if self.pGrayscale.value():
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#img = cv2.transpose(img)
if self.pFloat.value():
img = toFloatArray(img)
i = w.image
b = self.pBuffer.value()
if b:
# BUFFER LAST N IMAGES
if i is None or len(i) < b:
self.display.addLayer(data=img)
else:
# TODO: implement as ring buffer using np.roll()
img = np.insert(i, 0, img, axis=0)
img = img[:self.pBuffer.value()]
w.setImage(img, autoRange=False, autoLevels=False)
else:
w.setImage(img, autoRange=False, autoLevels=False)
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 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 augment2(x):
x = x.astype(np.float32) / 255
u = 0.75
if random.random() < u:
if random.random() > 0.5:
x = randomDistort1(x, distort_limit=0.35, shift_limit=0.25, u=1)
else:
x = randomDistort2(x, num_steps=10, distort_limit=0.2, u=1)
x = randomShiftScaleRotate(x, shift_limit=0.0625, scale_limit=0.10, rotate_limit=45, u=1)
x = randomFlip(x, u=0.5)
x = randomTranspose(x, u=0.5)
x = randomContrast(x, limit=0.2, u=0.5)
# x = randomSaturation(x, limit=0.2, u=0.5),
x = randomFilter(x, limit=0.5, u=0.2)
x = 255.0 * x
x[:, :, 0] -= 124
x[:, :, 1] -= 117
x[:, :, 2] -= 104
x *= 0.0167
x = np.transpose(x, (2, 0, 1))
return x
def augment(x):
x = x.astype(np.float32) / 255
u = 0.75
if random.random() < u:
if random.random() > 0.5:
x = randomDistort1(x, distort_limit=0.35, shift_limit=0.25, u=1)
else:
x = randomDistort2(x, num_steps=10, distort_limit=0.2, u=1)
x = randomShiftScaleRotate(x, shift_limit=0.0625, scale_limit=0.10, rotate_limit=45, u=1)
x = randomFlip(x, u=0.5)
x = randomTranspose(x, u=0.5)
x = randomContrast(x, limit=0.2, u=0.5)
# x = randomSaturation(x, limit=0.2, u=0.5),
x = randomFilter(x, limit=0.5, u=0.2)
x = np.uint8(255.0 * np.transpose(x, (2, 0, 1)))
return x
# draw -----------------------------------
def tensor_to_img(img, mean=0, std=1, dtype=np.uint8):
img = img.numpy()
img = np.transpose(img, (1, 2, 0))
img = img * std + mean
img = img.astype(dtype)
# img = cv2.cvtColor(img , cv2.COLOR_BGR2RGB)
return img
## transform (input is numpy array, read in by cv2)
def img_to_tensor(img, mean=0, std=1.):
img = img.astype(np.float32)
img = (img - mean) / std
img = img.transpose((2, 0, 1))
tensor = torch.from_numpy(img) ##.float()
return tensor
## for debug
def randomTranspose(img, u=0.5):
if random.random() < u:
img = img.transpose(1, 0, 2) # cv2.transpose(img)
return img
# http://stackoverflow.com/questions/16265673/rotate-image-by-90-180-or-270-degrees
def cropped_image(image, contours, min_aspect_ratio=0.5, show=False):
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
original_height, original_width, original_channels = image.shape
if not (original_height > original_width) is (height > width):
warped = cv2.transpose(warped)
warped = cv2.flip(warped, 0)
if show: #this is for debugging puposes
cv2.imshow("Payload", warped)
cv2.waitKey(0)
cv2.destroyAllWindows()
return warped
# def test(canny, opt1, opt2, opt3, polydb, image_file):
# image = cv2.imread(image_file)
# if canny:
# # done_image = sharpen_image(image)
# done_image = filter_image(image, canny1=opt1, canny2=opt2, show=True)
# else:
# contrasted_image = contrast_image(image, thresh1=opt1, thresh2=opt2, show=True)
# done_image = merge_image_contours(contrasted_image, distance=opt3, show=True)
# contours = get_contours(done_image, polydb=polydb)
# overlay(image, contours, show=True)
# cropped_image(image, contours, min_aspect_ratio=0.5, show=True)
# test(True, 5, 5, 0, 0.03, 'images\\1.jpg')
# test(True, 5, 5, 0, 0.03, 'images\\2.jpg')
# test(True, 8, 8, 0, 0.1, 'images\\5.jpg')
# test(True, 5, 5, 0, 0.03, 'images\\6.jpg')
# test(True, 5, 5, 0, 0.03, 'images\\7.jpg')
# test(False, 180, 200, 50, 0.03, 'images\\8.jpg')
# test(True, 5, 5, 0, 0.03, 'images\\9.jpg')
# test(True, 5, 5, 0, 0.03, 'images\\D.jpg')