【OpenCV_05】把图像&视频写入文件Write Image & Video to File

今天,聊聊怎么吧图像以及视频写入到文件里。

Write Image to File 把图片写入到文件

下面的例子是创建一个黄色的图像并且写到文件里,下面就是采用OpenCV来实现.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Write Image & Video to File

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
  Mat img(650, 600, CV_16UC3, Scalar(0,50000, 50000)); //create an image ( 3 channels, 16 bit image depth, 650 high, 600 wide, (0, 50000, 50000) assigned for Blue, Green and Red plane respectively. )

if (img.empty()) //check whether the image is loaded or not
{
cout << "ERROR : Image cannot be loaded..!!" << endl;
          //system("pause"); //wait for a key press
return -1;
}

     vector<int> compression_params; //vector that stores the compression parameters of the image

     compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); //specify the compression technique

     compression_params.push_back(98); //specify the compression quality



     bool bSuccess = imwrite("D:/TestImage.jpg", img, compression_params); //write the image to file



     if ( !bSuccess )

    {

cout << "ERROR : Failed to save the image" << endl;

         //system("pause"); //wait for a key press

    }

namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

waitKey(0);  //wait for a keypress

     destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


利用 OpenCV 将读取的图像文件生成视频文件可以通过以下步骤实现,以下是具体的代码示例: ```python import os import cv2 from PIL import Image def image_to_video(image_path, media_path): &#39;&#39;&#39; 图片合成视频函数 :param image_path: 图片路径 :param media_path: 合成视频保存路径 :return: &#39;&#39;&#39; # 获取图片路径下面的所有图片名称 image_names = os.listdir(image_path) # 对提取到的图片名称进行排序 image_names.sort(key=lambda n: int(n[:-4])) # 设置写入格式 fourcc = cv2.VideoWriter_fourcc(&#39;M&#39;, &#39;P&#39;, &#39;4&#39;, &#39;V&#39;) # 设置每秒帧数 fps = 5 # 由于图片数目较少,这里设置的帧数比较低 # 读取第一个图片获取大小尺寸,因为需要转换成视频的图片大小尺寸是一样的 image = Image.open(image_path + image_names[0]) # 初始化媒体写入对象 media_writer = cv2.VideoWriter(media_path, fourcc, fps, image.size) # 遍历图片,将每张图片加入视频当中 for image_name in image_names: im = cv2.imread(os.path.join(image_path, image_name)) media_writer.write(im) print(image_name, &#39;合并成!&#39;) # 释放媒体写入对象 media_writer.release() print(&#39;视频写入成!&#39;) # 定义目录 file = &#39;C:/Users/jmWang/Desktop/sample_data/GSM/&#39; # 图片目录 output = &#39;C:/Users/jmWang/Desktop/1.mp4&#39; # 运行函数 image_to_video(file, output) ``` 在上述代码中,`image_to_video` 函数实现了将指定路径下的图片合成视频的功能。首先获取图片路径下的所有图片名称并排序,然后设置视频写入格式和帧率,接着读取第一张图片以获取图片的尺寸,初始化 `cv2.VideoWriter` 对象,该对象用于将图片写入视频文件。最后遍历所有图片,将每张图片写入视频成后释放媒体写入对象。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值