[참고]

1. USB 카메라 영상(컬러) 

#UBS 카메라 실시간 영상 스트리밍
#Windows10, PC 사용

import numpy as np 
import cv2

img = cv2.VideoCapture(0)

while True:
    ret, frame = img.read()
    
    k = cv2.waitKey(3) & 0xFF

    if k == 27:
        break

    cv2.imshow('frame', frame)

img.release()
cv2.destroyAllWindows()

 

2. USB 카메라 영상(흑백)

import numpy as np 
import cv2

img = cv2.VideoCapture(0)

while True:
    ret, frame = img.read()
    
    gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    k = cv2.waitKey(3) & 0xFF

    if k == 27:
        break

    # cv2.imshow('frame', frame)
    cv2.imshow('gray_img', gray_img)

img.release()
cv2.destroyAllWindows()

 

3. THRESHOLD

import numpy as np 
import cv2

img = cv2.VideoCapture(0)

while True:
    ret, frame = img.read()
    
    gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    ret, thr1 = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY)
    ret, thr2 = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY_INV)
    ret, thr3 = cv2.threshold(gray_img, 127, 255, cv2.THRESH_TRUNC)
    ret, thr4 = cv2.threshold(gray_img, 127, 255, cv2.THRESH_TOZERO)
    ret, thr5 = cv2.threshold(gray_img, 127, 255, cv2.THRESH_TOZERO_INV)
    
    k = cv2.waitKey(3) & 0xFF

    if k == 27:
        break

    # cv2.imshow('frame', frame)
    cv2.imshow('gray_img', gray_img)
    cv2.imshow('BINARY', thr1)
    cv2.imshow('BINARY_INV', thr2)
    cv2.imshow('TRUNC', thr3)
    cv2.imshow('TOZERO', thr4)
    cv2.imshow('TOZERO_INV', thr5)

img.release()
cv2.destroyAllWindows()

[결과]

+ Recent posts