02_image_basics.py

Download
python 222 lines 5.6 KB
  1"""
  202. ์ด๋ฏธ์ง€ ๊ธฐ์ดˆ ์—ฐ์‚ฐ
  3- imread, imshow, imwrite
  4- ํ”ฝ์…€ ์ ‘๊ทผ ๋ฐ ์ˆ˜์ •
  5- ROI (Region of Interest)
  6- ์ด๋ฏธ์ง€ ๋ณต์‚ฌ ๋ฐ ์ฑ„๋„ ์กฐ์ž‘
  7"""
  8
  9import cv2
 10import numpy as np
 11
 12
 13def create_sample_image():
 14    """์ƒ˜ํ”Œ ์ด๋ฏธ์ง€ ์ƒ์„ฑ"""
 15    img = np.zeros((300, 400, 3), dtype=np.uint8)
 16
 17    # ์ปฌ๋Ÿฌ ์˜์—ญ
 18    img[0:100, 0:200] = [255, 0, 0]      # ํŒŒ๋ž‘
 19    img[0:100, 200:400] = [0, 255, 0]    # ์ดˆ๋ก
 20    img[100:200, 0:200] = [0, 0, 255]    # ๋นจ๊ฐ•
 21    img[100:200, 200:400] = [255, 255, 0] # ์ฒญ๋ก
 22    img[200:300, :] = [128, 128, 128]    # ํšŒ์ƒ‰
 23
 24    return img
 25
 26
 27def image_read_write_demo():
 28    """์ด๋ฏธ์ง€ ์ฝ๊ธฐ/์“ฐ๊ธฐ ๋ฐ๋ชจ"""
 29    print("=" * 50)
 30    print("์ด๋ฏธ์ง€ ์ฝ๊ธฐ/์“ฐ๊ธฐ")
 31    print("=" * 50)
 32
 33    # ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฐ ์ €์žฅ
 34    img = create_sample_image()
 35    cv2.imwrite('test_image.jpg', img)
 36    print("test_image.jpg ์ €์žฅ ์™„๋ฃŒ")
 37
 38    # ์ด๋ฏธ์ง€ ์ฝ๊ธฐ
 39    # cv2.IMREAD_COLOR: ์ปฌ๋Ÿฌ (๊ธฐ๋ณธ๊ฐ’)
 40    # cv2.IMREAD_GRAYSCALE: ๊ทธ๋ ˆ์ด์Šค์ผ€์ผ
 41    # cv2.IMREAD_UNCHANGED: ์›๋ณธ ๊ทธ๋Œ€๋กœ (์•ŒํŒŒ ์ฑ„๋„ ํฌํ•จ)
 42
 43    img_color = cv2.imread('test_image.jpg', cv2.IMREAD_COLOR)
 44    img_gray = cv2.imread('test_image.jpg', cv2.IMREAD_GRAYSCALE)
 45
 46    print(f"์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€ shape: {img_color.shape}")
 47    print(f"๊ทธ๋ ˆ์ด ์ด๋ฏธ์ง€ shape: {img_gray.shape}")
 48
 49    return img_color
 50
 51
 52def pixel_access_demo(img):
 53    """ํ”ฝ์…€ ์ ‘๊ทผ ๋ฐ๋ชจ"""
 54    print("\n" + "=" * 50)
 55    print("ํ”ฝ์…€ ์ ‘๊ทผ")
 56    print("=" * 50)
 57
 58    # ๋‹จ์ผ ํ”ฝ์…€ ์ ‘๊ทผ (y, x ์ˆœ์„œ ์ฃผ์˜!)
 59    pixel = img[50, 100]  # (y=50, x=100) ์œ„์น˜
 60    print(f"ํ”ฝ์…€ (50, 100) BGR ๊ฐ’: {pixel}")
 61
 62    # ๊ฐœ๋ณ„ ์ฑ„๋„ ์ ‘๊ทผ
 63    b = img[50, 100, 0]
 64    g = img[50, 100, 1]
 65    r = img[50, 100, 2]
 66    print(f"B={b}, G={g}, R={r}")
 67
 68    # ํ”ฝ์…€ ์ˆ˜์ •
 69    img_copy = img.copy()
 70    img_copy[50, 100] = [0, 0, 0]  # ๊ฒ€์ •์œผ๋กœ ๋ณ€๊ฒฝ
 71
 72    # ์˜์—ญ ์ˆ˜์ • (๋” ํšจ์œจ์ )
 73    img_copy[0:50, 0:50] = [255, 255, 255]  # ํฐ์ƒ‰ ์‚ฌ๊ฐํ˜•
 74
 75    return img_copy
 76
 77
 78def roi_demo(img):
 79    """ROI (Region of Interest) ๋ฐ๋ชจ"""
 80    print("\n" + "=" * 50)
 81    print("ROI (Region of Interest)")
 82    print("=" * 50)
 83
 84    # ROI ์ถ”์ถœ (์Šฌ๋ผ์ด์‹ฑ)
 85    roi = img[50:150, 100:250]  # y: 50~150, x: 100~250
 86    print(f"์›๋ณธ shape: {img.shape}")
 87    print(f"ROI shape: {roi.shape}")
 88
 89    # ROI ๋ณต์‚ฌ (์›๋ณธ ์˜ํ–ฅ ์—†์Œ)
 90    roi_copy = img[50:150, 100:250].copy()
 91
 92    # ROI ๋ถ™์—ฌ๋„ฃ๊ธฐ
 93    img_with_roi = img.copy()
 94    img_with_roi[150:250, 200:350] = roi  # ๋‹ค๋ฅธ ์œ„์น˜์— ๋ถ™์—ฌ๋„ฃ๊ธฐ
 95
 96    cv2.imwrite('roi_demo.jpg', img_with_roi)
 97    print("roi_demo.jpg ์ €์žฅ ์™„๋ฃŒ")
 98
 99    return roi
100
101
102def channel_operations_demo(img):
103    """์ฑ„๋„ ์—ฐ์‚ฐ ๋ฐ๋ชจ"""
104    print("\n" + "=" * 50)
105    print("์ฑ„๋„ ์—ฐ์‚ฐ")
106    print("=" * 50)
107
108    # ์ฑ„๋„ ๋ถ„๋ฆฌ
109    b, g, r = cv2.split(img)
110    print(f"B ์ฑ„๋„ shape: {b.shape}")
111    print(f"G ์ฑ„๋„ shape: {g.shape}")
112    print(f"R ์ฑ„๋„ shape: {r.shape}")
113
114    # ์ฑ„๋„ ๋ณ‘ํ•ฉ
115    merged = cv2.merge([b, g, r])
116    print(f"๋ณ‘ํ•ฉ ํ›„ shape: {merged.shape}")
117
118    # ์ฑ„๋„ ์ˆœ์„œ ๋ณ€๊ฒฝ (BGR -> RGB)
119    rgb = cv2.merge([r, g, b])
120
121    # ๋‹จ์ผ ์ฑ„๋„๋งŒ ์‚ฌ์šฉํ•œ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
122    zeros = np.zeros_like(b)
123    only_blue = cv2.merge([b, zeros, zeros])
124    only_green = cv2.merge([zeros, g, zeros])
125    only_red = cv2.merge([zeros, zeros, r])
126
127    cv2.imwrite('only_blue.jpg', only_blue)
128    cv2.imwrite('only_green.jpg', only_green)
129    cv2.imwrite('only_red.jpg', only_red)
130    print("์ฑ„๋„ ๋ถ„๋ฆฌ ์ด๋ฏธ์ง€ ์ €์žฅ ์™„๋ฃŒ")
131
132
133def image_properties_demo(img):
134    """์ด๋ฏธ์ง€ ์†์„ฑ ๋ฐ๋ชจ"""
135    print("\n" + "=" * 50)
136    print("์ด๋ฏธ์ง€ ์†์„ฑ")
137    print("=" * 50)
138
139    # ๊ธฐ๋ณธ ์†์„ฑ
140    print(f"Shape (H, W, C): {img.shape}")
141    print(f"Height: {img.shape[0]}")
142    print(f"Width: {img.shape[1]}")
143    print(f"Channels: {img.shape[2] if len(img.shape) > 2 else 1}")
144
145    # ๋ฐ์ดํ„ฐ ํƒ€์ž…
146    print(f"Data type: {img.dtype}")
147
148    # ์ „์ฒด ํ”ฝ์…€ ์ˆ˜
149    print(f"Total pixels: {img.size}")
150
151    # ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰
152    print(f"Memory (bytes): {img.nbytes}")
153
154    # ํ”ฝ์…€ ๊ฐ’ ๋ฒ”์œ„
155    print(f"Min value: {img.min()}")
156    print(f"Max value: {img.max()}")
157    print(f"Mean value: {img.mean():.2f}")
158
159
160def image_arithmetic_demo():
161    """์ด๋ฏธ์ง€ ์‚ฐ์ˆ  ์—ฐ์‚ฐ ๋ฐ๋ชจ"""
162    print("\n" + "=" * 50)
163    print("์ด๋ฏธ์ง€ ์‚ฐ์ˆ  ์—ฐ์‚ฐ")
164    print("=" * 50)
165
166    # ๋‘ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
167    img1 = np.full((200, 200, 3), 100, dtype=np.uint8)
168    img2 = np.full((200, 200, 3), 200, dtype=np.uint8)
169
170    # NumPy ๋ง์…ˆ (์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋ฐœ์ƒ)
171    result_np = img1 + img2
172    print(f"NumPy ๋ง์…ˆ ๊ฒฐ๊ณผ (100+200): {result_np[0, 0]}")  # 44 (์˜ค๋ฒ„ํ”Œ๋กœ์šฐ)
173
174    # OpenCV ๋ง์…ˆ (ํฌํ™” ์—ฐ์‚ฐ, saturate)
175    result_cv = cv2.add(img1, img2)
176    print(f"OpenCV ๋ง์…ˆ ๊ฒฐ๊ณผ (100+200): {result_cv[0, 0]}")  # 255 (ํฌํ™”)
177
178    # ๊ฐ€์ค‘์น˜ ํ•ฉ (๋ธ”๋ Œ๋”ฉ)
179    alpha = 0.7
180    beta = 0.3
181    blended = cv2.addWeighted(img1, alpha, img2, beta, 0)
182    print(f"๋ธ”๋ Œ๋”ฉ ๊ฒฐ๊ณผ (0.7*100 + 0.3*200): {blended[0, 0]}")
183
184    # ๋บ„์…ˆ
185    diff = cv2.subtract(img2, img1)
186    print(f"๋บ„์…ˆ ๊ฒฐ๊ณผ (200-100): {diff[0, 0]}")
187
188    # ๋น„ํŠธ ์—ฐ์‚ฐ
189    mask = np.zeros((200, 200), dtype=np.uint8)
190    mask[50:150, 50:150] = 255
191
192    masked = cv2.bitwise_and(img1, img1, mask=mask)
193    cv2.imwrite('masked_result.jpg', masked)
194    print("masked_result.jpg ์ €์žฅ ์™„๋ฃŒ")
195
196
197def main():
198    """๋ฉ”์ธ ํ•จ์ˆ˜"""
199    # ์ด๋ฏธ์ง€ ์ฝ๊ธฐ/์“ฐ๊ธฐ
200    img = image_read_write_demo()
201
202    # ํ”ฝ์…€ ์ ‘๊ทผ
203    modified = pixel_access_demo(img)
204
205    # ROI
206    roi = roi_demo(img)
207
208    # ์ฑ„๋„ ์—ฐ์‚ฐ
209    channel_operations_demo(img)
210
211    # ์ด๋ฏธ์ง€ ์†์„ฑ
212    image_properties_demo(img)
213
214    # ์‚ฐ์ˆ  ์—ฐ์‚ฐ
215    image_arithmetic_demo()
216
217    print("\n์ด๋ฏธ์ง€ ๊ธฐ์ดˆ ์—ฐ์‚ฐ ์™„๋ฃŒ!")
218
219
220if __name__ == '__main__':
221    main()