Add code examples of thresholding images

This commit is contained in:
Jake Howard 2022-10-27 17:24:06 +01:00
parent cf5e5fbd33
commit c6aedc5cec
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 32 additions and 0 deletions

View file

@ -0,0 +1,18 @@
import cv2
import numpy
cap = cv2.VideoCapture(2)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
_, frame = cap.read()
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
t1 = cv2.adaptiveThreshold(grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 3, 7)
t2 = cv2.adaptiveThreshold(grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 13, 7)
t3 = cv2.adaptiveThreshold(grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 23, 7)
threshed = numpy.concatenate((t1, t2, t3), axis=1)
cv2.imshow('threshed', threshed)
cv2.waitKey(1)

14
examples/threshold.py Normal file
View file

@ -0,0 +1,14 @@
import cv2
cap = cv2.VideoCapture(2)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
_, frame = cap.read()
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(grey, 127, 255, cv2.THRESH_BINARY)
cv2.imshow('original', frame)
cv2.imshow('thresh', thresh)
cv2.waitKey(1)