Histogram of Oriented Gradients - Musterlösung

 1import os
 2import cv2
 3import numpy as np
 4
 5
 6def calculate_gradient_directions(totalDirections=12, offset=0.0):
 7    angles = np.linspace(0, 2.0 * np.pi, totalDirections, endpoint=False) + offset
 8    hogDirections = np.zeros((2, totalDirections))
 9
10    hogDirections[0, :] = np.cos(angles)
11    hogDirections[1, :] = np.sin(angles)
12
13    return hogDirections
14
15
16def hog_cell(magnitude, winningBin, totalDirections):
17    cellVector = np.zeros(totalDirections)
18    for index in range(totalDirections):
19        cellVector[index] = magnitude[winningBin == index].sum()
20
21    return cellVector / 256.0  # / np.linalg.norm(cellVector)
22
23
24def visualize_hog_cell(cellVector, directions, shape=(16, 16)):
25    cell = np.zeros(shape)
26    W, H = shape[0], shape[1]
27    for index, value in enumerate(cellVector):
28        dx, dy = directions[0, index], directions[1, index]
29
30        x0, y0 = int(W // 2), int(H // 2)
31        x1, y1 = int(x0 + dx * W / 2), int(y0 + dy * H / 2)
32        cv2.line(cell, (x0, y0), (x1, y1), (value, value, value))
33
34    return cell
35
36
37def process_hog(gray, totalDirections=12):
38    hogDirections = calculate_gradient_directions(totalDirections)
39
40    gx = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=5)
41    gy = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=5)
42    mag = np.sqrt(gx**2 + gy**2)
43
44    gx, gy = gx.flatten(), gy.flatten()
45    winningBin, bestDot = np.zeros_like(gx), np.zeros_like(gx)
46    for direction in range(totalDirections):
47        dot = gx * hogDirections[0, direction] + gy * hogDirections[1, direction]
48
49        greaterIndices = dot > bestDot
50        winningBin[greaterIndices] = direction
51        bestDot[greaterIndices] = dot[greaterIndices]
52
53    winningBin = winningBin.reshape(gray.shape)
54
55    binGray = np.uint8(255 * winningBin / (totalDirections - 1))
56    binColor = cv2.applyColorMap(binGray, cv2.COLORMAP_JET)
57    binColor[mag < 1.5] = (0, 0, 0)
58
59    cv2.imshow("Gradient Direction", binColor)
60
61    # cellSize = 8
62    # hogImage = np.zeros_like(gray)
63    # for x0 in range(0, gray.shape[1], cellSize):
64    #    for y0 in range(0, gray.shape[0], cellSize):
65    #       x1, y1 = x0 + cellSize, y0 + cellSize
66
67    #       cellVector = hog_cell(mag[y0:y1, x0:x1], winningBin[y0:y1, x0:x1], totalDirections)
68    #       img = visualize_hog_cell(cellVector, hogDirections, (cellSize, cellSize))
69
70    #       hogImage[y0:y1, x0:x1] = img
71
72    # cv2.imshow("HOG Vector", hogImage)
73
74
75if __name__ == "__main__":
76    cap = cv2.VideoCapture(0)
77    if not cap.isOpened():
78        print("Cannot open camera")
79        exit()
80
81    while True:
82        # Capture frame-by-frame
83        ret, frame = cap.read()
84
85        # if frame is read correctly ret is True
86        if not ret:
87            exit()
88
89        # Convert image to gray scale
90        gray = np.float32(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) / 255.0)
91
92        process_hog(gray)
93
94        # Display the resulting frame
95        if cv2.waitKey(1) == ord("q"):
96            break