opencv中怎么连续保存多张图片
时间: 2023-05-12 07:07:33 浏览: 179
在 OpenCV 中,可以使用 imwrite 函数来保存图像。如果要连续保存多张图片,可以使用一个循环来实现。以下是一个示例代码:
```cpp
#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;
using namespace std;
int main()
{
string path = "path/to/save/images/";
int num_images = 10;
for (int i = 0; i < num_images; i++)
{
Mat image = imread("path/to/image" + to_string(i) + ".jpg");
imwrite(path + "image" + to_string(i) + ".jpg", image);
}
return 0;
}
```
在这个示例中,我们假设要保存的图像都以 "image0.jpg"、"image1.jpg"、"image2.jpg" 等命名,并且保存在同一个文件夹中。我们使用一个循环来读取每张图像,并使用 imwrite 函数将其保存到指定的文件夹中。
相关问题
C++opencv连续多张图片修改后保存
你可以使用OpenCV库中的imwrite函数来保存多张修改后的图像。以下是一个示例代码,它读取一系列图像,将它们转换为灰度图像,并将它们保存到一个新的目录中。
```
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// 读取图像序列
vector<Mat> imgList;
for (int i = 1; i <= 10; i++)
{
string filename = "image_" + to_string(i) + ".jpg";
Mat img = imread(filename);
if (img.empty())
{
cout << "Could not open or find the image" << endl;
return -1;
}
imgList.push_back(img);
}
// 修改图像并保存
string outputDir = "output/";
for (int i = 0; i < imgList.size(); i++)
{
// 将图像转换为灰度图像
Mat grayImg;
cvtColor(imgList[i], grayImg, COLOR_BGR2GRAY);
// 保存修改后的图像
string outputFilename = outputDir + "gray_" + to_string(i+1) + ".jpg";
imwrite(outputFilename, grayImg);
}
return 0;
}
```
在此示例中,我们首先使用for循环读取图像序列,并将它们存储在一个vector容器中。然后,我们使用另一个for循环遍历图像序列,将每个图像转换为灰度图像,并使用imwrite函数将其保存到指定目录中。请注意,我们为每个输出文件生成一个新的文件名,以避免覆盖原始图像。
python opencv 读取摄像头并每隔一分钟,连续保存三秒的图片
可以使用Python的OpenCV库读取摄像头,并使用time库实现每隔一分钟保存三秒的图片。
以下是一个示例代码:
```python
import cv2
import time
# 打开摄像头
cap = cv2.VideoCapture(0)
# 设置图片保存路径
save_path = './images/'
# 定义保存图片的函数
def save_image():
# 获取当前时间作为文件名
file_name = time.strftime('%Y%m%d%H%M%S', time.localtime()) + '.jpg'
# 保存图片
cv2.imwrite(save_path + file_name, frame)
print('Image saved:', file_name)
# 定义计数器和时间戳
count = 0
timestamp = time.time()
while True:
# 读取一帧图片
ret, frame = cap.read()
# 每隔一分钟保存三秒的图片
if time.time() - timestamp >= 60:
if count < 3:
save_image()
count += 1
else:
# 计数器和时间戳归零
count = 0
timestamp = time.time()
# 显示图片
cv2.imshow('frame', frame)
# 按下q键退出循环
if cv2.waitKey(1) == ord('q'):
break
# 释放摄像头并关闭所有窗口
cap.release()
cv2.destroyAllWindows()
```
这段代码会打开摄像头并不断地读取图片,同时每隔一分钟保存三秒的图片到指定的路径。按下q键可以退出循环并关闭所有窗口。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)