我正在尝试从视频中拍摄一张图像并裁剪掉一个随机的64 x 64 x 3块(64宽,64高,3为彩色通道)。
这是我到目前为止所拥有的:
def process_video(video_name):
# load video using cv2
video_cap = cv2.VideoCapture(video_name)
if video_cap.isOpened():
ret, frame = video_cap.read()
else:
ret = False
# while there's another frame
i = 0
while ret:
ret, frame = video_cap.read()
if i % 10 == 0:
# save several images from frame to local directory
i += 1
video_cap.release()
我想拍摄一小部分画面(64 x 64 x 3)并将其保存为.jpg文件,因此我在上一个评论部分遇到问题。有关如何解决此问题的任何建议吗?
谢谢!
答案 0 :(得分:2)
答案 1 :(得分:1)
要随机裁剪图像,您应该只采样x和y位置,然后按照@Max的说明选择矩阵的该部分:
import numpy as np
def get_random_crop(image, crop_height, crop_width):
max_x = image.shape[1] - crop_width
max_y = image.shape[0] - crop_height
x = np.random.randint(0, max_x)
y = np.random.randint(0, max_y)
crop = image[y: y + crop_height, x: x + crop_width]
return crop
example_image = np.random.randint(0, 256, (1024, 1024, 3))
random_crop = get_random_crop(example_image, 64, 64)