update help according single standart for it
[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/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"
15
16 #include <string>
17
18 void help()
19 {
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");
24 }
25
26 using namespace cv;
27
28 IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
29                               const vector<KeyPoint>& features2, const vector<int>& desc_idx);
30
31 int main(int argc, const char** argv)
32 {
33     help();
34
35     CommandLineParser parser(argc, argv);
36
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");
40
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
44
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);
48     
49     // extract keypoints from the first image
50     SURF surf_extractor(5.0e3);
51     vector<KeyPoint> keypoints1;
52
53     // printf("Extracting keypoints\n");
54     surf_extractor(img1, Mat(), keypoints1);
55     
56     printf("Extracted %d keypoints...\n", (int)keypoints1.size());
57
58     printf("Training one way descriptors... \n");
59     // create descriptors
60     OneWayDescriptorBase descriptors(patch_size, pose_count, OneWayDescriptorBase::GetPCAFilename(), path_name,
61                                      images_list);
62     descriptors.CreateDescriptorsFromImage(img1, keypoints1);
63     printf("done\n");
64
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());
69
70     printf("Finding nearest neighbors...");
71     // find NN for each of keypoints2 in keypoints1
72     vector<int> desc_idx;
73     desc_idx.resize(keypoints2.size());
74     for (size_t i = 0; i < keypoints2.size(); i++)
75     {
76         int pose_idx = 0;
77         float distance = 0;
78         descriptors.FindDescriptor(img2, keypoints2[i].pt, desc_idx[i], pose_idx, distance);
79     }
80     printf("done\n");
81
82     IplImage* img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, desc_idx);
83
84     cvNamedWindow("correspondences", 1);
85     cvShowImage("correspondences", img_corr);
86     cvWaitKey(0);
87
88     cvReleaseImage(&img1);
89     cvReleaseImage(&img2);
90     cvReleaseImage(&img_corr);
91 }
92
93 IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
94                               const vector<KeyPoint>& features2, const vector<int>& desc_idx)
95 {
96     IplImage* img_corr = cvCreateImage(cvSize(img1->width + img2->width, MAX(img1->height, img2->height)),
97                                        IPL_DEPTH_8U, 3);
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);
103
104     for (size_t i = 0; i < features1.size(); i++)
105     {
106         cvCircle(img_corr, features1[i].pt, 3, CV_RGB(255, 0, 0));
107     }
108
109     for (size_t i = 0; i < features2.size(); i++)
110     {
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));
114     }
115
116     return img_corr;
117 }