OpenCV、電子工作、3Dプリンタなどいろいろやってます
はじめまして。いつも楽しみに拝見しています。私もOpenCVとOpenGLを組み合わせてARのようなものを作っているのですが、OpenGLの座標系の設定がうまくできません。cvFindExtrinsicCameraParams2()から得たrotationとtranslationからglLoadMatrixd()で設定していると思うのですが、glLoadMatrixd()にはどのような行列を設定しているのでしょうか?教えていただければ幸いです。
>rhythmistsさん返答が非常に遅れてしまいました。cvFindExtrinsicCameraParams2で得られるのはrotationベクトルとtranslationベクトルで、rotationが3x1のベクトルになっています。これを3x3の回転行列にするために、cvRodrigues2を使って3x3の行列に変換しました。そのあと、OpenGLの座標系の4x4行列を作って、なんとなく入れてみたら動いた、といった感じです。参考になるかはわかりませんが、そのあたりのコードを記載します。//※rotation、rotationにはcvFindExtrinsicCameraParams2で求めた//ベクトルが入っています。//配列に格納しなおすdouble _r1[3]; _r1[0]=cvmGet(rotation,0,0); _r1[1]=cvmGet(rotation,0,1); _r1[2]=cvmGet(rotation,0,2);double trans[3];trans[0]=cvmGet(translation,0,0);trans[1]=cvmGet(translation,0,1);trans[2]=cvmGet(translation,0,2);//3x3の回転行列を確保double Homograph[9]; CvMat r1 = cvMat(3,1,CV_64F,_r1);CvMat _Homograph = cvMat(3, 3, CV_64F, Homograph );//3x3に変換 cvRodrigues2(&r1, &_H);//ここからはOpenGLのdisplay()関数の中に実装してます//OpenGLの4x4を初期化double glMatrix[16] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; //回転行列を入れる glMatrix[0] = Homograph[0]; glMatrix[1] = Homograph[1]; glMatrix[2] = Homograph[2]; glMatrix[4] = Homograph[3]; glMatrix[5] = Homograph[4]; glMatrix[6] = Homograph[5]; glMatrix[8] = Homograph[6]; glMatrix[9] = Homograph[7]; glMatrix[10]= Homographh[8]; //移動ベクトルを入れる glMatrix[12]= trans[0]; glMatrix[13] =trans[1]; glMatrix[14] =trans[2]; glMultMatrixd(glMatrix);//ここまで。。また、下記ページを参考させて作ってみました。マーカー検出と3次元座標推定http://kato.keiske.info/blog/2009/05/3.html被写体を回そうとするとなぜかカメラが回ってしまう の続きhttp://kato.keiske.info/blog/2009/05/post-101.htmlモデルビュー変換http://wisdom.sakura.ne.jp/system/opengl/gl11.html
>lhaplus8888さんお返事ありがとうございます。OpenGLのオブジェクトを無事表示させることができました。コードまで載せていただき、ありがとうございます。これからも記事楽しみにしています!
はじめまして。いつも楽しみに拝見しています。
返信削除私もOpenCVとOpenGLを組み合わせてARのようなものを作っているのですが、OpenGLの座標系の設定がうまくできません。
cvFindExtrinsicCameraParams2()から得たrotationとtranslationからglLoadMatrixd()で設定していると思うのですが、glLoadMatrixd()にはどのような行列を設定しているのでしょうか?
教えていただければ幸いです。
>rhythmistsさん
返信削除返答が非常に遅れてしまいました。
cvFindExtrinsicCameraParams2で得られるのは
rotationベクトルとtranslationベクトルで、
rotationが3x1のベクトルになっています。
これを3x3の回転行列にするために、
cvRodrigues2を使って3x3の行列に変換しました。
そのあと、OpenGLの座標系の4x4行列を作って、
なんとなく入れてみたら動いた、といった感じです。
参考になるかはわかりませんが、
そのあたりのコードを記載します。
//※rotation、rotationにはcvFindExtrinsicCameraParams2で求めた
//ベクトルが入っています。
//配列に格納しなおす
double _r1[3];
_r1[0]=cvmGet(rotation,0,0);
_r1[1]=cvmGet(rotation,0,1);
_r1[2]=cvmGet(rotation,0,2);
double trans[3];
trans[0]=cvmGet(translation,0,0);
trans[1]=cvmGet(translation,0,1);
trans[2]=cvmGet(translation,0,2);
//3x3の回転行列を確保
double Homograph[9];
CvMat r1 = cvMat(3,1,CV_64F,_r1);
CvMat _Homograph = cvMat(3, 3, CV_64F, Homograph );
//3x3に変換
cvRodrigues2(&r1, &_H);
//ここからはOpenGLのdisplay()関数の中に実装してます
//OpenGLの4x4を初期化
double glMatrix[16] =
{
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
//回転行列を入れる
glMatrix[0] = Homograph[0];
glMatrix[1] = Homograph[1];
glMatrix[2] = Homograph[2];
glMatrix[4] = Homograph[3];
glMatrix[5] = Homograph[4];
glMatrix[6] = Homograph[5];
glMatrix[8] = Homograph[6];
glMatrix[9] = Homograph[7];
glMatrix[10]= Homographh[8];
//移動ベクトルを入れる
glMatrix[12]= trans[0];
glMatrix[13] =trans[1];
glMatrix[14] =trans[2];
glMultMatrixd(glMatrix);
//ここまで。。
また、下記ページを参考させて作ってみました。
マーカー検出と3次元座標推定
http://kato.keiske.info/blog/2009/05/3.html
被写体を回そうとするとなぜかカメラが回ってしまう の続き
http://kato.keiske.info/blog/2009/05/post-101.html
モデルビュー変換
http://wisdom.sakura.ne.jp/system/opengl/gl11.html
>lhaplus8888さん
返信削除お返事ありがとうございます。OpenGLのオブジェクトを無事表示させることができました。コードまで載せていただき、ありがとうございます。
これからも記事楽しみにしています!