vc=cv2.VideoCapture('E:/**/**/**/**/guzhang_333.MP4')
if vc.isOpened():
ret, frame = vc.read()
else:
ret = False
while ret:
#此处省略具体的操作
ret, frame = vc.read() # 读下一帧
vc.release()
def mk_video_writer(vc, path,frame_width, frame_height):
fps_video = vc.get(cv2.CAP_PROP_FPS) #帧率 ,依据需求更改此句
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video_writer = cv2.VideoWriter(path, fourcc, fps_video, (frame_width, frame_height))
return video_writer
#其他参数我省略了,记得在项目里灵活运用
frame_width = 112
frame_height = 112
video_writer = mk_video_writer(vc, after_video_path,frame_width, frame_height)
在视频的循环中,保存每一帧
video_writer.write(frame)
循环结束后,记得释放
video_writer.release()
image = cv2.imread('E:/migu/test/img1.png')
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
其实跟二维矩阵的切片一样
crop = frame[x1:x2,y1:y2]
img = cv2.resize(img,(width,height))
将原图片转化为宽和长分别为300,300的图片。width和height可以自己任意指定,不论大小。
resize(InputArray src, OutputArray dst, Size dsize,
double fx=0, double fy=0, int interpolation=INTER_LINEAR )
cv2.copyMakeBorder(src,top,bottom,left,right,borderType,value)
cv2.BORDER_CONSTANT:添加的边界框像素值为常数(需要额外再给定一个参数)
cv2.BORDER_REFLECT:添加的边框像素将是边界元素的镜面反射
cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT:和上面类似,但是有一些细微的不同
cv2.BORDER_REPLICATE:使用最边界的像素值代替
cv2.BORDER_WRAP:不知道怎么解释,直接看吧
value:如果borderType为cv2.BORDER_CONSTANT
时需要填充的常数值。
pad_img = cv2.copyMakeBorder(resize_crop, top_padding, bottom_padding, left_padding, right_padding, cv2.BORDER_CONSTANT, value=[0,0,0]) #从图像边界向上,下,左,右扩的像素数目
max_fps = vc.get(cv2.CAP_PROP_FPS)
frame_num = vc.get(cv2.CAP_PROP_FRAME_COUNT)
fourcc = int(vc.get(cv2CAP_PROP_FOURCC)) #视频的编码
width = int(vc.get(cv2.CAP_PROP_FRAME_WIDTH)) #获取原视频的宽
height = int(vc.get(cv2.CAP_PROP_FRAME_HEIGHT)) #获取原视频的搞