π OpenCVλ?
OpenCV(Open Source Computer Vision Library)λ μ€μκ° μ»΄ν¨ν° λΉμ λ° λ¨Έμ λ¬λμ μν μ€νμμ€ λΌμ΄λΈλ¬λ¦¬μ
λλ€.
λ€μν μ΄λ―Έμ§/λΉλμ€ μ²λ¦¬ κΈ°λ₯μ μ 곡νλ©°, Python, C++, Java λ± λ€μν μΈμ΄μμ μ¬μ© κ°λ₯ν©λλ€.
π CUDA λͺ¨λμ μν
- GPU κ°μμ νμ©ν κ³ μ μ΄λ―Έμ§ μ²λ¦¬ μν
- OpenCVμ μΌλΆ ν¨μλ€μ CUDAλ₯Ό ν΅ν΄ λ³λ ¬ μ²λ¦¬λμ΄ μ±λ₯μ ν₯μμν΄
- μ¬μ© μ:
cv2.cuda.GpuMat
,cv2.cuda.filter2D()
,cv2.cuda.resize()
λ±
π οΈ μμ ν λλ ν 리 μμ± λ° νκ²½ μ€μ
# 1. μμ
λλ ν 리 μμ±
mkdir opencv # λλ ν 리 μ΄λ¦: opencv
cd opencv # ν΄λΉ λλ ν λ¦¬λ‘ μ΄λ
# 2. κ°μ νκ²½ μμ± λ° νμ±ν
python3 -m venv .env # κ°μ νκ²½ μμ± (ν΄λ μ΄λ¦: .env)
source .env/bin/activate # κ°μ νκ²½ νμ±ν
# 3. ν¨ν€μ§ μ€μΉ
pip install opencv-python # OpenCV κΈ°λ³Έ κΈ°λ₯(core, imgproc λ±)
pip install opencv-contrib-python # μΆκ° λͺ¨λ(contrib ν¬ν¨)
pip install -U pip # pip μ΅μ λ²μ μΌλ‘ μ
κ·Έλ μ΄λ
β μ€μΉ νμΈ (Python μΈν°νλ¦¬ν° μ€ν)
>>> import numpy as np
>>> import cv2
>>> np.__version__
'2.2.6' # μ€μΉλ NumPy λ²μ μΆλ ₯
>>> cv2.__version__
'4.11.0' # μ€μΉλ OpenCV λ²μ μΆλ ₯
>>> exit() # Python μΈν°νλ¦¬ν° μ’
λ£
π¨ μμ μ 보
π μ°Έκ³ μ¬μ΄νΈ
π RGB (Red, Green, Blue)
- κ° μμ μ±λ: 0~255 (8bit)
- R (Red): 8bit
- G (Green): 8bit
- B (Blue): 8bit
- ν½μ 1κ° = 24bit (8bit Γ 3)
π¨ HSL (Hue, Saturation, Lightness)
- H: μμ (Hue) β 0 ~ 360Β°
- S: μ±λ (Saturation) β 0 ~ 100%
- L: λ°κΈ° (Lightness) β 0 ~ 100%
π RGB vs HSL μ°¨μ΄μ
νλͺ© | RGB | HSL |
---|---|---|
κ΅¬μ± | Red, Green, Blue (κ° 0~255) | Hue (0~360), Saturation & Lightness (0~100%) |
μ§κ΄μ± | μ»΄ν¨ν°μμ μ¬μ©νκΈ° μ ν© | μ¬λμ΄ μμ μ΄ν΄νκΈ° μ¬μ |
μ μ‘°μ | μμ μ‘°μ μ΄ λ³΅μ‘ν¨ | μ±λ/λ°κΈ° μ‘°μ μ΄ μ©μ΄ν¨ |
μ©λ | λμ€νλ μ΄, μ΄λ―Έμ§ μ²λ¦¬ λ± | λμμΈ, μμ μ ν λꡬ λ±μ μ μ© |
β μμ½:
- RGBλ νλ©΄ μΆλ ₯/μ²λ¦¬μ μ ν©ν λμ§νΈ μ νν λ°©μ
- HSLμ μμ ꡬμ±μμλ₯Ό λΆλ¦¬ν΄ μ¬λμ΄ μ΄ν΄νκ±°λ μ‘°μ νκΈ° μ¬μ΄ λ°©μ
π λ©λͺ¨
- vi ex1.py : python μ€ν¬λ¦½νΈ μμ±
- python ex1.py : μμ±λ μ€ν¬λ¦½νΈ μ€ν
- jpg : νμΌμ΄ μκ³ μλκ° λΉ λ₯΄λ©°, μ£Όλ‘ μ¬μ§μ΄λ μΉ λ°°κ²½ μ΄λ―Έμ§μ μ¬μ©
- png : νμ§ λ³΄μ‘΄, ν¬λͺ λ°°κ²½μ΄ νμν κ²½μ° μ¬μ©
π¨βπ» μ€μ΅
π‘ Code : μ΄λ―Έμ§ Read / Write / Display
# ex1.py
import numpy as np
import cv2
# μ΄λ―Έμ§ νμΌμ Read
img = cv2.imread("Rengoku.jpg")
# Image λ μ΄λ¦μ Display μ°½ μμ±
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
# Numpy ndarray H/W/C order
print(img.shape)
# Read ν μ΄λ―Έμ§ νμΌμ Display
cv2.imshow("image", img)
# λ³λ ν€ μ
λ ₯μ΄ μμλ κΉμ§ λκΈ°
cv2.waitKey(0)
# ex1_output.jpg λ‘ μ½μ μ΄λ―Έμ§ νμΌμ μ μ₯
cv2.imwrite("ex1_output.jpg", img)
# Destory all windows
cv2.destroyAllWindows()
β Quiz: μ΄λ―Έμ§ Read / Write / Display
1. print(img.shape)μ μΆλ ₯ κ²°κ³Όλ λ¬΄μ¨ μλ―ΈμΌκΉ?
2. λ³ΈμΈμ΄ μ’μνλ μ¬μ§μ web μμ λ€μ΄λ°μμ OpenCV APIλ₯Ό μ¬μ©ν΄μ Display λ° νμΌλ‘ μ μ₯ν΄λ³΄μ.
3. νμ¬λ λ³λμ ν€ μ
λ ₯μ΄ μμ λκΉμ§ cv2.waitKey(0) ν¨μμμ λκΈ°νκ² λλ€. μ½λλ₯Ό μΆκ°ν΄μ μλ¬Έμ βsβ ν€λ₯Ό μ
λ ₯λ°μ λλ§ μ΄λ―Έμ§ νμΌμ μ μ₯νκ³ λ€λ₯Έ ν€κ° μ
λ ₯λλ©΄ μ΄λ―Έμ§ νμΌμ μ μ₯νμ§ μκ² μμ ν΄λ³΄μ.
π‘ Code : RGB/HSV Color Space (μ 곡κ°)
# ex2.py
import numpy as np
import cv2
# μ΄λ―Έμ§ νμΌμ Read νκ³ Color space μ 보 μΆλ ₯
color = cv2.imread("Rengoku.jpg", cv2.IMREAD_COLOR)
print(color.shape)
height,width,channels = color.shape
cv2.imshow("Original Image", color)
# Color channel μ B,G,R λ‘ λΆν νμ¬ μΆλ ₯
b,g,r = cv2.split(color)
rgb_split = np.concatenate((b,g,r),axis=1)
cv2.imshow("BGR Channels",rgb_split)
# μ곡κ°μ BGR μμ HSV λ‘ λ³ν
hsv = cv2.cvtColor(color, cv2.COLOR_BGR2HSV)
# Channel μ H,S,V λ‘ λΆν νμ¬ μΆλ ₯
h,s,v = cv2.split(hsv)
hsv_split = np.concatenate((h,s,v),axis=1)
cv2.imshow("Split HSV", hsv_split)
β Quiz : RGB/HSV Color Space (μ 곡κ°)
1. μ μκ³΅κ° μ΄λ―Έμ§μ λ§ν¬λ‘ μ΄λν΄μ κ° μ 곡κ°μ νν λ°©λ²μ μ΄ν΄ν΄ 보μ.
2. HSV color spaceκ° μ΄λ€ κ²½μ°μ ν¨κ³Όμ μΌλ‘ μ¬μ©λ κΉ?
3. HSVλ‘ λ³νλ μ΄λ―Έμ§λ₯Ό BGRμ΄ μλ RGBλ‘ λ€μ λ³νν΄μ μΆλ ₯ν΄ λ³΄μ.
4. COLOR_RGB2GRAYλ₯Ό μ¬μ©ν΄μ νλ°±μΌλ‘ λ³νν΄ μΆλ ₯ν΄ λ³΄μ.
π‘ Code : Crop / Resize (μλ₯΄κΈ° / ν¬κΈ° μ‘°μ )
# ex3.py
import numpy as np
import cv2
# μ΄λ―Έμ§ νμΌμ Read
img = cv2.imread("Rengoku.jpg")
# Crop 300x400 from original image from (100, 50)=(x, y)
# μΈλ‘(y): 100:500 β 500 - 100 = 400ν½μ
# κ°λ‘(x): 500:1200 β 1200 - 500 = 700ν½μ
cropped = img[100:500, 500:1200]
# Resize cropped image from 300x400 to 400x200
resized = cv2.resize(cropped, (800,200))
# Display all
cv2.imshow("Original", img)
cv2.imshow("Cropped image", cropped)
cv2.imshow("Resized image", resized)
cv2.imwrite("ex3_cropped.jpg", cropped)
cv2.imwrite("ex3_resized.jpg", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
β Quiz : Crop / Resize (μλ₯΄κΈ° / ν¬κΈ° μ‘°μ )
1. Input image λ₯Ό λ³ΈμΈμ΄ μ’μνλ μΈλ¬Ό μ¬μ§μΌλ‘ λ³κ²½ν΄μ μ μ©νμ. κ·Έλ¦¬κ³ λ³ΈμΈμ΄ μ¬μ©ν input image μ size λ₯Ό νμΈν΄ 보μ.
2. λ³ΈμΈμ΄ μ¬μ©ν μ΄λ―Έμ§μ μΌκ΅΄ μμλ§ crop ν΄μ display ν΄ λ³΄μ.
3. μλ³Έ μ΄λ―Έμ§μ μ νν 1.5λ°°λ§νΌ μ΄λ―Έμ§λ₯Ό νλν΄μ νμΌλ‘ μ μ₯ν΄ λ³΄μ.
4. openCV μ rotate API λ₯Ό μ¬μ©ν΄μ μ°μΈ‘μΌλ‘ 90λλ§νΌ νμ λ μ΄λ―Έμ§λ₯Ό μΆλ ₯ν΄ λ³΄μ.
π‘ Code : μμ (Reverse Image)
# ex4.py
import numpy as np
import cv2
src = cv2.imread("Rengoku.jpg", cv2.IMREAD_COLOR)
dst = cv2.bitwise_not(src)
cv2.imshow("src", src)
cv2.imshow("dst", dst)
cv2.imwrite("ex4_reverse.jpg", dst)
cv2.waitKey()
cv2.destroyAllWindows()
β Quiz : μμ (Reverse Image)
1. AND, OR, XOR μ°μ°μ λν΄μ νμΈν΄ 보μ.
π‘ Code : μ΄μ§ν (Binary)
# ex5.py
import numpy as np
import cv2
src = cv2.imread("Rengoku.jpg", cv2.IMREAD_COLOR)
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
ret, dst = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
cv2.imshow("dst", dst)
cv2.imwrite("ex5_binary.jpg", dst)
cv2.waitKey()
cv2.destroyAllWindows()
β Quiz : μ΄μ§ν (Binary)
1. μκ³κ°μ λ³νμμΌ λ³΄μ.
π‘ Code : νλ¦Όν¨κ³Ό (Blur)
# ex6.py
import numpy as np
import cv2
src = cv2.imread("Rengoku.jpg", cv2.IMREAD_COLOR)
dst = cv2.blur(src, (9, 9), anchor=(-1,- 1), borderType=cv2.BORDER_DEFAULT)
cv2.imshow("dst", dst)
cv2.imwrite("ex6_blur.jpg", dst)
cv2.waitKey()
cv2.destroyAllWindows()
β Quiz : νλ¦Όν¨κ³Ό (Blur)
1. Kernel Sizeλ₯Ό λ³κ²½νμ¬ λ³΄μ.
2. borderTypeμ λ³κ²½νμ¬ λ³΄μ.(cv2.BORDER_REFLECT)
π‘ Code : κ°μ₯μ리 κ²μΆ (Edge)
# ex7.py
import numpy as np
import cv2
src = cv2.imread("Rengoku.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)
cv2.imwrite("ex7_edge.jpg", sobel)
cv2.waitKey()
cv2.destroyAllWindows()
β Quiz : κ°μ₯μ리 κ²μΆ (Edge)
1. Laplacian λ³νμ μ μ©ν΄ 보μ.
2. Canny Edge Detectionμ μ μ©ν΄ 보μ.
π‘ Code : λ°°μ΄ λ³ν© (add Weighted)
# ex8.py
import numpy as np
import cv2
src = cv2.imread("RGB.png", cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
# 1. Red λ§μ€ν¬ μμ±
lower_red = cv2.inRange(hsv, (0, 100, 100), (5, 255, 255))
upper_red = cv2.inRange(hsv, (170, 100, 100), (180, 255, 255))
mask_red = cv2.addWeighted(lower_red, 1.0, upper_red, 1.0, 0.0)
# 2. Green λ§μ€ν¬ μμ±
mask_green = cv2.inRange(hsv, (40, 100, 100), (85, 255, 255))
# 3. Blue λ§μ€ν¬ μμ±
mask_blue = cv2.inRange(hsv, (100, 100, 100), (130, 255, 255))
# 4. κ° μμ μΆμΆ (HSV β BGR λ³ν ν¬ν¨)
red = cv2.bitwise_and(hsv, hsv, mask=mask_red)
green = cv2.bitwise_and(hsv, hsv, mask=mask_green)
blue = cv2.bitwise_and(hsv, hsv, mask=mask_blue)
red = cv2.cvtColor(red, cv2.COLOR_HSV2BGR)
green = cv2.cvtColor(green, cv2.COLOR_HSV2BGR)
blue = cv2.cvtColor(blue, cv2.COLOR_HSV2BGR)
# 5. νλ©΄ μΆλ ₯
cv2.imshow("Original", src)
cv2.imshow("Red", red)
cv2.imshow("Green", green)
cv2.imshow("Blue", blue)
cv2.imwrite("ex8_original.png", src)
cv2.imwrite("ex8_red.png", red)
cv2.imwrite("ex8_green.png", green)
cv2.imwrite("ex8_blue.png", blue)
cv2.waitKey()
cv2.destroyAllWindows()
β Quiz : λ°°μ΄ λ³ν© (add Weighted)
1. lower_red κ°μ λ²μλ₯Ό λ³κ²½ν΄ 보μ.
2. upper_red κ°μ λ²μλ₯Ό λ³κ²½ν΄ 보μ.
3. addWeightedμ gamma κ°μ λ³κ²½ν΄ 보μ.
π‘ Code : μ±λ λΆλ¦¬ λ° λ³ν©
# ex9.py
import numpy as np
import cv2
# μ΄λ―Έμ§ μ½κΈ°
src = cv2.imread("RGB.png", cv2.IMREAD_COLOR)
# μ±λ λΆλ¦¬
b, g, r = cv2.split(src)
# μ±λ μμ λ³κ²½ (RGBμ²λΌ 보μ΄κ²)
inverse = cv2.merge((r, g, b))
# νλ©΄ μΆλ ₯
cv2.imshow("b", b)
cv2.imshow("g", g)
cv2.imshow("r", r)
cv2.imshow("inverse", inverse)
# μ΄λ―Έμ§ μ μ₯
cv2.imwrite("ex9_blue_gray.png", b)
cv2.imwrite("ex9_green_gray.png", g)
cv2.imwrite("ex9_red_gray.png", r)
cv2.imwrite("ex9_inverse.png", inverse)
cv2.waitKey()
cv2.destroyAllWindows()
β Quiz : μ±λ λΆλ¦¬ λ° λ³ν©
1. Numpy ννμ μ±λ λΆλ¦¬λ₯Ό μ μ©ν΄ 보μ.
b = src[:, :, 0]
g = src[:, :, 1]
r = src[:, :, 2]
2. λΉ μ΄λ―Έμ§λ₯Ό μ μ©ν΄ 보μ.
height, width, channel = src.shape
zero = np.zeros((height, width, 1), dtype=np.uint8)
bgz = cv2.merge((b, g, zero))
π‘ Code : λμμ νμΌμ μ½κ³ 보μ¬μ£ΌκΈ°
# ex10.py
import numpy as np
import cv2
cap = cv2.VideoCapture("son.mp4")
save_count = 1 # μ μ₯ν μ΄λ―Έμ§ λ²νΈ μ΄κΈ°ν
while cap.isOpened():
ret, frame = cap.read()
# (2) νλ μ μ½κΈ° μ€ν¨ β μμ λ β μ²μλΆν° λ€μ
if not ret:
print("Restarting video...")
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
# (3) νλ μ ν¬κΈ° 50% μΆμ
resized = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
# μΆλ ₯
cv2.imshow("Resized Frame", resized)
# (1) κ³ μ λ μλλ‘ μ¬μ (μ½ 30fps)
key = cv2.waitKey(90)
# (4) 'c' ν€ μ
λ ₯ μ μ΄λ―Έμ§ μ μ₯
if key & 0xFF == ord('c'):
filename = f"{save_count:03}.jpg"
cv2.imwrite(filename, resized)
print(f"Saved {filename}")
save_count += 1
# 'q' ν€ μ
λ ₯ μ μ’
λ£
if key & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
β Quiz : λμμ νμΌμ μ½κ³ 보μ¬μ£ΌκΈ°
1. λμμμ΄ λ무 λΉ λ₯΄κ² μ¬μλλ€. μ΄μ λ₯Ό μ°Ύμλ³΄κ³ μ μμ μΈ μλλ‘ μ¬μλ μ μλλ‘ μμ ν΄ λ³΄μ.
2. λμμμ΄ λκΉμ§ μ¬μλλ©΄ λ μ΄μ frameμ μ½μ§ λͺ»ν΄ μ’
λ£λλ€. λμμμ΄ λκΉμ§ μ¬μλλ©΄ λ€μ μ²μλΆν° λ°λ³΅λ μ μλλ‘ μμ ν΄ λ³΄μ.
3. λμμ ν¬κΈ°λ₯Ό λ°μΌλ‘ resizeν΄μ μΆλ ₯ν΄ λ³΄μ.
4. λμμ μ¬μ μ€ 'c' ν€ μ
λ ₯μ λ°μΌλ©΄ ν΄λΉ νλ μμ μ΄λ―Έμ§ νμΌλ‘ μ μ₯νλ μ½λλ₯Ό μμ±ν΄ 보μ. νμΌ μ΄λ¦μ 001.jpg, 002.jpg λ±μΌλ‘ overwrite λμ§ μκ² νμ.
π‘ Code : μΉ΄λ©λΌλ‘λΆν° input μ λ°μ 보μ¬μ£Όκ³ λμμ νμΌλ‘ μ μ₯νκΈ°
# ex11.py
import numpy as np
import cv2
# Read from the first camera device
cap = cv2.VideoCapture(0)
w = 640 #1280#1920
h = 480 #720#1080
cap.set(cv2.CAP_PROP_FRAME_WIDTH, w)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h)
# μ±κ³΅μ μΌλ‘ video device κ° μ΄λ ΈμΌλ©΄ while λ¬Έ λ°λ³΅
while(cap.isOpened()):
# ν νλ μμ μ½μ΄μ΄
ret, frame = cap.read()
if ret is False:
print("Can't receive frame (stream end?). Exiting ...")
break
# Display
cv2.imshow("Camera", frame)
# 1 ms λμ λκΈ°νλ©° ν€ μ
λ ₯μ λ°κ³ 'q' μ
λ ₯ μ μ’
λ£
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
β Quiz : μΉ΄λ©λΌλ‘λΆν° input μ λ°μ 보μ¬μ£Όκ³ λμμ νμΌλ‘ μ μ₯νκΈ°
1. κ°μ§κ³ μλ μΉ΄λ©λΌμ μ§μ κ°λ₯ν ν΄μλλ₯Ό νμΈ ν μΉ΄λ©λΌ ν΄μλλ₯Ό λ³κ²½ν΄ 보μ.
2. μΉ΄λ©λΌ Inputμ "output.mp4" λμμ νμΌλ‘ μ μ₯νλλ‘ μ½λλ₯Ό μΆκ°ν΄ 보μ.
π λ©λͺ¨
- sudo apt install v4l-utils : μΉ΄λ©λΌ μ§μ ν΄μλ νμΈμ© λꡬ μ€μΉ
- v4l2-ctl -d /dev/video0 βlist-formats-ext : ν΄λΉ μΉ΄λ©λΌμ ν΄μλ λ° ν¬λ§· λͺ©λ‘ μΆλ ₯
π‘ Code : Text / Line / Ractangle
# ex12.py
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# λκ·ΈλΌλ―Έλ₯Ό 그릴 μ’νλ₯Ό μ μ₯ν 리μ€νΈ
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+100, 50)
bottomRight = (300+100, 300)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Line
cv2.line(frame,
(topLeft[0] + 80, topLeft[1]),
(bottomRight[0] + 80, bottomRight[1]),
(0, 255, 0), 3)
# Rectangle
cv2.rectangle(frame,
[pt+80 for pt in topLeft], [pt+50 for pt in bottomRight], (255, 0, 255), 3)
# Text
font = cv2.FONT_ITALIC
cv2.putText(frame, 'hhhong',
[pt-180 for pt in bottomRight], font, 2, (0, 255, 255), 5)
# μ μ₯λ μ’νμ λκ·ΈλΌλ―Έ 그리기
for center in circle_centers:
cv2.circle(frame, center, 30, (255, 255, 0), 3) # λ°μ§λ¦ 30, λκ» 3, μμ (BGR)
cv2.imshow("Camera", frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
β Quiz : Text / Line / Ractangle
1. Text 문ꡬ / Font / μμ / ν¬κΈ° / κ΅΅κΈ° / μΆλ ₯μμΉ λ± λͺ¨λ κ°μ λ³κ²½ν΄ 보μ.
2. λκ·ΈλΌλ―Έλ₯Ό 그리λ ν¨μλ₯Ό μ°Ύμμ μ μ©ν΄ 보μ.
3. λ§μ°μ€ μΌμͺ½ λ²νΌμ click νλ©΄ ν΄λΉ μμΉμ λκ·ΈλΌλ―Έκ° κ·Έλ €μ§λλ‘ μ½λλ₯Ό μΆκ°ν΄ 보μ.
(Reference : cv2.EVENT_LBUTTONDOWN)
π‘ Code : Trackbar
# ex13.py
import cv2
topLeft = (50, 50)
bold = 0
r, g, b = 255, 255, 0 # μ΄κΈ° ν
μ€νΈ μμ: λ
Έλμ (BGR = (0, 255, 255))
# bold νΈλλ° μ½λ°±
def on_bold_trackbar(value):
global bold
bold = value
global r
r = value
def on_g_trackbar(value):
global g
g = value
def on_b_trackbar(value):
global b
b = value
# μΉ΄λ©λΌ μ°κ²°
cap = cv2.VideoCapture(0)
# μλμ° λ° νΈλλ° μμ±
cv2.namedWindow("Camera")
cv2.createTrackbar("bold", "Camera", bold, 30, on_bold_trackbar)
cv2.createTrackbar("R", "Camera", r, 255, on_r_trackbar)
cv2.createTrackbar("G", "Camera", g, 255, on_g_trackbar)
cv2.createTrackbar("B", "Camera", b, 255, on_b_trackbar)
# 루ν μμ
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# ν
μ€νΈ μΆλ ₯ (νΈλλ°μμ λ°μμ¨ bold, color κ° μ¬μ©)
cv2.putText(frame, "hhhong",
topLeft,
cv2.FONT_HERSHEY_SIMPLEX,
2,
(b, g, r), # BGR
1 + bold)
# νλ μ μΆλ ₯
cv2.imshow("Camera", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# μ’
λ£ μ²λ¦¬
cap.release()
cv2.destroyAllWindows()
β Quiz: Trackbar
1. Trackbarλ₯Ό controlν΄μ TEXTμ κ΅΅κΈ°κ° λ³νλ κ²μ νμΈν΄ 보μ.
2. Trackbarλ₯Ό μΆκ°ν΄μ font sizeλ₯Ό λ³κ²½ / μ μ©ν΄ 보μ.
3. R/G/B Trackbarλ₯Ό κ°κ° μΆκ°ν΄μ κΈμμ font colorλ₯Ό λ³κ²½ν΄ 보μ.