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()