lot's of changes; nonfree & photo modules added; SIFT & SURF -> nonfree module; Inpai...
[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 #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 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 cv;
30
31 Mat DrawCorrespondences(const Mat& img1, const vector<KeyPoint>& features1, const Mat& img2,
32                         const vector<KeyPoint>& features2, const vector<int>& desc_idx);
33
34 int main(int argc, char** argv)
35 {
36     const char images_list[] = "one_way_train_images.txt";
37     const CvSize patch_size = cvSize(24, 24);
38     const int pose_count = 50;
39
40     if (argc != 4)
41     {
42         help();
43         return 0;
44     }
45
46     std::string path_name = argv[1];
47     std::string img1_name = path_name + "/" + std::string(argv[2]);
48     std::string img2_name = path_name + "/" + std::string(argv[3]);
49
50     printf("Reading the images...\n");
51     Mat img1 = imread(img1_name, CV_LOAD_IMAGE_GRAYSCALE);
52     Mat img2 = imread(img2_name, CV_LOAD_IMAGE_GRAYSCALE);
53     
54     // extract keypoints from the first image
55     SURF surf_extractor(5.0e3);
56     vector<KeyPoint> keypoints1;
57
58     // printf("Extracting keypoints\n");
59     surf_extractor(img1, Mat(), keypoints1);
60     
61     printf("Extracted %d keypoints...\n", (int)keypoints1.size());
62
63     printf("Training one way descriptors... \n");
64     // create descriptors
65     OneWayDescriptorBase descriptors(patch_size, pose_count, OneWayDescriptorBase::GetPCAFilename(), path_name,
66                                      images_list);
67     IplImage img1_c = img1;
68     IplImage img2_c = img2;
69     descriptors.CreateDescriptorsFromImage(&img1_c, keypoints1);
70     printf("done\n");
71
72     // extract keypoints from the second image
73     vector<KeyPoint> keypoints2;
74     surf_extractor(img2, Mat(), keypoints2);
75     printf("Extracted %d keypoints from the second image...\n", (int)keypoints2.size());
76
77     printf("Finding nearest neighbors...");
78     // find NN for each of keypoints2 in keypoints1
79     vector<int> desc_idx;
80     desc_idx.resize(keypoints2.size());
81     for (size_t i = 0; i < keypoints2.size(); i++)
82     {
83         int pose_idx = 0;
84         float distance = 0;
85         descriptors.FindDescriptor(&img2_c, keypoints2[i].pt, desc_idx[i], pose_idx, distance);
86     }
87     printf("done\n");
88
89     Mat img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, desc_idx);
90
91     imshow("correspondences", img_corr);
92     waitKey(0);
93 }
94
95 Mat DrawCorrespondences(const Mat& img1, const vector<KeyPoint>& features1, const Mat& img2,
96                         const vector<KeyPoint>& features2, const vector<int>& desc_idx)
97 {
98     Mat part, img_corr(Size(img1.cols + img2.cols, MAX(img1.rows, img2.rows)), CV_8UC3);
99     img_corr = Scalar::all(0);
100     part = img_corr(Rect(0, 0, img1.cols, img1.rows));
101     cvtColor(img1, part, COLOR_GRAY2RGB);
102     part = img_corr(Rect(img1.cols, 0, img2.cols, img2.rows));
103     cvtColor(img1, part, COLOR_GRAY2RGB);
104     
105     for (size_t i = 0; i < features1.size(); i++)
106     {
107         circle(img_corr, features1[i].pt, 3, CV_RGB(255, 0, 0));
108     }
109     
110     for (size_t i = 0; i < features2.size(); i++)
111     {
112         Point pt((int)features2[i].pt.x + img1.cols, (int)features2[i].pt.y);
113         circle(img_corr, pt, 3, Scalar(0, 0, 255));
114         line(img_corr, features1[desc_idx[i]].pt, pt, Scalar(0, 255, 0));
115     }
116     
117     return img_corr;
118 }