C++ opencv 挑选文件夹下特定文件并存储

我这个程序主要用途如下: 我的文件夹下有很多图片, 他们的文件名是 "number.jpg", 数字并不是按照特定顺序。我现在从一个文件读取一个数字,这里是变量number, 然后把大于这个number的文件,挑选出来存储到我新建的文件夹下。程序如下:红色的是注释

//opencv header
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include<fstream>
#include<stdlib.h>
#include<direct.h>
#include<string>
#include <iostream>

#include <io.h>
using namespace std;
    void main()
   {
//新建文件夹,名字是new
    char* newfile="D://coms661//project//new";
    mkdir(newfile);
    string filename;
    string s1,s2,s3;

    //打开所要文件得到number这个数字
    s1="D://coms661//project//Resource//video//training//video_26//Files";
    s2="//Einsert.txt";
    string number;
    ifstream inf(s1+s2);
    inf>>number;


    //打开所要挑选文件的文件夹

    string s4="D://coms661//project//Resource//video//training//video_26//";
    IplImage* img=NULL;
    string jpg=".jpg";
//hFile 是long类型  
    long hFile;
    struct _finddata_t c_file;    //struct  _finddate_t 是包里面有的。不是我自己写的。
    if((hFile = _findfirst((s4+"*"+jpg).c_str(), &c_file )) != -1L) //找到第一个图片,把这个图片赋值给c_file
    {
         //判断是否是文件夹,如果是文件夹,则不处理
        if (c_file.attrib != _A_SUBDIR)
        {
            string sFileInfo;
            sFileInfo=c_file.name;  //这里的name不包含路径
            size_t found=sFileInfo.find("."); //size_t 是unsigned int型, 这里要找到图片文件名的数字部分。去掉.jpg
            string filename=sFileInfo.substr(0,found); //得到文件名的数字部分。
            int filenumber=atoi(filename.c_str()); //转换为int
            if(filenumber>atoi(number.c_str()))
                 //download the image
            {
                img=cvLoadImage((s4+sFileInfo).c_str());
                 //save the image
                string newimg_str="D://coms661//project//new//"+number+jpg;
                cvSaveImage(newimg_str.c_str(),img);
           
            }
        }

        while(_findnext(hFile, &c_file ) == 0 )
        {
             //判断是否是文件夹,如果是文件夹,则不处理
            if (c_file.attrib != _A_SUBDIR)
            {
                string sFileInfo;
                sFileInfo=c_file.name;
                size_t found=sFileInfo.find(".");
                string filename=sFileInfo.substr(0,found);
                int filenumber=atoi(filename.c_str());
                if(filenumber>atoi(number.c_str()))
                {
                    //download the image
                    img=cvLoadImage((s4+sFileIn fo).c_str());
                    //save the image

                    string newimg_str="D://coms661//project//new//"+sFileInfo+jpg;
                    cvSaveImage(newimg_str.c_str(),img);
                  
                }
            }

            
        }
        _findclose(hFile); //关闭


        system("pause");

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值