Created
May 3, 2018 13:27
-
-
Save YHaruoka/5ba1ada0d895f130c8d5c146b913fb39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 HOLIZONTAL_MIRROR = false; | |
bool VERTICAL_MIRROR = false; | |
bool ALL_MIRROR = false; | |
// 入力画像サイズと、そのクリップサイズ | |
int IMAGE_SIZE = 256; | |
int CROP_SIZE = 128; | |
// 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 - CROP_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, CROP_SIZE, CROP_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 (HOLIZONTAL_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