视频分帧,图像crop,拼接,帧合视频

视频分帧

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 26 17:29:15 2018

@author: jianyuchen
"""

import cv2  
video='/home/jianyuchen/caffe-master/data/ucf101/v_BrushingTeeth_g24_c05.avi'
vidcap = cv2.VideoCapture(video)  
success,image = vidcap.read()  
count = 0  
success = True  
savefile = '/home/jianyuchen/caffe-master/data/ucf101/frames/24little/'
while success:  
  success,image = vidcap.read()  
  res=cv2.resize(image,(160,120),interpolation=cv2.INTER_CUBIC) # resize
  res = cv2.copyMakeBorder(res,60,60,80,80,cv2.BORDER_CONSTANT,value=[255,255,255])#add white margin
  cv2.imwrite(savefile+"%d.jpg" % count, res)     # save frame as JPEG file  
  if cv2.waitKey(10) == 27:                       
      break  
  count += 1  

  #cv2.imshow('res',res)
  #cv2.waitKey(0)

图像crop:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 27 21:55:56 2018

@author: jianyuchen
"""

from PIL import Image
import cv2
import numpy

for i in range(217):
    imgfile = "/home/jianyuchen/caffe-master/data/ucf101/frames/24little/"+str(i)+".jpg"
    img=Image.open(imgfile)
    box = (110,90,180,160)
    roi = img.crop(box)
    roi = cv2.cvtColor(numpy.asarray(roi),cv2.COLOR_RGB2BGR) # Image covert to uint8
    roi=cv2.resize(roi,(140,140),interpolation=cv2.INTER_CUBIC) # resize
    roi = cv2.copyMakeBorder(roi,25,25,45,45,cv2.BORDER_CONSTANT,value=[255,255,255])#add white margin
    cv2.imwrite("/home/jianyuchen/caffe-master/data/ucf101/frames/24littlecrop/"+str(i)+".jpg",roi)

这里写图片描述

图像加白边

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 27 21:11:59 2018

@author: jianyuchen
"""

import cv2



# size = 1920, 1080  

img = cv2.imread('/home/jianyuchen/caffe-master/data/ucf101/frames/24little/0.jpg')  

a = cv2.copyMakeBorder(img,30,30,40,40,cv2.BORDER_CONSTANT,value=[255,255,255])


cv2.imshow('img',img)
cv2.waitKey(0)

cv2.imshow('a',a)
cv2.waitKey(0)

这里写图片描述
图片拼接

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 26 22:21:52 2018

@author: jianyuchen
"""

from PIL import Image
import matplotlib.pyplot as plt


for i in range(217):
    imgfile = "/home/jianyuchen/caffe-master/data/ucf101/frames/24littlecropcam/"+str(i)+".jpg"
    imgorifile = "/home/jianyuchen/caffe-master/data/ucf101/frames/24littlecrop/"+str(i)+".jpg"
    img=Image.open(imgfile)
    imgori = Image.open(imgorifile)
    imgori=imgori.resize((320,240)) #imgori resize
    box = (56,18,376,258)
    roi = img.crop(box)

    toImage = Image.new('RGBA',(320,480))

    fromImge = imgori
    # loc = ((i % 2) * 200, (int(i/2) * 200))
    loc = ((int(0/2) * 320), (0 % 2) * 240)
    print(loc)
    toImage.paste(fromImge, loc)


    fromImge = roi
    # loc = ((i % 2) * 200, (int(i/2) * 200))
    loc = ((int(1/2) * 320), (1 % 2) * 240)
    print(loc)
    toImage.paste(fromImge, loc)
    toImage.save("/home/jianyuchen/caffe-master/data/ucf101/frames/24littlecroppinjie/"+str(i)+".png")
'''
img=Image.open('/home/jianyuchen/caffe-master/data/ucf101/frames/14cam/0.jpg')  #打开图像
plt.figure("beauty")
plt.subplot(1,2,1), plt.title('origin')
plt.imshow(img),plt.axis('off')

box=(56,18,376,258)
roi=img.crop(box)
plt.subplot(1,2,2), plt.title('roi')
plt.imshow(roi),plt.axis('off')
plt.show()


toImage = Image.new('RGBA',(320,480))

fromImge = Image.open('/home/jianyuchen/caffe-master/data/ucf101/frames/14/frame0.jpg')
# loc = ((i % 2) * 200, (int(i/2) * 200))
loc = ((int(0/2) * 320), (0 % 2) * 240)
print(loc)
toImage.paste(fromImge, loc)


fromImge = roi
# loc = ((i % 2) * 200, (int(i/2) * 200))
loc = ((int(1/2) * 320), (1 % 2) * 240)
print(loc)
toImage.paste(fromImge, loc)

这里写图片描述

帧合视频

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 26 22:06:15 2018

@author: jianyuchen
"""

import cv2
import os
filenames = []
for filename in os.listdir(r"/home/jianyuchen/caffe-master/data/ucf101/frames/24little4pinjie"):
    filenames.append(int(filename.split('.')[0]))

filenames.sort()

fps = 5   #视频帧率
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')  
videofile='/home/jianyuchen/caffe-master/data/ucf101/frames/24little4.avi'
videoWriter = cv2.VideoWriter(videofile, fourcc, fps, (640,480))   #(1360,480)为视频大小
#for i in range(217):
for i in filenames:
    imgfile =  "/home/jianyuchen/caffe-master/data/ucf101/frames/24little4pinjie/"+str(i)+".png"
    img = cv2.imread(imgfile)
#    cv2.imshow('img', img12)
#    cv2.waitKey(1000/int(fps))
    videoWriter.write(img)
videoWriter.release()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值