5 * Created by Victor Eruhimov on 8/5/09.
6 * Copyright 2009 Argus Corp. All rights reserved.
10 #include "opencv2/imgproc/imgproc.hpp"
11 #include "opencv2/features2d/features2d.hpp"
12 #include "opencv2/highgui/highgui.hpp"
13 #include "opencv2/imgproc/imgproc_c.h"
19 IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
20 const vector<KeyPoint>& features2, const vector<int>& desc_idx);
22 int main(int argc, char** argv)
24 const char images_list[] = "one_way_train_images.txt";
25 const CvSize patch_size = cvSize(24, 24);
26 const int pose_count = 50;
28 if (argc != 3 && argc != 4)
30 printf("Format: \n./one_way_sample [path_to_samples] [image1] [image2]\n");
31 printf("For example: ./one_way_sample ../../../opencv/samples/c scene_l.bmp scene_r.bmp\n");
35 std::string path_name = argv[1];
36 std::string img1_name = path_name + "/" + std::string(argv[2]);
37 std::string img2_name = path_name + "/" + std::string(argv[3]);
39 printf("Reading the images...\n");
40 IplImage* img1 = cvLoadImage(img1_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
41 IplImage* img2 = cvLoadImage(img2_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
43 // extract keypoints from the first image
44 SURF surf_extractor(5.0e3);
45 vector<KeyPoint> keypoints1;
47 // printf("Extracting keypoints\n");
48 surf_extractor(img1, Mat(), keypoints1);
50 printf("Extracted %d keypoints...\n", (int)keypoints1.size());
52 printf("Training one way descriptors... \n");
54 OneWayDescriptorBase descriptors(patch_size, pose_count, OneWayDescriptorBase::GetPCAFilename(), path_name,
56 descriptors.CreateDescriptorsFromImage(img1, keypoints1);
59 // extract keypoints from the second image
60 vector<KeyPoint> keypoints2;
61 surf_extractor(img2, Mat(), keypoints2);
62 printf("Extracted %d keypoints from the second image...\n", (int)keypoints2.size());
64 printf("Finding nearest neighbors...");
65 // find NN for each of keypoints2 in keypoints1
67 desc_idx.resize(keypoints2.size());
68 for (size_t i = 0; i < keypoints2.size(); i++)
72 descriptors.FindDescriptor(img2, keypoints2[i].pt, desc_idx[i], pose_idx, distance);
76 IplImage* img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, desc_idx);
78 cvNamedWindow("correspondences", 1);
79 cvShowImage("correspondences", img_corr);
82 cvReleaseImage(&img1);
83 cvReleaseImage(&img2);
84 cvReleaseImage(&img_corr);
87 IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
88 const vector<KeyPoint>& features2, const vector<int>& desc_idx)
90 IplImage* img_corr = cvCreateImage(cvSize(img1->width + img2->width, MAX(img1->height, img2->height)),
92 cvSetImageROI(img_corr, cvRect(0, 0, img1->width, img1->height));
93 cvCvtColor(img1, img_corr, CV_GRAY2RGB);
94 cvSetImageROI(img_corr, cvRect(img1->width, 0, img2->width, img2->height));
95 cvCvtColor(img2, img_corr, CV_GRAY2RGB);
96 cvResetImageROI(img_corr);
98 for (size_t i = 0; i < features1.size(); i++)
100 cvCircle(img_corr, features1[i].pt, 3, CV_RGB(255, 0, 0));
103 for (size_t i = 0; i < features2.size(); i++)
105 CvPoint pt = cvPoint(features2[i].pt.x + img1->width, features2[i].pt.y);
106 cvCircle(img_corr, pt, 3, CV_RGB(255, 0, 0));
107 cvLine(img_corr, features1[desc_idx[i]].pt, pt, CV_RGB(0, 255, 0));