Last active
May 4, 2018 01:46
-
-
Save YHaruoka/f62f3b1bfd5f08b28f3d1048a8ba8902 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
#define PI 3.141592653589793 | |
string INPUT_FOLDER_NAME = "./input_image/"; | |
string OUTPUT_FOLDER_NAME = "./output_image/"; | |
// 最大・最小の回転角度(-180~180度) | |
int ROTATION_MAX = +120; | |
int ROTATION_MIN = -120; | |
// 回転による画像枚数 | |
int ROTATION_NUM = 12; | |
// 入力画像サイズ | |
int IMAGE_SIZE = 256; | |
// INPUT_FOLDER_NAMEのフォルダ内にある画像名を取得する | |
vector<string> getImageName(string dir_name) { | |
HANDLE hFind; | |
WIN32_FIND_DATA win32fd; | |
vector<string> file_names; | |
string extension[3] = { "png" , "jpg", "bmp" }; | |
for (int i = 0; i < 3; i++) { | |
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; | |
} | |
Mat getRotationImage(Mat src_img, float angle) { | |
Mat dst_img; | |
Point2f center = Point2f(src_img.cols / 2, src_img.rows / 2); | |
// 回転計算 | |
cv::Mat affine; | |
cv::getRotationMatrix2D(center, angle, 1.0).copyTo(affine); | |
cv::warpAffine(src_img, dst_img, affine, src_img.size(), cv::INTER_CUBIC); | |
// クリッピング | |
float radian_angle = angle * PI / 180.0; | |
int edge = src_img.cols / (abs(tan(radian_angle)) + 1) / abs(cos(radian_angle)); | |
Rect clip_region = Rect(src_img.cols / 2 - edge / 2, | |
src_img.rows / 2 - edge / 2, edge, edge); | |
dst_img = dst_img(clip_region); | |
return dst_img; | |
} | |
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にリサイズする | |
resize(img, img, Size(), (float)IMAGE_SIZE / img.cols, (float)IMAGE_SIZE / img.rows); | |
float diff_angle = 0.0f; | |
if (ROTATION_NUM > 1) { | |
diff_angle = (float)(ROTATION_MAX - ROTATION_MIN) / (ROTATION_NUM - 1); | |
} | |
// ROTATION_NUMの枚数分の画像を出力する | |
int image_index = 0; | |
for (int i = 0; i < ROTATION_NUM; i++) { | |
// 回転角度の取得 | |
float angle = ROTATION_MIN + i * diff_angle; | |
// 回転画像の取得を行う | |
Mat rot_img = getRotationImage(img, angle); | |
// 画像の保存 | |
string output_filename = OUTPUT_FOLDER_NAME + "output_" + to_string(image_index) + "_" + f; | |
imwrite(output_filename, rot_img); | |
image_index++; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment