zoukankan      html  css  js  c++  java
  • 利用OPENCV写的对文件夹下所有图片大小归一化的小工具

    /****************************************************************************************************************************************
    //遍历文件夹下所有图片文件,并水平翻转复制,和大小归一化.

    //lian  2011-7-13
    *******************************************************************************************************************************************/

    #include <afxwin.h>
    #include <iostream>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/core/core.hpp>
    #include <opencv2/imgproc/imgproc.hpp>

    using namespace std;
    using namespace cv;

    const int NORM_WIDTH = 48; //归一化后的宽和高
    const int NORM_HEIGHT = 48;

    const char imgSrcPath[] = "d:\\训练图片库\\"; //原始图片路径

    const string  imgSavePath = "d:\\训练图片大小归一化库2\\"; //图片最后保存路径

    void Recurse(const char* pstr)
    //遍历文件夹下的某类型文件图片
    {
     namedWindow("srcIMG",1);
     namedWindow("dstIMG",1);
     Mat img;
     CFileFind finder;
     // build a string with wildcards

     CString strWildcard(pstr);
     strWildcard += _T("\\*.png");

     // start working for files

     BOOL bWorking = finder.FindFile(strWildcard);
     int i=0;
    //输出查找文件夹下的所有文件名
     while (bWorking)
     {
      bWorking = finder.FindNextFile();

      // skip . and .. files; otherwise, we'd recur infinitely!
      if (finder.IsDots())
       continue;

      CString sFileName = finder.GetFileName();
      
        //CString 转化为string
      char s[30]= {0};
      char* chr= (char*)(LPTSTR) LPCTSTR (sFileName);
      for (int i=0;i<sFileName.GetLength();i++)//将两个字符转换为一个字符
      {
       sprintf(&s[i],"%c",*(chr++));
       chr++;
      }

      string strFileName(s);  //转化为string的文件名

      string strPathFileName = imgSrcPath+strFileName; //路径文件全名

      img=imread(strPathFileName,1); //读取文件
      Mat dstImg,srcNormImg,dstNormImg;

      flip(img,dstImg,1);//水平翻转图像

      resize(img,srcNormImg,Size(NORM_WIDTH,NORM_HEIGHT),0,0,INTER_CUBIC);    //大小归一化
      resize(dstImg,dstNormImg,Size(NORM_WIDTH,NORM_HEIGHT),0,0,INTER_CUBIC);


      string strFileName2(strFileName); //翻转图像的文件名
      
      int pos = strFileName2.find('.');
      strFileName2 = strFileName2.insert(pos,"_1");

      cout<<strFileName2<<endl;

      imwrite(imgSavePath+strFileName,srcNormImg);   //保存归一化后的图片
      imwrite(imgSavePath+strFileName2,dstNormImg);  //保存翻转归一化后的图片

      imshow("srcIMG",srcNormImg);
      imshow("dstIMG",dstNormImg);
      char c = waitKey(0);

     }

     finder.Close();
    }//Recurse

    int main()
    {
     if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))//初始化MFC

      cout << "panic!" << endl;
     else
      Recurse( imgSrcPath); //此处也需要调整路径
     return 0;
    }

  • 相关阅读:
    【LeetCode】589.N叉树的前序遍历(递归+迭代,java实现,详细分析)
    百度网盘偷偷更新,终于实现免费不限速了!
    如何调整DOS窗口的宽高
    输入adb shell 时 提示error: more than one device and emulator
    logcat不显示信息
    安卓打开File Explorer里面不显示内容
    android查看源码的时候看不了
    This version of the rendering library is more recent than your version of ADT plug-in. Please update
    eclipse或者AS链接手机真机之后,logcat里面日志信息乱跳
    Android ADB使用之详细篇
  • 原文地址:https://www.cnblogs.com/seacode/p/2109459.html
Copyright © 2011-2022 走看看