魚眼カメラの画像をテクスチャ画像に変換
魚眼カメラの画像をテクスチャ画像に変換する Octave のプログラム例を示す.
前準備
Octave のインストールが済んでいること.
必見 Web ページ: http://www.csse.uwa.edu.au/~pk/Research/MatlabFns/
必見 Web ページ: http://www.eecs.berkeley.edu/Research/Projects/CS/vision/bsds/
* プロンプトを変えたいときは,次のように操作する.
PS1('> ')
* Octave のインストールによっては,Octave の起動時に毎回次の操作を行う必要があるかもしれない
pkg load image
プログラムのソースコード
function rgb = img_interp ( img, x, y )
# rgb 画像 img の線形補間を行い,点 (x, y) の画素値を求める
x0 = floor(x);
y0 = floor(y);
x1 = ceil(x);
y1 = ceil(y);
px = x - x0;
py = y - y0;
rgb1 = double(img(y0, x0, :));
rgb2 = double(img(y1, x0, :));
rgb3 = double(img(y0, x1, :));
rgb4 = double(img(y1, x1, :));
rgb = uint8( (1-py) * ((1-px) * rgb1 + px * rgb3)
+ py * ((1-px) * rgb2 + px * rgb4));
endfunction
function texture_image = fisheye2texture( fisheye_image, R )
# 魚眼カメラ画像をテクスチャ画像に変換
fisheye_height = size(fisheye_image)(1);
fisheye_width = size(fisheye_image)(2);
a = (fisheye_width + 1)/2;
b = (fisheye_height + 1)/2;
texture_width = round(R*2*pi);
for i = 1:texture_width
angX = double(i * (2*pi/texture_width));
for j = 1:R
angR = double(j * (pi/(R*4)));
r = R*sqrt(2)*sin(angR);
px = a+r*cos(-angX+pi/2);
py = b+r*sin(-angX+pi/2);
texture_image(j,i,:) = img_interp(fisheye_image, px, py);
end
fdisp(stderr, i);
end
endfunction
# 魚眼カメラの画像ファイル名(入力)を in_path に,
# テクスチャの画像ファイル名(出力)を out_path に指定して使ってください.
fdisp(stderr, "reading");
rgb = imread(in_path);
* Octave 3.1.51 以上では「[rgb, immap, alpha] = imread(in_path);;」のようにしてもよい(上のプログラムのままでもよい).
fdisp(stderr, "done");
R = 785;
clear texture;
texture = fisheye2texture(rgb, R);
# Octave 3.2 系列
imwrite(texture, out_path);
# Octave 3.0 系列
# imwrite(out_path, texture(:,:,1), texture(:,:,2), texture(:,:,3) );
実行方法と実行結果の例
- (オプション)Cygwin からリモートログインする場合
startx xhost + ssh -X username@ipaddress
- ソースコードをファイルとして保存
- Octave を起動
- Octave の source コマンドを使って実行
cd <ソースコードのファイルを置いたディレクトリ> source "<ソースコードのファイル名>"
- 実行が終わるまで数分待つ