OpenCV ホモグラフィー変換の断片 | i-mk2!

i-mk2!

自分のための備忘録
OpenCVの使い方とか.

キーポイントマッチングの結果,良い対応点が取れた場合の処理.
テスト画像中に見つけた教師画像をラインで囲んで表示する.

以下ソースコード


//-- Localize the object
if( gMatches.size() >=15 /*&& gMatches.size() <= 30*/ )
{
drawMatches(img_in1, keyPoints_1, img_in2, keyPoints_2, gMatches, img_matched);

std::cout << "OBJECT FOUND!" << std::endl;
std::vector<cv::Point2f> obj;
std::vector<cv::Point2f> scene;

for( unsigned int i = 0; i < gMatches.size(); i++ )
{
//-- Get the keypoints from the good matches
obj.push_back( keyPoints_1[ gMatches[i].queryIdx ].pt );
scene.push_back( keyPoints_2[ gMatches[i].trainIdx ].pt );
}

cv::Mat H = findHomography( obj, scene, CV_RANSAC );


//-- Get the corners from the image_1 ( the object to be "detected" )
std::vector<cv::Point2f> obj_corners(4);
obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_in1.cols, 0 );
obj_corners[2] = cvPoint( img_in1.cols, img_in1.rows ); obj_corners[3] = cvPoint( 0, img_in1.rows );
std::vector<cv::Point2f> scene_corners(4);

perspectiveTransform( obj_corners, scene_corners, H);

//-- Draw lines between the corners (the mapped object in the scene - image_2 )
if(1)
{
cv::Mat roi( img_matched , cv::Rect( img_in1.cols, 0, img_in2.cols, img_in2.rows ) );
cv::line( roi, scene_corners[0] , scene_corners[1], cv::Scalar(255,255,0), 2 ); //TOP line
cv::line( roi, scene_corners[1] , scene_corners[2], color, 2 );
cv::line( roi, scene_corners[2] , scene_corners[3], color, 2 );
cv::line( roi, scene_corners[3] , scene_corners[0], color, 2 );
}
}

ソースおしまい