Skip to content

Instantly share code, notes, and snippets.

@MasazI
Created July 31, 2015 06:58
Show Gist options
  • Select an option

  • Save MasazI/ea26a874d62d5231556b to your computer and use it in GitHub Desktop.

Select an option

Save MasazI/ea26a874d62d5231556b to your computer and use it in GitHub Desktop.
argumentation part
char* db_file_list_path = argv[2];
char* output_dir = argv[3];
vector<string> file_list;
// trainリストのロード
try {
ifstream ifs;
OpenFile(ifs, db_file_list_path);
string readline;
while (getline(ifs, readline)) {
file_list.push_back(readline);
}
}
catch (const char* error) {
cerr << error << endl;
return -1;
}
int file_list_size = file_list.size();
// Train(画像毎)
for (int i = 0; i < file_list_size; i++){
cout << "GENERATE No. " << i << " " << file_list[i] << endl;
const string textline = file_list[i];
vector<string> split_line = splitstring(textline, '\t');
// 画像のパスとカテゴリーの取得
string filepath = split_line[0];
string output_file_name_prefix = split_line[1];
// 画像のロード
Mat train_image = imread(filepath);
// アフィン変換
// 変換前の三点
const cv::Point2f src_pt[] = { cv::Point2f(100, 100), cv::Point2f(200, 200), cv::Point2f(100, 200) };
float x1 = 100 + rand() % 10 + 1 - rand() % 5;
float y1 = 100 + rand() % 10 + 1 - rand() % 5;
float x2 = 200 + rand() % 10 + 1 - rand() % 5;
float y2 = 200 + rand() % 10 + 1 - rand() % 5;
float x3 = 100 + rand() % 10 + 1 - rand() % 5;
float y3 = 200 + rand() % 10 + 1 - rand() % 5;
// 変換後の三点
const cv::Point2f dst_pt[] = { cv::Point2f(x1, y1), cv::Point2f(x2, y2), cv::Point2f(x3, y3) };
// これらから,アフィン変換行列を計算
const cv::Mat affine_matrix = cv::getAffineTransform(src_pt, dst_pt);
//std::cout << "affine_matrix=\n" << affine_matrix << std::endl;
cv::Mat dst_img;
cv::warpAffine(train_image, dst_img, affine_matrix, train_image.size());
imwrite(output_dir + output_file_name_prefix + "_af.jpg", dst_img);
// 透視投影変換
cv::Point2f pts1[] = { cv::Point2f(150, 150), cv::Point2f(150, 300), cv::Point2f(350, 300), cv::Point2f(350, 150) };
float hx1 = 150 + rand() % 5 + 1 - rand() % 5;
float hy1 = 150 + rand() % 5 + 1 - rand() % 5;
float hx2 = 150 + rand() % 5 + 1 - rand() % 5;
float hy2 = 300 + rand() % 5 + 1 - rand() % 5;
float hx3 = 350 + rand() % 5 + 1 - rand() % 5;
float hy3 = 300 + rand() % 5 + 1 - rand() % 5;
float hx4 = 350 + rand() % 5 + 1 - rand() % 5;
float hy4 = 150 + rand() % 5 + 1 - rand() % 5;
cv::Point2f pts2[] = { cv::Point2f(hx1, hy1), cv::Point2f(hx2, hy2), cv::Point2f(hx3, hy3), cv::Point2f(hx4, hy4) };
// 透視変換行列を計算
cv::Mat perspectivehm_matrix = cv::getPerspectiveTransform(pts1, pts2);
cv::Mat dsthm_img;
cv::warpPerspective(train_image, dsthm_img, perspectivehm_matrix, train_image.size(), cv::INTER_LINEAR);
imwrite(output_dir + output_file_name_prefix + "_hm.jpg", dsthm_img);
// 透視投影変換&回転
cv::Point2f ptsa1[] = { cv::Point2f(150, 150), cv::Point2f(150, 300), cv::Point2f(350, 300), cv::Point2f(350, 150) };
float hax1 = 150 + rand() % 3 + 1 - rand() % 5;
float hay1 = 150 + rand() % 3 + 1 - rand() % 5;
float hax2 = 150 + rand() % 3 + 1 - rand() % 5;
float hay2 = 300 + rand() % 3 + 1 - rand() % 5;
float hax3 = 350 + rand() % 3 + 1 - rand() % 5;
float hay3 = 300 + rand() % 3 + 1 - rand() % 5;
float hax4 = 350 + rand() % 3 + 1 - rand() % 5;
float hay4 = 150 + rand() % 3 + 1 - rand() % 5;
cv::Point2f ptsa2[] = { cv::Point2f(hax1, hay1), cv::Point2f(hax2, hay2), cv::Point2f(hax3, hay3), cv::Point2f(hax4, hay4) };
// 透視変換行列を計算
cv::Mat perspectiveha_matrix = cv::getPerspectiveTransform(ptsa1, ptsa2);
cv::Mat dsthmq_img;
cv::warpPerspective(train_image, dsthmq_img, perspectiveha_matrix, train_image.size(), cv::INTER_LINEAR);
// 回転: -40 [deg], スケーリング: 1.0 [倍]
float angle = rand() % 180 + 1, scale = 1.0 + (float)rand() / (RAND_MAX * 100);
// 中心:画像中心
cv::Point2f center(dsthmq_img.cols*0.5, dsthmq_img.rows*0.5);
// 以上の条件から2次元の回転行列を計算
const cv::Mat affinehmaa_matrix = cv::getRotationMatrix2D(center, angle, scale);
//std::cout << "affine_matrix=\n" << affine_matrix << std::endl;
cv::Mat dsthmaa_img;
cv::warpAffine(dsthmq_img, dsthmaa_img, affinehmaa_matrix, dsthmq_img.size());
imwrite(output_dir + output_file_name_prefix + "_hma.jpg", dsthmaa_img);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment