基于Python & Opencv 图像-视频-处理算法

Alg1:图像数据格式之间相互转换.png to .jpg(其他的请举一反三)

 1 import cv2
 2 import glob
 3 
 4 def png2jpg():
 5         images = glob.glob('*.png') # 获取当前路径下的所有后缀名为.png的文件
 6         count = 0 # 不断累加,提供输出图像名称
 7         for i in images:
 8                 print('Picture %d is Processing...' % count)
 9                 I = cv2.imread(i) # 读取图像
10                 cv2.imwrite(str(count)+'.jpg',I) # 写入图像
11                 count += 1

将程序封装成脚本方便调用,将上述代码封装成.py文件,放到对应需要修改的文件夹即可完成所有图片的格式转换png2jpg.py

 Alg2:重新设置图像大小.py直接双击运行即可cv2.resize()

import cv2
import glob
images = glob.glob('*.png') # 获取当前路径下的所有后缀名为.png的文件
print(images)
zoom_coff = 0.8 # 系数,用来计算图像结果大小
for i in images:
    I = cv2.imread(i)
    w, h = I.shape[:2]
    print(w,h)
    Res = cv2.resize(I,(int(h*zoom_coff), int(w*zoom_coff)))
    cv2.imwrite(i,Res)

Alg2:读取MATLAB输出的.mat文件

from scipy.io import loadmat
path='C:\\Users\\miao.ma\\Desktop\\data\\Ref\\MATLAB_Calculate\\calibrationSession.mat'
mat = loadmat(path)        
print(mat)

Alg3:启动设备Camera进行视频捕获&按键监测捕获照片

 1 import cv2
 2 import time
 3 import numpy as np
 4 import threading
 5 
 6 cap = cv2.VideoCapture(0)
 7 while not cap.isOpened():
 8         time.sleep(100)
 9         print('Camera is Initialize...')
10 
11 width = int(cap.get(3))
12 height = int(cap.get(4))
13 
14 frame = np.zeros((width,height,3),dtype=np.uint8)
15 
16 Key_val = 0
17 process_flag = True
18 
19 def Keybo_Moni():
20         count = 0
21         while True:
22                 global Key_val, frame, process_flag, cap
23                 if Key_val == ord('r'):
24                         Key_val= 0
25                         cv2.imwrite('ResPic' + str(count) + '.jpg', frame)
26                         count += 1
27                         print('Get new pic %d' % count)
28                 if Key_val == ord('q'):
29                         cap.release()
30                         cv2.destroyAllWindows()
31                         print('Pic Sample Finished!')
32                         process_flag = False
33                         break
34 
35 try:
36 
37         Keybo_Moni_Thread = threading.Thread(target=Keybo_Moni, name='Keyboard-T                   hread')
38         Keybo_Moni_Thread.start()
39 except:
40         print('Error:uqnable to start the thread!')
41 
42 while process_flag:
43         ret, frame = cap.read()
44         while not ret:
45                 ret, frame = cap.read()
46                 print('Error: Camera wrong!')
47         cv2.imshow('Video_Show', frame)
48         Key_val = cv2.waitKey(1)
49 
50 Keybo_Moni_Thread.join()
View Code

 上述程序中,使用了多线程的方式,在按键监测和图像视频帧输出的同时,我们需要保证能够实时的监测到键盘,必须采用多线程,事实上单一线程也能完成相应的工作,但单一线程在例如算法处理等操作的时间上存在一定限制,处理时间过长将导致视频帧率的下降!所以采用多线程是个好的办法!采样和处理分开,再加上相应的缓冲就能很好的完成数据速率的匹配了.

Alg4:视频裁剪及保存 

 1 import os
 2 import sys
 3 import glob
 4 import time
 5 import getpass
 6 import cv2 as cv
 7 import numpy as np
 8 
 9 mswindows = (sys.platform == "win32")
10 linux = (sys.platform == "linux2" or sys.platform == "linux")
11 
12 Find_video_path = '*.mp4' # 这里决定了原始视频的基本格式,方便提取所有视频名称
13 video_dataset = glob.glob(Find_video_path) # 提取视频名称list
14 video_n = len(video_dataset) # 计算当前目录所有视频数量
15 roi_video_height = 200 # 设定输出视频的高度范围
16 
17 # Define the codec and create VideoWriter object
18 fourcc = cv.VideoWriter_fourcc(*'XVID') # 设置输出视频的压缩格式
19 
20 print('Starting Processing...')
21 for i in range(video_n):
22     video_name = video_dataset[i][0:] # 获取批处理文件的各个文件名称
23     print(video_name)
24     capture = cv.VideoCapture(video_name) # 打开一个视频
25     if not capture.isOpened(): # 异常处理
26         print('Unable to open: ' + video_name)
27         exit(0)
28 
29     video_fps = capture.get(cv.CAP_PROP_FPS) # 获取视频帧率
30     video_height = capture.get(cv.CAP_PROP_FRAME_HEIGHT) # 获取视频高度
31     video_width = capture.get(cv.CAP_PROP_FRAME_WIDTH) # 获取视频宽度
32     video_counts = capture.get(cv.CAP_PROP_FRAME_COUNT) # 获取视频帧数
33 
34     print('fps-height-width-counts:',video_fps,'-',video_height,'-',video_width,'-',video_counts) # 打印当前视频文件基本参数
35 
36     Sta = int(video_height/2 - roi_video_height/2) # 计算当前ROI起始值
37     End = int(video_height/2 + roi_video_height/2) # 计算当前ROI终止值
38 
39     if roi_video_height > video_height: # 判断视频能否进行指定参数的裁剪
40         print('video roi_video_height size Error!') # 输出异常
41         exit(0)
42 
43     roi_size = (int(video_width), int(roi_video_height)) # 得到ROI区域的大小
44     print('roi_size:',roi_size)
45 
46     # count = 0
47     roi_video_name = 'roi_'+video_name # 设置视频名称
48     out = cv.VideoWriter(roi_video_name, fourcc, video_fps, roi_size) # 设置视频输出操作句柄
49 
50     while capture.isOpened(): # 知道当前视频文件完全处理完成
51         ret, frame = capture.read() # 读取Capture句柄中的视频文件
52         if frame is None:
53             print("Can't receive frame (stream end?). Exiting ...")
54             break
55         # cv.imshow('Frame', frame)
56         # FrameSize = frame.size # 求解当前帧大小
57         # print(FrameSize)
58 
59         ROIFrame = frame[Sta:End, :] # 得到ROI区域截取结果
60         # image_name = image_name+str(count)+'.jpg' # 设置图片名称
61         # print(image_name)
62         # cv.imwrite(image_name, ball) # 保存当前帧ROI区域图片
63         # count = count + 1 # 帧计数
64 
65         out.write(ROIFrame) # 输出裁剪后的视频
66         
67         # time.sleep(0.5)
68         keyboard = cv.waitKey(30) # 终止按键 q
69         if keyboard == 'q' or keyboard == 27:
70             break
71 
72 capture.release() # 释放句柄
73 out.release() # 释放句柄
74 # cv.destroyAllWindows()
75 
76 print('Finished Processing...') # 打印终止信息
View Code

 功能是批处理将原始视频文件内容根据设定的图像尺寸大小进行裁剪,裁剪完成后保存成新的视频文件,帧率、尺寸、ROI区域、视频格式、压缩形式全可配置,亦可以将视频中的每一帧单独保存到指定文件夹中.

Alg5:人脸识别算法

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while True:
    ret,img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    cv2.imshow('img',img)       
    if cv2.waitKey(1) &0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

原理介绍:

Keep Going~

posted @ 2019-03-14 00:52  小淼博客  阅读(978)  评论(0编辑  收藏  举报

大家转载请注明出处!谢谢! 在这里要感谢GISPALAB实验室的各位老师和学长学姐的帮助!谢谢~