사용하지않는공간/openCV

[openCV] Track Bar를 생성해 사용하는 법

반나무 2021. 5. 5. 18:24

트랙바를 사용하기 위해선 빈 함수라도 정의가 필요하다.

 

import cv2

# 트랙바를 사용하려면 비어 있는 함수라도 필요함.
def nothing(x):
    pass

cv2.namedWindow("Binary")
cv2.createTrackbar("threshold", "Binary", 0, 255, nothing)
cv2.setTrackbarPos("threshold", "Binary", 177)

img_color = cv2.imread('./binarization/dog3.jpg', cv2.IMREAD_COLOR)

#cv2.imshow('Color',img_color)
#cv2.waitKey(0)

img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)

#cv2.imshow('Gray', img_gray)
#cv2.waitKey(0)

while True:
    low = cv2.getTrackbarPos("threshold","Binary")
    # 사진 이진화 
    # threshold(그레이이미지,threshold(임계값),)
    ret,img_binary = cv2.threshold(img_gray, low, 255, cv2.THRESH_BINARY_INV)
    cv2.imshow("Binary", img_binary)

    img_result = cv2.bitwise_and(img_color,img_color,mask=img_binary)
    cv2.imshow("And", img_result)

    # esc 눌럿을때 종료
    if cv2.waitKey(1)&0xff == 27:
        break

cv2.destroyAllWindows()
반응형