π©π» IoT (Embedded)/Image Processing
[Python μΌλ‘ μμμ²λ¦¬ (2)] μ΄μ§ν
μ§μ§μνμΉ΄
2023. 12. 28. 10:43
728x90
λ°μν
π threshold λ‘ tone μ‘°μ νκΈ°
import numpy as np
def two_tone(img, threshold=128):
output = (img > threshold) * 255
return output.astype(np.uint8)
import cv2
img = cv2.imread("images/lena.jpg", 0)
# 100 μ΄νλ©΄ 0 black, 100 μ΄μμ΄λ©΄ 255 white
new_img = two_tone(img, threshold=100)
cv2.imwrite('lena_bin.jpg', new_img)
cv2.imshow('img', new_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
π threshold λ‘ μ€μκ° webcam tone μ‘°μ νκΈ° (BUT,, λλ μΊ‘μ²λ‘ λλ€)
import cv2
cap = cv2.VideoCapture(0)
if cap.isOpened():
print(cap.get(cv2.CAP_PROP_FPS))
delay = int(1000 / cap.get(cv2.CAP_PROP_FPS))
while True:
ret, img = cap.read()
if ret:
# 100 μ΄νλ©΄ 0 black, 100 μ΄μμ΄λ©΄ 255 white
new_img = two_tone(img, threshold=100)
cv2.imwrite('lena_bin.jpg', new_img)
cv2.imshow('img', new_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if cv2.waitKey(delay) & 0xFF == 27 : # ESCν€
print("ESC Key pressed")
break
else:
print("No Frame")
print(ret, img)
break
else:
print("File not opened")
728x90
λ°μν