码迷,mamicode.com
首页 > 其他好文 > 详细

[OpenCV] Samples 10: imagelist_creator

时间:2016-12-01 14:33:44      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:src   opencv   parser   filename   cpp   例子   i++   empty   div   

yaml写法的简单例子。将 $ ./ 1 2 3 4 5 命令的参数(代表图片地址)写入yaml中。

 

写yaml文件。

参考:[OpenCV] Samples 06: [ML] logistic regression 读xml文件。

技术分享
    {
        /*
         * Jeff --> Load xml.
         *          transform to Mat.
         * FileStorage.
         */
        cout << "loading the dataset...";
        // Step 1.
        FileStorage f;
        if(f.open(filename, FileStorage::READ))
        {
            // Step 2.
            f["datamat"] >> data;
            f["labelsmat"] >> labels;
            f.release();
        }
        else
        {
            cerr << "file can not be opened: " << filename << endl;
            return 1;
        }
        // Step 3.
        data.convertTo(data, CV_32F);
        labels.convertTo(labels, CV_32F);
 
        cout << "read " << data.rows << " rows of data" << endl;
    }
read xml

 

/*this creates a yaml or xml list of files from the command line args
 */

#include "opencv2/core/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <string>
#include <iostream>

using std::string;
using std::cout;
using std::endl;

using namespace cv;

static void help(char** av)
{
    cout << "\nThis creates a yaml or xml list of files from the command line args\n"
            "usage:\n./" << av[0] << " imagelist.yaml *.png\n"
         << "Try using different extensions.(e.g. yaml yml xml xml.gz etc...)\n"
         << "This will serialize this list of images or whatever with opencv‘s FileStorage framework" << endl;
}

int main(int ac, char** av)
{
    cv::CommandLineParser parser(ac, av, "{help h||}{@output||}");
    if (parser.has("help"))
    {
        help(av);
        return 0;
    }
    string outputname = parser.get<string>("@output");

    cout << "{yaml} outputname = " << outputname << endl;

    if (outputname.empty())
    {
        help(av);
        return 1;
    }

    Mat m = imread(outputname); //check if the output is an image - prevent overwrites!
    if(!m.empty()){
        std::cerr << "fail! Please specify an output file, don‘t want to overwrite you images!" << endl;
        help(av);
        return 1;
    }

    //Jeff --> write into outputname.
    FileStorage fs(outputname, FileStorage::WRITE);
    fs << "images" << "[";
    for(int i = 2; i < ac; i++){
        //Jeff --> write by "<<", redirection to file stream.
        fs << string(av[i]);
    }
    fs << "]";
    return 0;
}

 

[OpenCV] Samples 10: imagelist_creator

标签:src   opencv   parser   filename   cpp   例子   i++   empty   div   

原文地址:http://www.cnblogs.com/jesse123/p/6121448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!