我正在尝试从视频中获取一张图像,并随机裁剪出其中的64x64x3块(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()
我想获取帧的一小部分(64x64x3)并将其保存为.jpg文件,因此我在处理最后注释的部分时遇到了问题。对于如何做这件事有什么建议吗?
谢谢!
发布于 2017-02-16 09:38:23
对于给定的c,r,width,height
img = img[r:r+height,c:c+width]
将从所需宽度的列c和所需高度的行r中获取数据块。
发布于 2019-03-30 23:14:04
要获得图像的随机裁剪,您只需对位置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)
https://stackoverflow.com/questions/42263020
复制