-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate.py
93 lines (76 loc) · 3.23 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import numpy as np
import cv2 as cv
from os import path
dirname = path.dirname(path.abspath(__file__))
def writeImg(name, img):
cv.imwrite(path.join(dirname, name), img)
interpolations = [
[cv.INTER_NEAREST, 'nearest'],
[cv.INTER_LINEAR, 'bilinear'],
[cv.INTER_CUBIC, 'bicubic'],
]
img = cv.imread(path.join(dirname, 'test.png'))
assert img is not None, "file could not be read, check with os.path.exists()"
rows, cols = img.shape[0], img.shape[1]
# Image scaling by 10.
scale = 4
M = np.float32([[scale, 0, 0], [0, scale, 0]])
dst = cv.warpAffine(img, M, dsize=(cols * scale, rows * scale), flags=cv.INTER_LINEAR, borderMode=cv.BORDER_CONSTANT,
borderValue=0)
writeImg('testScale.png', dst)
# Image resizing.
sizes = [
[(80, 100), 'larger'],
[(8, 10), 'same'],
[(5, 6), 'smaller'],
]
for interpolation, interpolationName in interpolations:
for size, sizeName in sizes:
writeImg(
f'test_resize_{interpolationName}_{sizeName}.png',
cv.resize(img, size, interpolation=interpolation)
)
# Image rotate counter-clockwise by 90 degrees
M = np.float32([[0, 1, 0], [-1, 0, cols - 1]])
dst = cv.warpAffine(img, M, (rows, cols), flags=cv.INTER_LINEAR, borderMode=cv.BORDER_CONSTANT)
writeImg('testAntiClockwiseRot90.png', dst)
# Image rotate clockwise by 90 degrees
M = np.float32([[0, -1, cols + 1], [1, 0, 0]])
dst = cv.warpAffine(img, M, (rows, cols), flags=cv.INTER_LINEAR, borderMode=cv.BORDER_CONSTANT)
writeImg('testClockwiseRot90.png', dst)
# Image interpolation
matrix = cv.getRotationMatrix2D((2, 4), angle=30, scale=0.8)
dst = cv.warpAffine(img, matrix, dsize=(cols, rows), flags=cv.INTER_NEAREST, borderMode=cv.BORDER_REFLECT)
writeImg('testInterpolate.png', dst)
# Image bilinear interpolation
matrix = cv.getRotationMatrix2D((2, 4), angle=30, scale=1.4)
dst = cv.warpAffine(img, matrix, dsize=(cols, rows), flags=cv.INTER_LINEAR, borderMode=cv.BORDER_REFLECT)
writeImg('testRotateBilinear.png', dst)
# Image bicubic interpolation
matrix = cv.getRotationMatrix2D((2, 4), angle=30, scale=1.4)
dst = cv.warpAffine(img, matrix, dsize=(cols, rows), flags=cv.INTER_CUBIC, borderMode=cv.BORDER_REFLECT)
writeImg('testRotateBicubic.png', dst)
# Image reflection
M = np.float32([[1, 0, 0], [0, -1, rows - 1]])
dst = cv.warpAffine(img, M, (cols, rows), flags=cv.INTER_LINEAR, borderMode=cv.BORDER_CONSTANT)
writeImg('testReflect.png', dst)
# Image translation
M = np.float32([[1, 0, 2], [0, 1, 4]])
dst = cv.warpAffine(img, M, (16, 20))
writeImg('testTranslate.png', dst)
# Image affine transformation
M = np.float32([[2, 1, 2], [-1, 1, 2]])
dst = cv.warpAffine(img, M, (cols, rows), flags=cv.INTER_LINEAR, borderMode=cv.BORDER_CONSTANT)
writeImg('testAffineTransform.png', dst)
# Image blur
dst = cv.blur(img, (3, 5), borderType=cv.BORDER_REFLECT)
writeImg('testBlur.png', dst)
# Image gaussian blur
kernel = cv.getGaussianKernel(3, 1)
dst = cv.sepFilter2D(img, -1, kernel, kernel, borderType=cv.BORDER_REFLECT)
writeImg('testGaussianBlur.png', dst)
# Image convolution
kernelX = np.float32([[0.1, 0.2, 0.3]])
kernelY = np.float32([[0.4, 0.5, 0.6, -0.3, -0.4]])
dst = cv.sepFilter2D(img, ddepth=-1, kernelX=kernelX, kernelY=kernelY, borderType=cv.BORDER_REFLECT)
writeImg('testConvolution.png', dst)