【opencv450-samples】读取图像路径列表并保持比例显示

/*
 *  Created on: Nov 23, 2010
 *      Author: Ethan Rublee
 *
 * A starter sample for using opencv, load up an imagelist
 * that was generated with imagelist_creator.cpp
 * easy as CV_PI right?
 */
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

static void help(char** av)
{
    cout << "\nThis program gets you started being able to read images from a list in a file\n"
        "Usage:\n./" << av[0] << " image_list.yaml\n"
        << "\tThis is a starter sample, to get you up and going in a copy pasta fashion.\n"
        << "\tThe program reads in an list of images from a yaml or xml file and displays\n"
        << "one at a time\n"
        << "\tTry running imagelist_creator to generate a list of images.\n"
        "Using OpenCV version %s\n" << CV_VERSION << "\n" << endl;
}
//读取路径字符串列表 
static bool readStringList(const string& filename, vector<string>& l)
{
    l.resize(0);//调整容器大小 
    FileStorage fs(filename, FileStorage::READ);//读取文件
    if (!fs.isOpened())
        return false;
    FileNode n = fs.getFirstTopLevelNode();//首节点
    if (n.type() != FileNode::SEQ)
        return false;
    FileNodeIterator it = n.begin(), it_end = n.end();//迭代器
    for (; it != it_end; ++it)
        l.push_back((string)*it);//加入列表
    return true;
}
//依次显示所有图像灰度图
static int process(const vector<string>& images)
{
    namedWindow("image", WINDOW_KEEPRATIO); //保持图像比例 resizable window;
    for (size_t i = 0; i < images.size(); i++)
    {
        Mat image = imread(images[i], IMREAD_GRAYSCALE); // do grayscale processing?
        imshow("image", image);
        cout << "Press a key to see the next image in the list." << endl;
        waitKey(); // wait infinitely for a key to be pressed
    }
    return 0;
}

int main(int ac, char** av)
{
    cv::CommandLineParser parser(ac, av, "{help h||}{@input||}");
    if (parser.has("help"))
    {
        help(av);
        return 0;
    }
    std::string arg = parser.get<std::string>("@input");//文件名
    if (arg.empty())
    {
        help(av);
        return 1;
    }
    vector<string> imagelist;//图像路径列表

    if (!readStringList(arg, imagelist))//读取文件路径列表
    {
        cerr << "Failed to read image list\n" << endl;
        help(av);
        return 1;
    }

    return process(imagelist);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值