1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
void read();
int main( int argc, char** argv )
{
if( argc != 3 )
{
read(); return -1;
}
Mat image1 = imread(argv[1]);
Mat image2 = imread(argv[2]);
Mat image3 = imread(argv[3]);
Mat grey_image1;
Mat grey_image2;
Mat grey_image3;
cvtColor( image1, grey_image1, CV_RGB2GRAY );
cvtColor( image2, grey_image2, CV_RGB2GRAY );
cvtColor( image3, grey_image3, CV_RGB2GRAY );
imshow("first image",image1);
imshow("second image",image2);
imshow("third image",image3);
if(!grey_image1.data || !grey_image2.data || !grey_image3.data)
{
std::cout<< " --(!) Error reading images " << std::endl; return -1;
}
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std::vector< KeyPoint > keypoints_object, keypoints_scene;
detector.detect( grey_image1, keypoints_object );
detector.detect( grey_image2, keypoints_scene );
SurfDescriptorExtractor extractor;
Mat descriptors_object, descriptors_scene;
extractor.compute( grey_image1, keypoints_object, descriptors_object );
extractor.compute( grey_image2, keypoints_scene, descriptors_scene );
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );
double max_dist = 0; double min_dist = 100;
for( int i = 0; i < descriptors_object.rows; i++ )
{
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
printf("-- Max dist : %f \n", max_dist );
printf("-- Min dist : %f \n", min_dist );
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_object.rows; i++ )
{
if( matches[i].distance < 3*min_dist )
{
good_matches.push_back( matches[i]);
}
}
std::vector< Point2f > obj;
std::vector< Point2f > scene;
for( int i = 0; i < good_matches.size(); i++ )
{
obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
}
Mat H = findHomography( obj, scene, CV_RANSAC );
cv::Mat result;
warpPerspective(image1,result,H,cv::Size(image1.cols+image2.cols,image1.rows));
cv::Mat half(result,cv::Rect(0,0,image2.cols,image2.rows));
image2.copyTo(half);
imshow( "Result", result );
waitKey(0);
return 0;
}
void read()
{
std::cout << " Usage: Panorama < img1 > < img2 > <img3>" << std::endl;
}
| |