converted some more samples to C++
[profile/ivi/opencv.git] / samples / c / one_way_sample.cpp
1 /*
2  *  one_way_sample.cpp
3  *  outlet_detection
4  *
5  *  Created by Victor  Eruhimov on 8/5/09.
6  *  Copyright 2009 Argus Corp. All rights reserved.
7  *
8  */
9
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"
14
15 #include <string>
16
17 using namespace cv;
18
19 IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
20                               const vector<KeyPoint>& features2, const vector<int>& desc_idx);
21
22 int main(int argc, char** argv)
23 {
24     const char images_list[] = "one_way_train_images.txt";
25     const CvSize patch_size = cvSize(24, 24);
26     const int pose_count = 50;
27
28     if (argc != 3 && argc != 4)
29     {
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");
32         return 0;
33     }
34
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]);
38
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);
42     
43     // extract keypoints from the first image
44     SURF surf_extractor(5.0e3);
45     vector<KeyPoint> keypoints1;
46
47     // printf("Extracting keypoints\n");
48     surf_extractor(img1, Mat(), keypoints1);
49     
50     printf("Extracted %d keypoints...\n", (int)keypoints1.size());
51
52     printf("Training one way descriptors... \n");
53     // create descriptors
54     OneWayDescriptorBase descriptors(patch_size, pose_count, OneWayDescriptorBase::GetPCAFilename(), path_name,
55                                      images_list);
56     descriptors.CreateDescriptorsFromImage(img1, keypoints1);
57     printf("done\n");
58
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());
63
64     printf("Finding nearest neighbors...");
65     // find NN for each of keypoints2 in keypoints1
66     vector<int> desc_idx;
67     desc_idx.resize(keypoints2.size());
68     for (size_t i = 0; i < keypoints2.size(); i++)
69     {
70         int pose_idx = 0;
71         float distance = 0;
72         descriptors.FindDescriptor(img2, keypoints2[i].pt, desc_idx[i], pose_idx, distance);
73     }
74     printf("done\n");
75
76     IplImage* img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, desc_idx);
77
78     cvNamedWindow("correspondences", 1);
79     cvShowImage("correspondences", img_corr);
80     cvWaitKey(0);
81
82     cvReleaseImage(&img1);
83     cvReleaseImage(&img2);
84     cvReleaseImage(&img_corr);
85 }
86
87 IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
88                               const vector<KeyPoint>& features2, const vector<int>& desc_idx)
89 {
90     IplImage* img_corr = cvCreateImage(cvSize(img1->width + img2->width, MAX(img1->height, img2->height)),
91                                        IPL_DEPTH_8U, 3);
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);
97
98     for (size_t i = 0; i < features1.size(); i++)
99     {
100         cvCircle(img_corr, features1[i].pt, 3, CV_RGB(255, 0, 0));
101     }
102
103     for (size_t i = 0; i < features2.size(); i++)
104     {
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));
108     }
109
110     return img_corr;
111 }