Skip to content

Instantly share code, notes, and snippets.

@YHaruoka
Created May 3, 2018 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YHaruoka/f49cd59490afc6c5b39747416634d146 to your computer and use it in GitHub Desktop.
Save YHaruoka/f49cd59490afc6c5b39747416634d146 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <Windows.h>
#include <stdexcept>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
string INPUT_FOLDER_NAME = "./input_image/";
string OUTPUT_FOLDER_NAME = "./output_image/";
// 各種方向の反転を行うかどうか
bool HORIZONTAL_MIRROR = true;
bool VERTICAL_MIRROR = true;
bool ALL_MIRROR = true;
// 入力画像サイズと、そのクリップサイズ
int IMAGE_SIZE = 256;
int CLIP_SIZE = 224;
// INPUT_FOLDER_NAMEのフォルダ内にある画像名を取得する
vector<string> getImageName(string dir_name) {
HANDLE hFind;
WIN32_FIND_DATA win32fd;
std::vector<std::string> file_names;
std::string extension[3] = { "png" , "jpg", "bmp"};
for (int i = 0; i < 3; i++) {
std::string search_name = dir_name + "*." + extension[i];
hFind = FindFirstFile(search_name.c_str(), &win32fd);
if (hFind == INVALID_HANDLE_VALUE) {
continue;
}
do {
if (win32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
}
else {
file_names.push_back(win32fd.cFileName);
}
} while (FindNextFile(hFind, &win32fd));
FindClose(hFind);
}
return file_names;
}
int main(void){
// ファイル内の画像名の取得
vector<string> file_names = getImageName(INPUT_FOLDER_NAME);
// ファイル名の表示部分
for (auto f : file_names) {
cout << f << endl;
}
// フォルダ内の全ての画像に対して処理
for (auto f : file_names) {
// 画像をインプットする
string input_filename = INPUT_FOLDER_NAME + f;
Mat img = imread(input_filename, IMREAD_COLOR);
// IMAGE_SIZE×IMAGE_SIZEにリサイズする
cv::resize(img, img, Size(), (float)IMAGE_SIZE / img.cols, (float)IMAGE_SIZE / img.rows);
// CROP_SIZE×CROP_SIZEで9箇所でクリッピングする
int image_index = 0;
int d = (IMAGE_SIZE - CLIP_SIZE) / 2;
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
Rect clip_region = Rect(0 + x * d, 0 + y * d, CLIP_SIZE, CLIP_SIZE);
string output_filename = OUTPUT_FOLDER_NAME + "output_" + to_string(image_index) + "_" + f;
Mat clip_img = img(clip_region);
imwrite(output_filename, clip_img);
image_index++;
// MIRRORがtrueなら反転画像も出力する
if (HORIZONTAL_MIRROR == true) {
Mat dst_img;
flip(clip_img, dst_img, 0);
string output_filename = OUTPUT_FOLDER_NAME + "output_" + to_string(image_index) + "_" + f;
imwrite(output_filename, dst_img);
image_index++;
}
if (VERTICAL_MIRROR == true) {
Mat dst_img;
flip(clip_img, dst_img, 1);
string output_filename = OUTPUT_FOLDER_NAME + "output_" + to_string(image_index) + "_" + f;
imwrite(output_filename, dst_img);
image_index++;
}
if (ALL_MIRROR == true) {
Mat dst_img;
flip(clip_img, dst_img, -1);
string output_filename = OUTPUT_FOLDER_NAME + "output_" + to_string(image_index) + "_" + f;
imwrite(output_filename, dst_img);
image_index++;
}
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment