Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用OpenCV读取文件夹中有序的图像序列_C++_Opencv - Fatal编程技术网

C++ 使用OpenCV读取文件夹中有序的图像序列

C++ 使用OpenCV读取文件夹中有序的图像序列,c++,opencv,C++,Opencv,我有下面的示例代码,在这里我显示我的网络摄像头 #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> int main() { cv::VideoCapture capture(0); cv::Mat myImage; while(1) { capture>>

我有下面的示例代码,在这里我显示我的网络摄像头

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

int main()
{
    cv::VideoCapture capture(0);

    cv::Mat myImage;
    while(1)
    {
        capture>> myImage;

        cv::imshow( "HEYO", myImage);
        int c = cv::waitKey(1);

    }
    return 0;
}
但是如何在文件夹中显示一系列图片,如:

0.jpg 
1.jpg 
2.jpg
... and so on 
使用
imread

我想使用该文件夹作为输入,而不是我的网络摄像头

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

int main()
{
    cv::VideoCapture capture(0);

    cv::Mat myImage;
    while(1)
    {
        capture>> myImage;

        cv::imshow( "HEYO", myImage);
        int c = cv::waitKey(1);

    }
    return 0;
}
#包括
#包括
#包括
int main()
{
cv::视频捕获(0);
cv::Mat myImage;
而(1)
{
捕获>>我的图像;
cv::imshow(“HEYO”,myImage);
int c=cv::waitKey(1);
}
返回0;
}
1)您可以将
VideoCapture
与以下功能结合使用:

filename–打开的视频文件(例如video.avi)或图像序列的名称(例如img_uu%02d.jpg,它将读取img_00.jpg、img_01.jpg、img_02.jpg等样本)

2) 或者根据计数器更改要加载的图像的名称

#include <opencv2/opencv.hpp>
#include <string>
#include <iomanip>
#include <sstream>

int main()
{
    std::string folder = "your_folder_with_images";
    std::string suffix = ".jpg";
    int counter = 0;

    cv::Mat myImage;

    while (1)
    {
        std::stringstream ss;
        ss << std::setw(4) << std::setfill('0') << counter; // 0000, 0001, 0002, etc...
        std::string number = ss.str();

        std::string name = folder + number + suffix;
        myImage = cv::imread(name);

        cv::imshow("HEYO", myImage);
        int c = cv::waitKey(1);

        counter++;
    }
    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
std::string folder=“您的带有图片的文件夹”;
std::string后缀=“.jpg”;
int计数器=0;
cv::Mat myImage;
而(1)
{
std::stringstream-ss;

如果我的图像名为0000、00010002?0011…0012…0121…0141…等等,该如何处理on@Bern案例1)img_u%04d.jpg,2)只需要一点流操作,或者使用好的旧sprintf。请参阅更新的代码片段,3)已经可以工作了谢谢,它可以工作了…可以使用我的imread(文件夹中的图像序列)吗作为视频捕获?@Bern是的,请参见选项1)您只需要
VideoCapture捕获(“您的_文件夹/%04d.jpg”);