d2ca7f40099789e1b513dacf450d0573eb5be724
[platform/upstream/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 #include "opencv2/nonfree/nonfree.hpp"
15 #include "opencv2/legacy/legacy.hpp"
16 #include "opencv2/legacy/compat.hpp"
17
18 #include <string>
19 #include <stdio.h>
20
21 static void help()
22 {
23     printf("\nThis program demonstrates the one way interest point descriptor found in features2d.hpp\n"
24             "Correspondences are drawn\n");
25     printf("Format: \n./one_way_sample <path_to_samples> <image1> <image2>\n");
26     printf("For example: ./one_way_sample . ../c/scene_l.bmp ../c/scene_r.bmp\n");
27 }
28
29 using namespace std;
30 using namespace cv;
31
32 Mat DrawCorrespondences(const Mat& img1, const vector<KeyPoint>& features1, const Mat& img2,
33                         const vector<KeyPoint>& features2, const vector<int>& desc_idx);
34
35 int main(int argc, char** argv)
36 {
37     const char images_list[] = "one_way_train_images.txt";
38     const CvSize patch_size = cvSize(24, 24);
39     const int pose_count = 50;
40
41     if (argc != 4)
42     {
43         help();
44         return 0;
45     }
46
47     std::string path_name = argv[1];
48     std::string img1_name = path_name + "/" + std::string(argv[2]);
49     std::string img2_name = path_name + "/" + std::string(argv[3]);
50
51     printf("Reading the images...\n");
52     Mat img1 = imread(img1_name, IMREAD_GRAYSCALE);
53     Mat img2 = imread(img2_name, IMREAD_GRAYSCALE);
54
55     // extract keypoints from the first image
56     SURF surf_extractor(5.0e3);
57     vector<KeyPoint> keypoints1;
58
59     // printf("Extracting keypoints\n");
60     surf_extractor(img1, Mat(), keypoints1);
61
62     printf("Extracted %d keypoints...\n", (int)keypoints1.size());
63
64     printf("Training one way descriptors... \n");
65     // create descriptors
66     OneWayDescriptorBase descriptors(patch_size, pose_count, OneWayDescriptorBase::GetPCAFilename(), path_name,
67                                      images_list);
68     IplImage img1_c = img1;
69     IplImage img2_c = img2;
70     descriptors.CreateDescriptorsFromImage(&img1_c, keypoints1);
71     printf("done\n");
72
73     // extract keypoints from the second image
74     vector<KeyPoint> keypoints2;
75     surf_extractor(img2, Mat(), keypoints2);
76     printf("Extracted %d keypoints from the second image...\n", (int)keypoints2.size());
77
78     printf("Finding nearest neighbors...");
79     // find NN for each of keypoints2 in keypoints1
80     vector<int> desc_idx;
81     desc_idx.resize(keypoints2.size());
82     for (size_t i = 0; i < keypoints2.size(); i++)
83     {
84         int pose_idx = 0;
85         float distance = 0;
86         descriptors.FindDescriptor(&img2_c, keypoints2[i].pt, desc_idx[i], pose_idx, distance);
87     }
88     printf("done\n");
89
90     Mat img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, desc_idx);
91
92     imshow("correspondences", img_corr);
93     waitKey(0);
94 }
95
96 Mat DrawCorrespondences(const Mat& img1, const vector<KeyPoint>& features1, const Mat& img2,
97                         const vector<KeyPoint>& features2, const vector<int>& desc_idx)
98 {
99     Mat part, img_corr(Size(img1.cols + img2.cols, MAX(img1.rows, img2.rows)), CV_8UC3);
100     img_corr = Scalar::all(0);
101     part = img_corr(Rect(0, 0, img1.cols, img1.rows));
102     cvtColor(img1, part, COLOR_GRAY2RGB);
103     part = img_corr(Rect(img1.cols, 0, img2.cols, img2.rows));
104     cvtColor(img1, part, COLOR_GRAY2RGB);
105
106     for (size_t i = 0; i < features1.size(); i++)
107     {
108         circle(img_corr, features1[i].pt, 3, CV_RGB(255, 0, 0));
109     }
110
111     for (size_t i = 0; i < features2.size(); i++)
112     {
113         Point pt((int)features2[i].pt.x + img1.cols, (int)features2[i].pt.y);
114         circle(img_corr, pt, 3, Scalar(0, 0, 255));
115         line(img_corr, features1[desc_idx[i]].pt, pt, Scalar(0, 255, 0));
116     }
117
118     return img_corr;
119 }