2011年12月23日金曜日

ちょっと休憩

久しぶりにラベルがProgramsな投稿
画像の回転をやってみました(画像?ここゲーム関連じゃ・・・)

問題設定?は以下のような斜めになってる棋譜画像を
正しい方向に元に戻すことにしました.
ここではPNG形式のファイルになってるんですが,
実際にはPGM形式のファイルを使用しています orz
あ、この棋譜画像は棋譜画像作成ツールを使用させて頂いて作成したものを
テスト用にわざわざ10度程度回転させたものです.




ここでは超高等テクニックであるHough変換を使って直線を検出したりはせず<-
棋譜画像で傾いているのは大抵この2つのパターンなので
場合分けして左上と右上の頂点を調べて,
そこから傾いている角度を算出して,
直したものを結果として出力画像として出力するものを作成してみました.
素晴らしく汎用性皆無のコードを最後に貼っつけて置きます・・・.
使い方はコマンドライン引数に対象とするPGM形式のファイルパスと
結果を出力するPGM形式のファイル名を指定する感じで動きます.
こんなコード書いていて大学生として恥ずかしくないわけがない<-
githubにあげられるようなコード書けるように頑張ろう・・・<-

あ,っでこのコードによる出力は以下のようになりました.
これもPNG形式の画像となっていますが,実際はPGM形式の画像です・・・ orz
う〜ん、ちゃんと正しい方向な棋譜が得られていることがわかります.ヽ(*゚д゚)ノ
意外に角を打たれていても良い感じですね<-
けど,ただ回転行列を用いているだけなので,画質が劣化してると思います.Σ(´∀`;)
あと,トリミング機能あったほうが良いかも<-

 


まとめると<-
  • Hough変換使わなくても簡単な棋譜の方向の修正はたぶんできる.
  • 単純に回転行列使ってるだけじゃ画質が劣化する.



  1. #include <iostream>  
  2. #include <string>  
  3. #include <vector>  
  4. #include <iterator>  
  5. #include <algorithm>  
  6. #include <fstream>  
  7. #include <cstdlib>  
  8. #include <cmath>  
  9.   
  10. class Image {  
  11.  public:  
  12.   Image(char *file_name, char *output_file_name) { init(file_name,  
  13. output_file_name); }  
  14.   void init(char *file_name, char *output_file_name);  
  15.   void doCorrect();  
  16.   void outputImage();  
  17.     
  18.  private:  
  19.   std::string file_type_;  
  20.   std::string file_comment_;  
  21.   int img_height_;  
  22.   int img_width_;  
  23.   int max_value_;  
  24.   std::vector<std::vector <int> > img_data_;  
  25.   std::string output_file_name_;  
  26. };  
  27.   
  28. void Image::init(char *file_name, char *output_file_name) {  
  29.   output_file_name_ = output_file_name;  
  30.   
  31.   std::ifstream fin(file_name);  
  32.   if (!fin) {  
  33.     std::cerr << "file not found" << std::endl;  
  34.     exit(1);  
  35.   }  
  36.       
  37.   fin >> file_type_;  
  38.   if (file_type_ != "P2") {  
  39.     std::cerr << "file not found" << std::endl;  
  40.     exit(1);      
  41.   }  
  42.     
  43.   // コメント処理 コメントが1行あると仮定  
  44.   fin.ignore();  
  45.   getline(fin, file_comment_);  
  46.      
  47.   fin >> img_width_ >> img_height_ >> max_value_;  
  48.   img_data_.resize(img_height_);  
  49.   for (int i = 0; i < img_height_; i++) {  
  50.     img_data_[i].resize(img_width_);  
  51.     for (int j = 0; j < img_width_; j++) {  
  52.         fin >> img_data_[i][j];  
  53.     }  
  54.   }  
  55.   fin.close();  
  56. }  
  57.   
  58. void Image::doCorrect() {  
  59.   int x_top_left = -1;  
  60.   int y_top_left = 0;  
  61.   int x_top_right = -1;  
  62.   int y_top_right = 0;  
  63.   
  64.   // 左上の頂点を探索  
  65.   for (int i = 0; i < img_height_; i++) {  
  66.     for (int j = 0; j < img_width_; j++) {  
  67.       if (img_data_[i][j] < max_value_ / 2) {  
  68.         x_top_left = j;  
  69.         y_top_left = i;  
  70.         break;  
  71.       }  
  72.     }  
  73.     if (x_top_left >= 0) {  
  74.       break;  
  75.     }  
  76.   }  
  77.     
  78.   // 反時計回りに傾いていると考えられる場合  
  79.   if (x_top_left > img_width_ / 2) {  
  80.     x_top_right = x_top_left;  
  81.     y_top_right = y_top_left;  
  82.     int min_distance = img_width_;  
  83.     for (int i = 0; i < img_height_ / 2; i++) {  
  84.       for (int j = 0; j < img_width_ / 2; j++) {  
  85.         if (img_data_[i][j] < max_value_ / 2 && j < min_distance) {  
  86.           min_distance = j;  
  87.           x_top_left = j;  
  88.           y_top_left =  i;  
  89.         }  
  90.       }  
  91.     }     
  92.   }  
  93.   else {  
  94.     int max_distance = 0;  
  95.     for (int i = 0; i < img_height_; i++) {  
  96.       for (int j = 0; j < img_width_; j++) {      
  97.         if (img_data_[i][j] < max_value_ / 2 && j > max_distance) {  
  98.           max_distance = j;  
  99.           x_top_right = j;  
  100.           y_top_right =  i;  
  101.         }  
  102.       }  
  103.     }   
  104.   }  
  105.     
  106.   double theta = atan2((double)(y_top_right - y_top_left),  
  107.                        (double)(x_top_right - x_top_left));  
  108.   
  109.   std::vector<std::vector<int> > correct_img_data(img_height_,  
  110.  std::vector<int>(img_width_, max_value_));  
  111.   for (int i = 0; i < img_height_; i++) {  
  112.     for (int j = 0; j < img_width_; j++) {  
  113.       int x_correct = j * cos(theta) + i * sin(theta);  
  114.       int y_correct = -j * sin(theta) + i * cos(theta);  
  115.         
  116.       if (x_correct >= 0 && x_correct < img_width_  
  117.           && y_correct >= 0 && y_correct  < img_height_) {  
  118.           correct_img_data[y_correct][x_correct] = img_data_[i][j];  
  119.       }  
  120.     }  
  121.   }  
  122.   img_data_ = correct_img_data;  
  123. }  
  124.   
  125. void Image::outputImage() {  
  126.   std::ofstream fout(output_file_name_.c_str());  
  127.   fout << "P2" << std::endl;  
  128.   fout << file_comment_ << std::endl;  
  129.   fout << img_width_ << " " << img_height_ << std::endl;  
  130.   fout << max_value_ << std::endl;  
  131.   
  132.   for (int i = 0; i < img_height_; i++) {  
  133.     std::copy(img_data_[i].begin(), img_data_[i].end(),  
  134.               std::ostream_iterator<int>(fout, "\n"));  
  135.   }  
  136.   
  137.   fout.close();  
  138. }  
  139.   
  140. int main(int argc, char **argv) {  
  141.   if (argc == 1) {  
  142.     std::cerr << "enter a pgm file on the command line" << std::endl;  
  143.     return 1;  
  144.   }  
  145.   else if (argc == 2) {  
  146.     std::cerr << "enter a output file name on the command line" << std::endl;  
  147.     return 1;      
  148.   }  
  149.     
  150.   Image img(argv[1], argv[2]);  
  151.   img.doCorrect();  
  152.   img.outputImage();  
  153.   
  154.   return 0;  
  155. }  
2011年12月25日にちょっと修正<-

0 件のコメント:

コメントを投稿