1"""
204. ๊ธฐํํ์ ๋ณํ
3- resize, rotate, flip
4- ์ดํ์ธ ๋ณํ (warpAffine)
5- ์๊ทผ ๋ณํ (warpPerspective)
6- ์ด๋, ํ์ , ์ค์ผ์ผ๋ง
7"""
8
9import cv2
10import numpy as np
11
12
13def create_test_image():
14 """ํ
์คํธ ์ด๋ฏธ์ง ์์ฑ"""
15 img = np.zeros((300, 400, 3), dtype=np.uint8)
16 img[:] = [200, 200, 200] # ๋ฐ์ ํ์ ๋ฐฐ๊ฒฝ
17
18 # ์ฌ๊ฐํ
19 cv2.rectangle(img, (50, 50), (150, 150), (0, 0, 255), -1)
20
21 # ํ
์คํธ
22 cv2.putText(img, 'OpenCV', (180, 100),
23 cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
24
25 # ์
26 cv2.circle(img, (300, 200), 50, (255, 0, 0), -1)
27
28 return img
29
30
31def resize_demo():
32 """ํฌ๊ธฐ ์กฐ์ ๋ฐ๋ชจ"""
33 print("=" * 50)
34 print("ํฌ๊ธฐ ์กฐ์ (resize)")
35 print("=" * 50)
36
37 img = create_test_image()
38 h, w = img.shape[:2]
39 print(f"์๋ณธ ํฌ๊ธฐ: {w}x{h}")
40
41 # ์ ๋ ํฌ๊ธฐ๋ก ์กฐ์
42 resized1 = cv2.resize(img, (200, 150)) # (width, height)
43 print(f"์ ๋ ํฌ๊ธฐ ์กฐ์ : {resized1.shape[1]}x{resized1.shape[0]}")
44
45 # ๋น์จ๋ก ์กฐ์
46 resized2 = cv2.resize(img, None, fx=0.5, fy=0.5)
47 print(f"50% ์ถ์: {resized2.shape[1]}x{resized2.shape[0]}")
48
49 resized3 = cv2.resize(img, None, fx=2, fy=2)
50 print(f"200% ํ๋: {resized3.shape[1]}x{resized3.shape[0]}")
51
52 # ๋ณด๊ฐ๋ฒ ๋น๊ต
53 print("\n๋ณด๊ฐ๋ฒ ์ข
๋ฅ:")
54 print(" cv2.INTER_NEAREST: ์ต๊ทผ์ ์ด์ (๋น ๋ฆ, ํ์ง ๋ฎ์)")
55 print(" cv2.INTER_LINEAR: ์์ ํ (๊ธฐ๋ณธ๊ฐ, ๊ท ํ)")
56 print(" cv2.INTER_CUBIC: ๋ฐ์ดํ๋น
(ํ์ง ์ข์, ๋๋ฆผ)")
57 print(" cv2.INTER_AREA: ์์ญ (์ถ์์ ์ ํฉ)")
58
59 # ๊ฐ ๋ณด๊ฐ๋ฒ ์ ์ฉ
60 enlarged_nearest = cv2.resize(img, (800, 600), interpolation=cv2.INTER_NEAREST)
61 enlarged_linear = cv2.resize(img, (800, 600), interpolation=cv2.INTER_LINEAR)
62 enlarged_cubic = cv2.resize(img, (800, 600), interpolation=cv2.INTER_CUBIC)
63
64 cv2.imwrite('resize_half.jpg', resized2)
65 cv2.imwrite('resize_double.jpg', resized3)
66 cv2.imwrite('resize_nearest.jpg', enlarged_nearest)
67 cv2.imwrite('resize_cubic.jpg', enlarged_cubic)
68 print("\nํฌ๊ธฐ ์กฐ์ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ")
69
70
71def rotate_demo():
72 """ํ์ ๋ฐ๋ชจ"""
73 print("\n" + "=" * 50)
74 print("ํ์ (rotate)")
75 print("=" * 50)
76
77 img = create_test_image()
78
79 # ๊ฐ๋จํ 90๋ ํ์ (๋ด์ฅ ํจ์)
80 rotated_90 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
81 rotated_180 = cv2.rotate(img, cv2.ROTATE_180)
82 rotated_270 = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
83
84 print("๊ฐ๋จํ ํ์ :")
85 print(f" 90๋ ์๊ณ๋ฐฉํฅ: {rotated_90.shape[1]}x{rotated_90.shape[0]}")
86 print(f" 180๋: {rotated_180.shape[1]}x{rotated_180.shape[0]}")
87 print(f" 270๋(๋ฐ์๊ณ): {rotated_270.shape[1]}x{rotated_270.shape[0]}")
88
89 cv2.imwrite('rotate_90.jpg', rotated_90)
90 cv2.imwrite('rotate_180.jpg', rotated_180)
91 cv2.imwrite('rotate_270.jpg', rotated_270)
92
93 # ์์ ๊ฐ๋ ํ์ (getRotationMatrix2D ์ฌ์ฉ)
94 h, w = img.shape[:2]
95 center = (w // 2, h // 2)
96
97 # 45๋ ํ์ , ์ค์ผ์ผ 1.0
98 M = cv2.getRotationMatrix2D(center, 45, 1.0)
99 rotated_45 = cv2.warpAffine(img, M, (w, h))
100
101 # 30๋ ํ์ , ์ค์ผ์ผ 0.8
102 M = cv2.getRotationMatrix2D(center, 30, 0.8)
103 rotated_30_scaled = cv2.warpAffine(img, M, (w, h))
104
105 cv2.imwrite('rotate_45.jpg', rotated_45)
106 cv2.imwrite('rotate_30_scaled.jpg', rotated_30_scaled)
107 print("\nํ์ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ")
108
109
110def flip_demo():
111 """๋ค์ง๊ธฐ ๋ฐ๋ชจ"""
112 print("\n" + "=" * 50)
113 print("๋ค์ง๊ธฐ (flip)")
114 print("=" * 50)
115
116 img = create_test_image()
117
118 # ์ํ ๋ค์ง๊ธฐ (์ข์ฐ)
119 flipped_h = cv2.flip(img, 1)
120
121 # ์์ง ๋ค์ง๊ธฐ (์ํ)
122 flipped_v = cv2.flip(img, 0)
123
124 # ๋ ๋ค (์ํ์ข์ฐ)
125 flipped_both = cv2.flip(img, -1)
126
127 print("๋ค์ง๊ธฐ ์ฝ๋:")
128 print(" flip(img, 1): ์ํ (์ข์ฐ)")
129 print(" flip(img, 0): ์์ง (์ํ)")
130 print(" flip(img, -1): ์ํ์ข์ฐ")
131
132 cv2.imwrite('flip_horizontal.jpg', flipped_h)
133 cv2.imwrite('flip_vertical.jpg', flipped_v)
134 cv2.imwrite('flip_both.jpg', flipped_both)
135 print("\n๋ค์ง๊ธฐ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ")
136
137
138def translation_demo():
139 """์ด๋ ๋ฐ๋ชจ"""
140 print("\n" + "=" * 50)
141 print("์ด๋ (Translation)")
142 print("=" * 50)
143
144 img = create_test_image()
145 h, w = img.shape[:2]
146
147 # ์ด๋ ํ๋ ฌ: [[1, 0, tx], [0, 1, ty]]
148 # tx: x์ถ ์ด๋ (์์: ์ค๋ฅธ์ชฝ)
149 # ty: y์ถ ์ด๋ (์์: ์๋)
150
151 tx, ty = 50, 30
152 M = np.float32([[1, 0, tx], [0, 1, ty]])
153 translated = cv2.warpAffine(img, M, (w, h))
154
155 print(f"์ด๋: x={tx}, y={ty}")
156 print(f"์ด๋ ํ๋ ฌ:\n{M}")
157
158 cv2.imwrite('translated.jpg', translated)
159 print("\n์ด๋ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ")
160
161
162def affine_transform_demo():
163 """์ดํ์ธ ๋ณํ ๋ฐ๋ชจ"""
164 print("\n" + "=" * 50)
165 print("์ดํ์ธ ๋ณํ (Affine Transform)")
166 print("=" * 50)
167
168 img = create_test_image()
169 h, w = img.shape[:2]
170
171 # ์ดํ์ธ ๋ณํ: 3๊ฐ์ ์ ๋์
172 # ์๋ณธ ์ด๋ฏธ์ง์ 3์
173 pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
174 # ๋ณํ ํ 3์
175 pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
176
177 # ๋ณํ ํ๋ ฌ ๊ณ์ฐ
178 M = cv2.getAffineTransform(pts1, pts2)
179
180 # ๋ณํ ์ ์ฉ
181 affine = cv2.warpAffine(img, M, (w, h))
182
183 print("์ดํ์ธ ๋ณํ ํน์ฑ:")
184 print(" - ํํ์ ์ ํํ ์ ์ง")
185 print(" - 3์ ๋์์ผ๋ก ์ ์")
186 print(" - ์ด๋, ํ์ , ์ค์ผ์ผ, ์ ๋จ(shear) ์กฐํฉ")
187
188 cv2.imwrite('affine.jpg', affine)
189 print("\n์ดํ์ธ ๋ณํ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ")
190
191
192def perspective_transform_demo():
193 """์๊ทผ ๋ณํ ๋ฐ๋ชจ"""
194 print("\n" + "=" * 50)
195 print("์๊ทผ ๋ณํ (Perspective Transform)")
196 print("=" * 50)
197
198 img = create_test_image()
199 h, w = img.shape[:2]
200
201 # ์๊ทผ ๋ณํ: 4๊ฐ์ ์ ๋์
202 # ์๋ณธ ์ด๋ฏธ์ง์ 4์ (์ฌ๊ฐํ ๋ชจ์๋ฆฌ)
203 pts1 = np.float32([[0, 0], [w-1, 0], [0, h-1], [w-1, h-1]])
204
205 # ๋ณํ ํ 4์ (์๊ทผ๊ฐ ์ ์ฉ)
206 pts2 = np.float32([[50, 50], [w-50, 20], [30, h-30], [w-30, h-50]])
207
208 # ๋ณํ ํ๋ ฌ ๊ณ์ฐ
209 M = cv2.getPerspectiveTransform(pts1, pts2)
210
211 # ๋ณํ ์ ์ฉ
212 perspective = cv2.warpPerspective(img, M, (w, h))
213
214 print("์๊ทผ ๋ณํ ํน์ฑ:")
215 print(" - ํํ์ ์ด ํ ์ ์์ ๋ง๋จ (์๊ทผ๊ฐ)")
216 print(" - 4์ ๋์์ผ๋ก ์ ์")
217 print(" - ๋ฌธ์ ์ค์บ ๋ณด์ ์ ํ์ฉ")
218
219 cv2.imwrite('perspective.jpg', perspective)
220
221 # ์ญ๋ณํ (๋ฌธ์ ๋ณด์ ์๋ฎฌ๋ ์ด์
)
222 pts_doc = np.float32([[50, 50], [350, 30], [60, 280], [340, 270]])
223 pts_rect = np.float32([[0, 0], [300, 0], [0, 200], [300, 200]])
224
225 M_rect = cv2.getPerspectiveTransform(pts_doc, pts_rect)
226 rectified = cv2.warpPerspective(img, M_rect, (300, 200))
227
228 cv2.imwrite('perspective_rectified.jpg', rectified)
229 print("์๊ทผ ๋ณํ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ")
230
231
232def combined_transforms_demo():
233 """๋ณตํฉ ๋ณํ ๋ฐ๋ชจ"""
234 print("\n" + "=" * 50)
235 print("๋ณตํฉ ๋ณํ")
236 print("=" * 50)
237
238 img = create_test_image()
239 h, w = img.shape[:2]
240
241 # ์ด๋ -> ํ์ -> ์ค์ผ์ผ์ ํ ๋ฒ์
242 center = (w // 2, h // 2)
243 angle = 30
244 scale = 0.8
245
246 # ํ์ + ์ค์ผ์ผ ํ๋ ฌ
247 M = cv2.getRotationMatrix2D(center, angle, scale)
248
249 # ์ถ๊ฐ ์ด๋ ์ ์ฉ
250 M[0, 2] += 50 # x ์ด๋
251 M[1, 2] += 20 # y ์ด๋
252
253 result = cv2.warpAffine(img, M, (w, h))
254
255 print(f"๋ณตํฉ ๋ณํ: ํ์ {angle}๋, ์ค์ผ์ผ {scale}, ์ด๋ (50, 20)")
256
257 cv2.imwrite('combined_transform.jpg', result)
258 print("๋ณตํฉ ๋ณํ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ")
259
260
261def main():
262 """๋ฉ์ธ ํจ์"""
263 # ํฌ๊ธฐ ์กฐ์
264 resize_demo()
265
266 # ํ์
267 rotate_demo()
268
269 # ๋ค์ง๊ธฐ
270 flip_demo()
271
272 # ์ด๋
273 translation_demo()
274
275 # ์ดํ์ธ ๋ณํ
276 affine_transform_demo()
277
278 # ์๊ทผ ๋ณํ
279 perspective_transform_demo()
280
281 # ๋ณตํฉ ๋ณํ
282 combined_transforms_demo()
283
284 print("\n๊ธฐํํ์ ๋ณํ ๋ฐ๋ชจ ์๋ฃ!")
285
286
287if __name__ == '__main__':
288 main()