作业12:
视频中随机生成边框,并且将这些边框对应的图像保存到本地。
参考链接:
https://www.cnblogs.com/subic/p/6937611.html
https://blog.csdn.net/u014630987/article/details/76713814
https://www.cnblogs.com/xiaodai0/p/9725159.html
1、均分宽高max
2、.jpg
3、
cv.imwrite('cut'+str(frames)+'.jpg', imCrop)存储每帧框内图片写循环外边
4、
imCrop = frame[right_y_down:left_y_up, left_x_up:right_x_down, :]#想让crop循环起来循环外边也得写一个
答案:
import cv2 as cv
import numpy as np
import random
frames= 0
capture = cv.VideoCapture(0) # 创建一个VideoCapture对象
result_video = "result.mp4"
r = None
while(True):
ret, frame = capture.read() # 一帧一帧读取视频
cv.imshow('frame', frame)#视频frame的第一帧用于画框
if frames == 0:#编辑第0帧内的东西目标框大小和显示框里的内容
left_x_up = random.randint(1, 340)
left_y_up = random.randint(1, 281)
right_x_down = left_x_up + random.randint(1, 300)
right_y_down = left_y_up - random.randint(1, 200)
left_x_up=min(left_x_up,right_x_down)
left_y_up=max(left_y_up,right_y_down)
right_x_down=max(left_x_up,right_x_down)
right_y_down=min(left_y_up,right_y_down)
#cv.rectangle(frame, (left_x_up, left_y_up), (right_x_down, right_y_down), (55, 255, 155), 5)
#r = cv.selectROI(frame)
# Crop image
imCrop = frame[right_y_down:left_y_up ,left_x_up:right_x_down,:]
cv.imshow('cut', imCrop) # 显示一下带框的结果(视频cut的第一帧(带框))
#cv.imwrite('cut' + str(frames) + '.jpg', imCrop)
#cv.imwrite('cut.jpg', imCrop)
frames += 1
left_x_up = random.randint(1, 340)
left_y_up = random.randint(1, 281)
right_x_down = left_x_up + random.randint(1, 300)
right_y_down = left_y_up - random.randint(1, 200)
left_x_up = min(left_x_up, right_x_down)
left_y_up = max(left_y_up, right_y_down)
right_x_down = max(left_x_up, right_x_down)
right_y_down = min(left_y_up, right_y_down)
cv.rectangle(frame,(left_x_up, left_y_up), (right_x_down, right_y_down) , (55, 255, 155), 5)
imCrop = frame[right_y_down:left_y_up, left_x_up:right_x_down, :]
#cv.rectangle(frame, r, (55, 255, 155), 5)
#cv.putText(frame, 'frame_%s' % frame, (word_x, word_y), cv.FONT_HERSHEY_SIMPLEX, 1, (55, 255, 155), 2)
cv.imshow('angle',frame) #带有目标框的视频
cv.imwrite('cut'+str(frames)+'.jpg', imCrop)#存储每帧框内图片,注意加上.jpg存储图片de
if cv.waitKey(1) &0xFF == ord('q'): # 按q停止
break
capture.release() # 释放cap,销毁窗口
cv.destroyAllWindows()