Good Features To Track - Musterlösung

 1import cv2
 2import numpy as np
 3
 4
 5def processImage(frame):
 6    # GoodFeaturesToTrack works on a single channel image
 7    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 8
 9    # Detect strong, well separated corners
10    corners = cv2.goodFeaturesToTrack(
11        gray,
12        maxCorners=100,
13        qualityLevel=0.01,
14        minDistance=10,
15    )
16
17    # Draw the detected corners on top of the camera image
18    overlay = frame.copy()
19    if corners is not None:
20        corners = np.int32(corners)
21        for corner in corners:
22            x, y = corner.ravel()
23            cv2.circle(overlay, (x, y), 4, (0, 255, 0), -1)
24
25    return overlay
26
27
28def mainLoop():
29    """
30    The main loop of this program
31    """
32    # TODO: Open the default camera
33    cam = cv2.VideoCapture(0)
34
35    while True:
36        # TODO: Read next image from camera
37        _, frame = cam.read()
38
39        # TODO: Detect good features and draw them on the camera image
40        overlay = processImage(frame)
41
42        # TODO: Display the camera image with the detected features
43        cv2.imshow("Good Features To Track", overlay)
44
45        # TODO: Break the infinite loop when the users presses ESCAPE (27)
46        if cv2.waitKey(1) == 27:
47            break
48
49    # TODO: Release the capture and writer objects
50    cam.release()
51    cv2.destroyAllWindows()
52
53
54if __name__ == "__main__":
55    mainLoop()