フォルダ内の画像リストの作成
前回パイプ処理を使ってgnuplotを実行させてみました。
このやり方って、ひよこプログラマにはありがたすぎです。
さらに、フォルダ内のある拡張子におけるリストを作成するのにも使えるんじゃないと思いました。windows APIがさっぱりな私みたいな人にはおすすめです(自虐)。
その際、使わせて頂いたフリーソフトは「LS」です。
http://www.vector.co.jp/soft/win95/util/se247619.html
多謝です。
手順としては、まずLSを用いて、任意のフォルダ内の拡張子のリストをtxtに列挙します。それをfgetsで一行ずつ読み取っていきます。あとは、読みっとった名前から画像を使いやすい型で保存するというものです。
ちなみに、今回はopenCVのMat型に変換してます。
//namedWindow(name, CV_WINDOW_AUTOSIZE);
//imshow(name ,mat[count]);
を含めると、画像がぶわっと出現します。
以下、ソースです!
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <string.h>
#ifdef _DEBUG
//Debugモードの場合
#pragma comment(lib,"opencv_core230d.lib")
#pragma comment(lib,"opencv_highgui230d.lib")
#else
//Releaseモードの場合
#pragma comment(lib,"opencv_core230.lib")
#pragma comment(lib,"opencv_highgui230.lib")
#endif
using namespace cv;
using namespace std;
#define LS_PATH "LS.exe"
//-------------------------------------------------------------------------
//任意のディレクトリから画像を取得します
//注意点:ls.exeがないと作動しません
//-------------------------------------------------------------------------
void read_file()
{
//------------------------------------------------------------------
//パイプを作成
//------------------------------------------------------------------
FILE *gp;
if ( ( gp = _popen(LS_PATH , "w") ) == NULL){
fprintf(stderr,"ファイルが見つかりません%s." , LS_PATH);
exit(EXIT_FAILURE);
}
fflush(gp); //バッファに格納されているデータを吐き出す(必須)
_pclose(gp);
//-------------------------------------------------------------------
//画像のリストを作成
//------------------------------------------------------------------
FILE *fp;
fp = fopen("ls.txt","r");
if(fp == NULL){
cout << "テキスト読み込み失敗" << endl;
}
char name[256];
bool flag = false;
Mat* mat;
mat = new Mat[100];
int count = 0;
char *p;
while( fgets( name , sizeof(name) , fp ) != NULL ){
if(flag == true){
while( strchr( name , '\n') != NULL){
p = strchr( name, '\n' );
*p = ' ';
}
while( strchr( name , '\\') != NULL){
p = strchr( name, '\\' );
*p = '/';
}
printf("name = %s\n",name);
mat[count] = imread(name,1);
if(mat[count].empty()){
cout << "読み込みに失敗しました" << endl;
}else{
cout << "読み込みに成功しました" << endl;
//namedWindow(name, CV_WINDOW_AUTOSIZE);
//imshow(name ,mat[count]);
}
}
flag = true;
count ++;
}
fclose(fp);
//--------------------------------------------------------------------
waitKey(0);
}