Last active
January 22, 2020 04:34
-
-
Save yagihiro/dc1990ec707b11a6aa8b5743072fae7f to your computer and use it in GitHub Desktop.
動画変換サンプルコード w/ opencv
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 numpy as np | |
import cv2 | |
import time | |
import sys | |
start_time = time.time() | |
input_path = sys.argv[1] | |
cap = cv2.VideoCapture(input_path) | |
# width / height / fps | |
W = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
H = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
print({'W': W, 'H': H, 'fps': fps}) | |
cap.set(cv2.CAP_PROP_POS_AVI_RATIO, 1) | |
duration_msec = cap.get(cv2.CAP_PROP_POS_MSEC) | |
pos_of_frames = cap.get(cv2.CAP_PROP_POS_FRAMES) | |
num_of_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) | |
cap.set(cv2.CAP_PROP_POS_AVI_RATIO, 0) | |
pos_of_frames2 = cap.get(cv2.CAP_PROP_POS_FRAMES) | |
print({'duration_msec': duration_msec, 'pos_of_frames': pos_of_frames, 'num_of_frames': num_of_frames, 'pos_of_frames2': pos_of_frames2}) | |
while cap.isOpened(): | |
ret, image = cap.read() | |
if not ret: | |
break | |
new_image = np.zeros(image.shape, image.dtype) | |
alpha = 1.2 # Simple contrast control(1.0-3.0) | |
beta = 50 # Simple brightness control(0-100) | |
new_image = cv2.convertScaleAbs(image, alpha = alpha, beta = beta) | |
# for y in range(image.shape[0]): | |
# for x in range(image.shape[1]): | |
# for c in range(image.shape[2]): | |
# new_image[y,x,c] = np.clip(alpha*image[y,x,c] + beta, 0, 255) | |
cv2.imshow('Original Image', image) | |
cv2.imshow('New Image', new_image) | |
if cv2.waitKey(int(1000/fps)) & 0xFF == ord('q'): | |
break | |
cv2.destroyAllWindows() | |
end_time = time.time() | |
print({'elapsed': (end_time - start_time)}) |
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 numpy as np | |
import cv2 | |
import time | |
import sys | |
start_time = time.time() | |
input_path = sys.argv[1] | |
cap = cv2.VideoCapture(input_path) | |
# width / height / fps | |
W = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
H = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
print({'W': W, 'H': H, 'fps': fps}) | |
cap.set(cv2.CAP_PROP_POS_AVI_RATIO, 1) | |
duration_msec = cap.get(cv2.CAP_PROP_POS_MSEC) | |
pos_of_frames = cap.get(cv2.CAP_PROP_POS_FRAMES) | |
num_of_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) | |
cap.set(cv2.CAP_PROP_POS_AVI_RATIO, 0) | |
pos_of_frames2 = cap.get(cv2.CAP_PROP_POS_FRAMES) | |
print({'duration_msec': duration_msec, 'pos_of_frames': pos_of_frames, 'num_of_frames': num_of_frames, 'pos_of_frames2': pos_of_frames2}) | |
# https://docs.opencv.org/master/d3/dc1/tutorial_basic_linear_transform.html | |
gamma = 0.3 | |
lookUpTable = np.empty((1,256), np.uint8) | |
for i in range(256): | |
lookUpTable[0,i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255) | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret: | |
break | |
res = cv2.LUT(frame, lookUpTable) | |
cv2.imshow('New video', res) | |
if cv2.waitKey(int(1000/fps)) & 0xFF == ord('q'): | |
break | |
cv2.destroyAllWindows() | |
end_time = time.time() | |
print({'elapsed': (end_time - start_time)}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment