画像処理回路SIMの入力データ作成方法
はじめに
公開していますが編集中です。 画像処理回路を検証する際によく入力パターンが必要になると思います。 幾何学パターンであればプログラムで作ることが出来ますが、カメラで取得した画像をパターンとして流したい場合があります。 そこでOpenCVでpngやbmpを扱いやすいpgmに直すコードを作成しました。pgmはImageJで開くことが可能でかつ、 バイナリデータではないので編集しやすくお勧めです。
環境
- Visual Studio 2015
- OpenCV 3.1
- ImageJ 1.50i
入力画像例
プログラム
#include <iostream> #include <fstream> #include <string> #include <math.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #define H_SIZE 512 //画像横サイズ #define V_SIZE 512 //画像縦サイズ #define MAX_DP 8 //画素ビット数 #define IMG_CH 3 //チャネル数(RGB...3, Gray...1) using namespace std; using namespace cv; //argv[0]...入力ファイルパス(名) //argv[1]...出力ファイルパス(名) int main(int argc, char *argv[]) { //画像読み込み Mat img = imread(argv[1]); //ファイル出力 ofstream ofs(argv[2]); //ヘッダー書き込み if (IMG_CH == 1) { ofs << "P2" << endl; //チャネル数 } else if (IMG_CH == 3) { ofs << "P3" << endl; //チャネル数 } ofs << H_SIZE << " " << V_SIZE << endl; //横サイズ 縦サイズ ofs << pow(2,MAX_DP) - 1 << endl; //ビット数 //画素書き込み for (int i = 0; i < V_SIZE; i++) { for (int j = 0; j < H_SIZE; j++) { if (IMG_CH == 1) { ofs << to_string(img.data[i*H_SIZE * 3 + 3 * j]) << endl; //1chで間隔が空いている //ofs << to_string(img.data[i*H_SIZE + j]) << endl; //1chで間隔が詰まってる } else if (IMG_CH == 3) { ofs << to_string(img.data[i*H_SIZE*3 + 3 * j + 2]) << " "; ofs << to_string(img.data[i*H_SIZE*3 + 3 * j + 1]) << " "; ofs << to_string(img.data[i*H_SIZE*3 + 3 * j]) << endl; } } } return 0; }
以下のようにバッチファイルで流せるので一度の操作で変換出来ると思います。
~.exe img1.png img1.pgm ~.exe img2.png img2.pgm ~.exe img3.png img3.pgm