0
点赞
收藏
分享

微信扫一扫

6月18日,每日信息差

和谐幸福的人生 2023-06-19 阅读 36

文章目录

一、读取函数imread

cv::Mat cv::imread(const String & filename,int  flags=IMREAD_COLOR)

filename:需要读取图像的文件名称,包含图像地址、名称和图像文件扩展名
flags:读取图像形式的标志,如将彩色图像按照灰度图读取,默认参数是按照彩色图像格式读取,可

二、图片窗口函数namedWindow

void cv::namedWindow(const String & winname,int  flags = WINDOW_AUTOSIZE)

winname:窗口名称,用作窗口的标识符
flags:窗口属性设置标志

在这里插入图片描述

三、 图片保存Imwrite和显示函数Imshow

bool cv::imwrite	(	const String & 	filename,
	InputArray 	img,
	const std::vector< int > & 	params = std::vector< int >() 
)	

16 位无符号 CV 16U 图像可以保存成 PNG JPEG TIFF 式文件
32 位浮点 CV_32F 图像可以保存成 PFM TIFF OpenEXR Radiance HDR 格式文化
4 通道 (Alpha 通道 图像可以保存成 PNG 式文件.
在这里插入图片描述

void cv::imshow(const String & winname,InputArray mat)

四、视频数据的读取

在这里插入图片描述

cv :: VideoCapture :: VideoCapture(); //默认构造函数
cv :: VideoCapture :: VideoCapture(const String& filename,int apiPreference =CAP_ANY)
  • filename:读取的视频文件或者图像序列名称
  • apiPreference:读取数据时设置的属性,例如编码格式、是否调用OpenNI等,详细参数及含义在
#include<iostream>
//#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "opencv/highgui.h"

using namespace std;
using namespace cv;

int main(int argc,char** argv) {
    cout<<"OpenCv Version: "<<CV_VERSION<<endl;
    VideoCapture video("/home/wyh/Documents/C++demo/project_video.mp4");
    if(video.isOpened()){
        cout<<"视频中图像的宽度 = "<<video.get(CAP_PROP_FRAME_WIDTH)<<endl;
        cout<<"视频中图像的高度 = "<<video.get(CAP_PROP_FRAME_HEIGHT)<<endl;
        cout<<"视频帧率 = "<<video.get(CAP_PROP_FPS)<<endl;
        cout<<"视频的总帧数 = "<<video.get(CAP_PROP_FRAME_COUNT)<<endl;
    }
    else{
        cout<<"请确认视频文件名称是否正确"<<endl;
        return -1;
    }
    while(1){
        Mat frame;
        video>>frame;
        if(frame.empty()){
            break;;
        }
        imshow("video",frame);
        waitKey(1000/video.get(CAP_PROP_FPS));
    }
    waitKey();
    return 0;
}

五、摄像头直接调用

cv :: VideoCapture :: VideoCapture(int index,int apiPreference = CAP_ANY)
  • opencv常用读视频函数 cv2.VideoCapture 、cv2.VideoCapture.get 等,可以参考这里
  • opencv常用写视频函数 cv2.VideoWriter 等可以参考这里 ,其中视频格式可以参考这里
  • videoCapture.read() 是按帧读取视频,ret,frame 是获 .read() 方法的两个返回值。其中 ret
    是布尔值,如果读取帧是正确的则返回True,如果文件读取到结尾,它的返回值就为False。frame 就是每一帧的图像,是个三维矩阵。
  • 可以使用 cv2.putText 来添加文字,具体参数可以参考这里
//本例程将读取一个视频,并个视频加上流动水印后保存为一个新视频。
import cv2

#读取视频并获取视频帧率、分辨率、总帧数
VideoCapture = cv2.VideoCapture("VideoExample.mp4")
fps = VideoCapture.get(cv2.CAP_PROP_FPS)
size = (int(VideoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(VideoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
totalFrames = int(VideoCapture.get(7))

#创建新视频
ViideoWrite = cv2.VideoWriter("VideoWriterExample.avi",cv2.VideoWriter_fourcc('I','4','2','0'),
                              fps,size)

x=10 #水印坐标
y=10 #水印坐标
i=1
step_x=5
step_y=5

success,frame = VideoCapture.read() #读取视频第一帧
print("第"+str(i)+"帧, 共"+str(totalFrames)+"帧")
while success:
        cv2.waitKey(1)
        # 给图片添加水印
        cv2.putText(frame,'hello,opencv',(x,y),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),3)
        cv2.imshow("frame",frame)
        ViideoWrite.write(frame) #给新视频添加新帧

        # 水印坐标变化
        if(x>size[0]):step_x=-5
        if(x<0):step_x=5
        if(y>size[1]):step_y=-5
        if(y<0):step_y=5
        x += step_x
        y += step_y
        success,frame = VideoCapture.read()
        i += 1
        print("第"+str(i)+"帧, 共"+str(totalFrames)+"帧")

print ('Quitted!') #提示程序已停止
cv2.destroyAllWindows() #程序停止前关闭所有窗口
//本例程将读取并显示摄像头图像,在图像窗口按下按键“S”将开始录制摄像头视频,
//按下按键“X”将停止录制摄像头视频,按下按键“Q”将停止程序。
import cv2

#读取视频并获取视频帧率、分辨率
cameraCapture  = cv2.VideoCapture(0)
fps = cameraCapture .get(5)
size = (int(cameraCapture .get(cv2.CAP_PROP_FRAME_WIDTH)),int(cameraCapture .get(cv2.CAP_PROP_FRAME_HEIGHT)),)


#创建新视频
cameraWriter = cv2.VideoWriter("CameraExample.avi",cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)


x=10 #水印坐标
y=10 #水印坐标
i=1
step_x=5
step_y=5

#读取视频第一帧
success,frame = cameraCapture.read()


#提示停止方法
print ('Showing camera. Press key "Q" to quit.')
print ('Press key "S" to start recording.')
Quit=1 #是否继续运行标志位
Record=0 #录制视频标志位

while success and Quit:
    keycode=cv2.waitKey(1)
    if keycode & 0xFF == ord('q'): #如果按下“Q”键,停止运行标志位置1,跳出while循环,程序停止运行
        Quit = 0
    if keycode & 0xFF == ord('s'): #如果按下“S”键,开始录制摄像头视频
        Record = 1
    if keycode & 0xFF == ord('x'): #如果按下“X”键,停止录制摄像头视频
        Record = 0

    if Record:

        # 给图片添加水印
        cv2.putText(frame,'hello,opencv',(x,y),cv2.FONT_HERSHEY_SIMPLEX,3,(0,255,255),3)
        cameraWriter.write(frame)# 给新视频添加新帧
        # 水印坐标变化
        if x > size[0]:
            step_x = -5
        if x < 0:
            step_x = 5
        if y > size[1]:
            step_y = -5
        if y < 0:
            step_y = 5
        x += step_x
        y += step_y
        print("第" + str(i) + "帧,")
        i = i + 1
        print('Press key "X" to end recording.')
        print("\n\t")

    cv2.imshow('frame',frame)
    success,frame = cameraCapture.read() # 逐帧读取视频

if success == 0: #提示由于摄像头读取失败停止程序
    print ('Camera disconnect !')
print('Quitted!')  # 提示程序已停止
#释放摄像头
cameraCapture.release()
#程序停止前关闭所有窗口
cv2.destroyAllWindows()
举报

相关推荐

0 条评论