作用是,素描的时候可以清晰地看出明暗对比。

opencv_threshold.png
import numpy as np
import cv2 as cv
# todo 以后再看看其他的滤镜,然后再添加进来,最终是有一个比较丰富的功能。
def resize_img(img):
return cv.resize(img, None, fx=0.6, fy=0.6, interpolation=cv.INTER_AREA)
def nothing(x):
pass
img = cv.imread("d.png", 1)
print(img.shape, img.dtype, img.size)
if img.shape[0] > 1000:
img = resize_img(img)
img2 = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.namedWindow('image')
# 用一个滑动条来控制 threshold 的大小
cv.createTrackbar("thresh", 'image', 0, 255, nothing)
while 1:
thresh = cv.getTrackbarPos('thresh', 'image')
# THRESH_BINARY, THRESH_BINARY_INV, THRESH_TOZERO, THRESH_TOZERO_INV
ret3, thresh3 = cv.threshold(img2, thresh, 255, cv.THRESH_BINARY)
cv.imshow("image", thresh3)
k = cv.waitKey(1) & 0xFF
if k == ord('q'): # press "Q" to quit.
# todo save img before close window
break
cv.destroyAllWindows()
网友评论