EunSeong Lee Portfolio
Project
Avoid Bullets
Digital Clock
Subsubcategory 1
Subsubcategory 2
post-03
Study
AI Algorithm Structure
LAB
Arm Architecture
CMOS VLSI Design
LAB
THEORY
FPGA
LAB
THEORY
comment system
Home
markdown
Contact
Copyright ยฉ 2024 |
Yankos
Home
>
Study
>
AI Algorithm Structure
> LAB
Please wait ...
LAB
Basic Operation
๐ ํ์ต๋ชฉํ OpenCV(Open Source Computer Vision Library)๋ฅผ ํ์ฉํ์ฌ ์ด๋ฏธ์ง/๋น๋์ค ์ฒ๋ฆฌํ๊ธฐ 1. ์ด๋ฏธ์ง Read & Write ๐ ์ด๋ฏธ์ง ํ์ผ ์ค๋น import numpy as np import cv2 img = cv2.imread("image.jpg") cv2.namedWindow("image", cv2.WINDOW_NORMAL) print(img.shape) cv2.imshow("image", img) cv2.waitKey(0) cv2.imwrite("output.png", img) cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ 2. ์์ ์ฑ๋ ๋ถ๋ฆฌ์ ์๊ณต๊ฐ ๋ณํ ๐ ์ด๋ฏธ์ง ํ์ผ ์ค๋น import numpy as np import cv2 color = cv2.imread("strawberry.jpg", cv2.IMREAD_COLOR) print(color.shape) height, width, channels = color.shape cv2.imshow("Original Image", color) r,g,b = cv2.split(color) rgb_split = np.concatenate((r,g,b), axis=1) cv2.imshow("RGB Channels", rgb_split) hsv = cv2.cvtColor(color, cv2.COLOR_RGB2HSV) h,s,v = cv2.split(hsv) hsv_split = np.concatenate((h,s,v),axis=1) cv2.imshow("Split HSV", hsv_split) cv2.waitKey(0) cv2.imwrite("hsv2rgb_split.png", hsv_split) ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ 3. ์ด๋ฏธ์ง ์ผ๋ถ ์์ญ ์๋ฅด๊ธฐ, ํฌ๊ธฐ ๋ฐ๊พธ๊ธฐ, ํ์ ํ๊ธฐ ๐ ์ด๋ฏธ์ง ํ์ผ ์ค๋น import numpy as np import cv2 img = cv2.imread("image.jpg") print(img.shape) # 0,100 cropped = img[0:220, 185:385] #220, 200, 3 print(cropped.shape) resized = cv2.resize(cropped, (400,200)) print(resized.shape) rotated_90 = cv2.rotate(resized, cv2.ROTATE_90_CLOCKWISE) #rotated_180 = cv2.rotate(image, cv2.ROTATE_180) #rotated_270 = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE) cv2.imshow("Origin", img) cv2.imshow("Cropped image", cropped) cv2.imshow("Resized image", resized) cv2.imshow("Rotated 90 image", rotated_90) cv2.waitKey(0) cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ cropped resized rotated_90 4. ์๋ณธ ์์ ๋ฐ์ ์ํค๊ธฐ ๐ ์ด๋ฏธ์ง ํ์ผ ์ค๋น import numpy as np import cv2 src = cv2.imread("output.png", cv2.IMREAD_COLOR) dst = cv2.bitwise_not(src) cv2.imshow("src", src) cv2.imshow("dst", dst) cv2.waitKey() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ 5. ์๊ณ๊ฐ ๊ธฐ์ค์ผ๋ก ์ด์งํ์ํค๊ธฐ ๐ ์ด๋ฏธ์ง ํ์ผ ์ค๋น import cv2 src = cv2.imread("image.jpg", cv2.IMREAD_COLOR) gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) ret, dst = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY) cv2.imshow("dst", dst) cv2.waitKey() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ 6. ์ด๋ฏธ์ง ํ๋ฆฌ๊ฒ(๋ธ๋ฌ) ์ฒ๋ฆฌ ๐ ์ด๋ฏธ์ง ํ์ผ ์ค๋น import cv2 src = cv2.imread("image.jpg", cv2.IMREAD_COLOR) dst = cv2.blur(src, (9, 9), anchor=(-1, -1), borderType=cv2.BORDER_DEFAULT) cv2.imshow("dst", dst) cv2.waitKey() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ 7. ์ธ ๊ฐ์ง ๋ํ์ ์ธ ์ฃ์ง(๊ฒฝ๊ณ) ๊ฒ์ถ ์๊ณ ๋ฆฌ์ฆ ๐ ์ด๋ฏธ์ง ํ์ผ ์ค๋น import cv2 src = cv2.imread("image.jpg", cv2.IMREAD_COLOR) gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, 3) cv2.imshow("sobel", sobel) laplacian = cv2.Laplacian(gray, cv2.CV_8U, ksize=3) cv2.imshow("laplacian", laplacian) canny = cv2.Canny(gray, 100, 200) cv2.imshow("canny", canny) cv2.waitKey() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ sobel laplacian canny 8. ์ปฌ๋ฌ ์ด๋ฏธ์ง์ BGR(Blue, Green, Red) ์ฑ๋์ ๋ถ๋ฆฌ ํ ์ฑ๋ ์์๋ฅผ ๋ฐ๊ฟ์ ์ด๋ฏธ์ง ํฉ์น๊ธฐ ๐ ์ด๋ฏธ์ง ํ์ผ ์ค๋น import numpy as np import cv2 src = cv2.imread("image.jpg", cv2.IMREAD_COLOR) #b,g,r = cv2.split(src) b = src[:, :, 0] g = src[:, :, 1] r = src[:, :, 2] inverse = cv2.merge((r,g,b)) cv2.imshow("b", b) cv2.imshow("g", g) cv2.imshow("r", r) cv2.imshow("inverse", inverse) cv2.waitKey() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ B G R inverse 9. ์ปฌ๋ฌ ์ด๋ฏธ์ง์ BGR(Blue, Green, Red) ์ฑ๋์ ๋ถ๋ฆฌ ํ Red ์ฑ๋๋ง 0(๊ฒ์ ์) ๐ ์ด๋ฏธ์ง ํ์ผ ์ค๋น import numpy as np import cv2 src = cv2.imread("bgr.png", cv2.IMREAD_COLOR) b = src[:, :, 0] g = src[:, :, 1] r = src[:, :, 2] height, width, channel = src.shape zero = np.zeros((height, width, 1), dtype=np.uint8) bgz = cv2.merge((b, g, zero)) cv2.imshow("b", b) cv2.imshow("g", g) cv2.imshow("r", r) cv2.imshow("bgz", bgz) cv2.waitKey() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ B G R bgz 10. ๋์์์์ ์ํ๋ ์ฅ๋ฉด์ ์ด๋ฏธ์ง๋ก ์บก์ฒํ๊ธฐ ๐ ๋์์ ํ์ผ ์ค๋น import numpy as np import cv2 import os save_dir = "SON" os.makedirs(save_dir, exist_ok=True) cap = cv2.VideoCapture("output.mp4") img_idx = 1 while cap.isOpened(): ret, frame = cap.read() if not ret: cap.set(cv2.CAP_PROP_POS_FRAMES, 0) continue height, width = frame.shape[:2] small_frame = cv2.resize(frame, (width // 2, height // 2), interpolation=cv2.INTER_AREA) cv2.imshow("Frame", small_frame) key = cv2.waitKey(80) if key & 0xFF == ord('q'): break if key & 0xFF == ord('c'): while True: filename = f"{img_idx:03d}.jpg" filepath = os.path.join(save_dir, filename) if not os.path.exists(filepath): cv2.imwrite(filepath, small_frame) print(f"Saved {filepath}") img_idx += 1 break else: img_idx += 1 cap.release() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ 11. ๋ค์ํ OpenCV ๊ทธ๋ฆฌ๊ธฐ ํจ์ ์ฌ์ฉํด๋ณด๊ธฐ import numpy as np import cv2 cap = cv2.VideoCapture(5) circle_centers = [] def draw_circle(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: circle_centers.append((x, y)) cv2.namedWindow("Camera") cv2.setMouseCallback("Camera", draw_circle) topLeft = (50, 50) bottomRight = (300, 300) while cap.isOpened(): ret, frame = cap.read() if not ret: break cv2.line(frame, topLeft, bottomRight, (0, 255, 0), 3) cv2.rectangle(frame, [pt+30 for pt in topLeft], [pt-30 for pt in bottomRight], (255, 0, 255), 3) font = cv2.FONT_ITALIC cv2.putText(frame, 'me', [pt+40 for pt in bottomRight], font, 2, (255, 0, 255), 5) for center in circle_centers: cv2.circle(frame, center, 30, (255, 255, 0), 3) cv2.imshow("Camera", frame) key = cv2.waitKey(1) if key & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ 12. ์ค์๊ฐ ์นด๋ฉ๋ผ ์์ ์์ ๊ธ์ ์ถ๋ ฅํ๊ณ ํธ๋๋ฐ๋ก ๊ตต๊ธฐ, ํฌ๊ธฐ, ์์ ์กฐ์ ํ๊ธฐ import numpy as np import cv2 cap = cv2.VideoCapture(4) #initial topLeft = (100, 100) bold = 0 font_size = 1 r, g, b = 0, 255, 255 def on_bold_trackbar(value): global bold bold = value def on_fontsize_trackbar(value): global font_size font_size = max(1, value) def on_r(val): global r r = val def on_g(val): global g g = val def on_b(val): global b b = val cv2.namedWindow("Camera") cv2.createTrackbar("bold", "Camera", bold, 10, on_bold_trackbar) cv2.createTrackbar("font size", "Camera", font_size, 10, on_fontsize_trackbar) cv2.createTrackbar('R', 'Camera', 0, 255, on_r) cv2.createTrackbar('G', 'Camera', 255, 255, on_g) cv2.createTrackbar('B', 'Camera', 255, 255, on_b) while cap.isOpened(): ret, frame = cap.read() if ret is False: print("Can't receive frame (stream end?). Exiting . . .") break # Text cv2.putText(frame, 'TEXT', topLeft, cv2.FONT_HERSHEY_SIMPLEX, font_size, (b, g, r), 1 + bold) # Display cv2.imshow("Camera", frame) key = cv2.waitKey(1) if key & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ 13. ์ค์๊ฐ ์นด๋ฉ๋ผ ์์ ์์ ํ๊ธ ์ถ๋ ฅํ๊ณ ํธ๋๋ฐ๋ก ๊ตต๊ธฐ, ํฌ๊ธฐ, ์์ ์กฐ์ ํ๊ธฐ import numpy as np import cv2 from PIL import ImageFont, ImageDraw, Image cap = cv2.VideoCapture(4) topLeft = (100, 100) bold = 0 font_size = 10 r, g, b = 0, 255, 255 def on_bold_trackbar(value): global bold bold = value def on_fontsize_trackbar(value): global font_size font_size = max(10, value * 5) def on_r(val): global r r = val def on_g(val): global g g = val def on_b(val): global b b = val cv2.namedWindow("Camera") cv2.createTrackbar("bold", "Camera", bold, 10, on_bold_trackbar) cv2.createTrackbar("font size", "Camera", font_size//5, 10, on_fontsize_trackbar) cv2.createTrackbar('R', 'Camera', 0, 255, on_r) cv2.createTrackbar('G', 'Camera', 255, 255, on_g) cv2.createTrackbar('B', 'Camera', 255, 255, on_b) font_path = "NanumGothic.ttf" while cap.isOpened(): ret, frame = cap.read() if not ret: print("Can't receive frame (stream end?). Exiting . . .") break img_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(img_pil) font = ImageFont.truetype(font_path, font_size) text = 'LEE ์์ฑ' # Bold ํจ๊ณผ: ์ฌ๋ฌ ๋ฒ ๊ฒน์ณ ๊ทธ๋ฆฌ๊ธฐ for dx in range(-bold, bold+1): for dy in range(-bold, bold+1): draw.text((topLeft[0]+dx, topLeft[1]+dy), text, font=font, fill=(r, g, b, 0)) frame = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR) cv2.imshow("Camera", frame) key = cv2.waitKey(1) if key & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ๐ก ์ถ๋ ฅ ๊ฒฐ๊ณผ
Study
ยท 2025-06-24
<
>
Touch background to close