Created
July 3, 2020 11:25
-
-
Save jangsoopark/7056f9a74643a6d0c6fc05ffd6d43368 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scipy.ndimage as ndimage | |
import cv2 | |
import numpy as np | |
def generate_dct(n): | |
m = np.zeros((n, n)) | |
for i in range(n): | |
for j in range(n): | |
m[i, j] = np.sqrt(2 / n) * np.cos((np.pi / n) * (i + 0.5) * (j + 0.5)) | |
return m | |
def generate_high_pass_filter(n, dw): | |
v = [0] * dw + [1] * (n - dw) | |
return np.diagflat(v) | |
if __name__ == '__main__': | |
num_frame = 5 | |
dct = generate_dct(num_frame) | |
idct = np.transpose(dct) | |
high_pass_filter = generate_high_pass_filter(num_frame, 2) | |
w, h = 400, 256 | |
change_detection_filter = np.matmul(idct, np.matmul(high_pass_filter, dct)) | |
frame_vectors = [np.zeros((h, w)) for _ in range(num_frame)] | |
cam = cv2.VideoCapture('posVideo11.870.avi') | |
while cam.isOpened(): | |
ret, frame = cam.read() | |
if not ret: | |
break | |
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
frame = cv2.medianBlur(frame, 11) | |
sobel_x = cv2.Sobel(frame, cv2.CV_8U, 1, 0, ksize=3) | |
sobel_y = cv2.Sobel(frame, cv2.CV_8U, 0, 1, ksize=3) | |
sobel_x = cv2.convertScaleAbs(sobel_x) | |
sobel_y = cv2.convertScaleAbs(sobel_y) | |
edge = sobel_y + sobel_x | |
edge = 255 - edge | |
frame_vectors.append(edge.astype(np.float32)) | |
frame_vectors.pop(0) | |
frame_array = np.array(frame_vectors).transpose([1, 2, 0]) | |
# motion_array = np.matmul(frame_array, dct) | |
# motion_array = np.matmul(motion_array, high_pass_filter) | |
# motion_array = np.matmul(motion_array, idct) | |
motion_array = np.matmul(frame_array, change_detection_filter.transpose()) | |
cv2.imshow('gray', frame) | |
cv2.imshow('edge', edge) | |
cv2.imshow('edge2', frame_vectors[2] - frame_vectors[3]) | |
result = motion_array[:, :, 4] | |
# result = (result - result.min()) / (result.max() - result.min()) | |
# print(np.abs(motion_array[:, :, 4]).max(), edge.max()) | |
cv2.imshow('asdf', result) | |
frame_h = cv2.calcHist([frame], [0], None, [256], [0, 255]) | |
# frame_e = cv2.calcHist([edge], [0], None, [256], [0, 255]) | |
def draw_hist(f, c): | |
_h = np.zeros((300, 256, 3)) | |
cv2.normalize(f, f, 0, 255, cv2.NORM_MINMAX) | |
hist = np.int32(np.around(f)) | |
pts = np.column_stack((np.arange(256).reshape(256, 1), hist)) | |
cv2.polylines(_h, [pts], False, c) | |
_h = np.flipud(_h) | |
return _h | |
def draw_hist_vector(v): | |
_len = len(v) | |
_h = np.zeros((300, 256, 3)) | |
c = [(128, 0, 128), (256, 0, 128), (256, 128, 0), (0, 256, 128), (0, 0, 256)] | |
for i in range(_len): | |
f = cv2.calcHist([v[i].astype(np.uint8)], [0], None, [256], [0, 255]) | |
cv2.normalize(f, f, 0, 255, cv2.NORM_MINMAX) | |
hist = np.int32(np.around(f)) | |
pts = np.column_stack((np.arange(256).reshape(256, 1), hist)) | |
cv2.polylines(_h, [pts], False, c[i]) | |
_h = np.flipud(_h) | |
return _h | |
def draw_hist_array(v): | |
_len = v.shape[-1] | |
_h = np.zeros((300, 256, 3)) | |
c = [(128, 0, 128), (256, 0, 128), (256, 128, 0), (0, 256, 128), (0, 0, 256)] | |
for i in range(_len): | |
f = cv2.calcHist([v[:, :, i].astype(np.uint8)], [0], None, [256], [0, 255]) | |
cv2.normalize(f, f, 0, 255, cv2.NORM_MINMAX) | |
hist = np.int32(np.around(f)) | |
pts = np.column_stack((np.arange(256).reshape(256, 1), hist)) | |
cv2.polylines(_h, [pts], False, c[i]) | |
_h = np.flipud(_h) | |
return _h | |
cv2.imshow('framehist', draw_hist(frame_h, (0, 255, 0))) | |
# cv2.imshow('frameedge', draw_hist(frame_e, (0, 0, 255))) | |
cv2.imshow('v', draw_hist_vector(frame_vectors)) | |
cv2.imshow('a', draw_hist_array(motion_array)) | |
if cv2.waitKey(1) & 0xff == ord('q'): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment