This commit is contained in:
NikolajDanger
2022-09-19 13:51:56 +02:00
parent 280de12b3d
commit cea460354e
8 changed files with 368 additions and 0 deletions

View File

@ -0,0 +1,34 @@
# This script shows how to open a camera in OpenCV and grab frames and show these.
# Kim S. Pedersen, 2022
import cv2 # Import the OpenCV library
print("OpenCV version = " + cv2.__version__)
# Open a camera device for capturing
cam = cv2.VideoCapture(0)
if not cam.isOpened(): # Error
print("Could not open camera")
exit(-1)
# Open a window
WIN_RF = "Example 1"
cv2.namedWindow(WIN_RF)
cv2.moveWindow(WIN_RF, 100, 100)
while cv2.waitKey(4) == -1: # Wait for a key pressed event
retval, frameReference = cam.read() # Read frame
if not retval: # Error
print(" < < < Game over! > > > ")
exit(-1)
# Show frames
cv2.imshow(WIN_RF, frameReference)
# Finished successfully

View File

@ -0,0 +1,72 @@
# This script shows how to do simple processing on frames comming from a camera in OpenCV.
# Kim S. Pedersen, 2015
import cv2 # Import the OpenCV library
import numpy as np # We also need numpy
from pkg_resources import parse_version
OPCV3 = parse_version(cv2.__version__) >= parse_version('3')
def capPropId(prop):
"""returns OpenCV VideoCapture property id given, e.g., "FPS
This is needed because of differences in the Python interface in OpenCV 2.4 and 3.0
"""
return getattr(cv2 if OPCV3 else cv2.cv, ("" if OPCV3 else "CV_") + "CAP_PROP_" + prop)
print("OpenCV version = " + cv2.__version__)
# Define some constants
lowThreshold = 35
ratio = 3
kernel_size = 3
# Open a camera device for capturing
cam = cv2.VideoCapture(0)
if not cam.isOpened(): # Error
print("Could not open camera")
exit(-1)
# Get camera properties
width = int(cam.get(capPropId("FRAME_WIDTH")))
height = int(cam.get(capPropId("FRAME_HEIGHT")))
print("width = " + str(width) + ", Height = " + str(height))
# Open a window
WIN_RF = "Example 2"
cv2.namedWindow(WIN_RF)
cv2.moveWindow(WIN_RF, 100, 100)
# Preallocate memory
#gray_frame = np.zeros((height, width), dtype=np.uint8)
while cv2.waitKey(4) == -1: # Wait for a key pressed event
retval, frameReference = cam.read() # Read frame
if not retval: # Error
print(" < < < Game over! > > > ")
exit(-1)
# Convert the image to grayscale
gray_frame = cv2.cvtColor( frameReference, cv2.COLOR_BGR2GRAY )
# Reduce noise with a kernel 3x3
edge_frame = cv2.blur( gray_frame, (3,3) )
# Canny detector
cv2.Canny( edge_frame, lowThreshold, lowThreshold*ratio, edge_frame, kernel_size )
# Show frames
cv2.imshow(WIN_RF, edge_frame)
# Close all windows
cv2.destroyAllWindows()
# Finished successfully