04_geometric_transforms.py

Download
python 289 lines 7.8 KB
  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()