5 * Created by Victor Eruhimov on 8/5/09.
6 * Copyright 2009 Argus Corp. All rights reserved.
10 #include "opencv2/core/core.hpp"
11 #include "opencv2/imgproc/imgproc.hpp"
12 #include "opencv2/features2d/features2d.hpp"
13 #include "opencv2/highgui/highgui.hpp"
14 #include "opencv2/imgproc/imgproc_c.h"
20 printf("\nThis program demonstrates the one way interest point descriptor found in features2d.hpp\n"
21 "Correspondences are drawn\n");
22 printf("Format: \n./one_way_sample <path_to_samples> <image1> <image2>\n");
23 printf("For example: ./one_way_sample --path=../../../opencv/samples/c --first_image=scene_l.bmp --second_image=scene_r.bmp\n");
28 IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
29 const vector<KeyPoint>& features2, const vector<int>& desc_idx);
31 int main(int argc, const char** argv)
35 CommandLineParser parser(argc, argv);
37 std::string path_name = parser.get<string>("path", "../../../opencv/samples/c");
38 std::string img1_name = path_name + "/" + parser.get<string>("first_image", "scene_l.bmp");
39 std::string img2_name = path_name + "/" + parser.get<string>("second_image", "scene_r.bmp");
41 const char images_list[] = "one_way_train_images.txt";
42 const CvSize patch_size = cvSize(24, 24);
43 const int pose_count = 1; //50
45 printf("Reading the images...\n");
46 IplImage* img1 = cvLoadImage(img1_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
47 IplImage* img2 = cvLoadImage(img2_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
49 // extract keypoints from the first image
50 SURF surf_extractor(5.0e3);
51 vector<KeyPoint> keypoints1;
53 // printf("Extracting keypoints\n");
54 surf_extractor(img1, Mat(), keypoints1);
56 printf("Extracted %d keypoints...\n", (int)keypoints1.size());
58 printf("Training one way descriptors... \n");
60 OneWayDescriptorBase descriptors(patch_size, pose_count, OneWayDescriptorBase::GetPCAFilename(), path_name,
62 descriptors.CreateDescriptorsFromImage(img1, keypoints1);
65 // extract keypoints from the second image
66 vector<KeyPoint> keypoints2;
67 surf_extractor(img2, Mat(), keypoints2);
68 printf("Extracted %d keypoints from the second image...\n", (int)keypoints2.size());
70 printf("Finding nearest neighbors...");
71 // find NN for each of keypoints2 in keypoints1
73 desc_idx.resize(keypoints2.size());
74 for (size_t i = 0; i < keypoints2.size(); i++)
78 descriptors.FindDescriptor(img2, keypoints2[i].pt, desc_idx[i], pose_idx, distance);
82 IplImage* img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, desc_idx);
84 cvNamedWindow("correspondences", 1);
85 cvShowImage("correspondences", img_corr);
88 cvReleaseImage(&img1);
89 cvReleaseImage(&img2);
90 cvReleaseImage(&img_corr);
93 IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
94 const vector<KeyPoint>& features2, const vector<int>& desc_idx)
96 IplImage* img_corr = cvCreateImage(cvSize(img1->width + img2->width, MAX(img1->height, img2->height)),
98 cvSetImageROI(img_corr, cvRect(0, 0, img1->width, img1->height));
99 cvCvtColor(img1, img_corr, CV_GRAY2RGB);
100 cvSetImageROI(img_corr, cvRect(img1->width, 0, img2->width, img2->height));
101 cvCvtColor(img2, img_corr, CV_GRAY2RGB);
102 cvResetImageROI(img_corr);
104 for (size_t i = 0; i < features1.size(); i++)
106 cvCircle(img_corr, features1[i].pt, 3, CV_RGB(255, 0, 0));
109 for (size_t i = 0; i < features2.size(); i++)
111 CvPoint pt = cvPoint((int)features2[i].pt.x + img1->width, (int)features2[i].pt.y);
112 cvCircle(img_corr, pt, 3, CV_RGB(255, 0, 0));
113 cvLine(img_corr, features1[desc_idx[i]].pt, pt, CV_RGB(0, 255, 0));