批量处理图像

转自:http://blog.youkuaiyun.com/sangni007/article/details/7975075

 

stdafx.h:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once
#pragma warning(disable: 4996)
#pragma warning(disable: 4819)
//#define WIN32_LEAN_AND_MEAN

#include <stdio.h>
#include <tchar.h>

main.cpp

#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "windows.h"
#include <vector>
#include <string>
#include "iostream"
#include <ctime> //计时用的头文件
using namespace std;
typedef std::vector<std::string> file_lists;
template<typename T>  T sqr(T x) { return x * x;}
 

static int str_compare(const void *arg1, const void *arg2)
{
       return strcmp((*(std::string*)arg1).c_str(), (*(std::string*)arg2).c_str());//比较字符串arg1 and arg2
}


file_lists ScanDirectory(const std::string &path, const std::string &extension)
{
    WIN32_FIND_DATA wfd;//WIN32_FIND_DATA:Contains information about the file that is found by the 
        //FindFirstFile, FindFirstFileEx, or FindNextFile function
     HANDLE hHandle;
     string searchPath, searchFile;
     file_lists vFilenames;
     int nbFiles = 0;
    
     searchPath = path + "/*" + extension;
     hHandle = FindFirstFile(searchPath.c_str(), &wfd);//Searches a directory for a file or subdirectory
              //with a name that matches a specific name
     if (INVALID_HANDLE_VALUE == hHandle)
    {
         fprintf(stderr, "ERROR(%s, %d): Cannot find (*.%s)files in directory %s/n",
              __FILE__, __LINE__, extension.c_str(), path.c_str());
         exit(0);
    }
    do
    {
         //. or ..
          if (wfd.cFileName[0] == '.')
         {
              continue;
          }
          // if exists sub-directory
          if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//dwFileAttributes:The file attributes of a file
        {             

          //FILE_ATTRIBUTE_DIRECTORY:The handle identifies a directory
             continue;
         }
        else//if file
        {
            searchFile = path + "/" + wfd.cFileName;
            vFilenames.push_back(searchFile);
            nbFiles++;
         }
    }while (FindNextFile(hHandle, &wfd));//Call this member function to continue a file search begun 
          //with a call to CGopherFileFind::FindFile

    FindClose(hHandle);//Closes a file search handle opened by the FindFirstFile, FindFirstFileEx,
       //or FindFirstStreamW function

 // sort the filenames
    qsort((void *)&(vFilenames[0]), (size_t)nbFiles, sizeof(string), str_compare);//Performs a quick sort

    return vFilenames;
}

 
int _tmain(int argc, _TCHAR* argv[])
{
 double t = getTickCount();
 file_lists files = ScanDirectory("D:/test2", ".jpg");
 if (files.empty())
 {
  cout<<"no image file find in current directory.."<<endl;
  system("pause");
  exit(-1);
 }
 int size = files.size();
 cout<<"there are "<<size<<" image files totally...."<<endl;
 for (int i=0; i<size; i++)
 {
  Mat img = imread(files[i].c_str()); //随便放一张jpg图片在D盘或另行设置目录
  	//Mat img=imread(filename,1);
  	//imshow("src",img);
  	Mat dst;
  	GaussianBlur(img,dst,Size(3,3),0);
  	string a="D:/test2/out/";
  string b;
  b.assign(files[i],9,10);
  a.append(b);
  a.append("_RC.png");
  imwrite(a.c_str(),dst);
  cout<<files[i].c_str()<<" "<<i<<"/"<<size<<endl;
  
 }
 t=getTickCount()-t;
 cout<<"time:"<<t*1000/getTickFrequency()<<endl;
}


 

代码中包含的string一些操作

代码有时会报错:这时修改:属性——常规——字符集——未设置

还有更高级的一些应用比如mingming cheng的用的文件后缀导入方式,俺还么有研究明白,研究明白了自会奉上,待续中……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值