视频分帧
#!/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()