2010-05-11 19:44:00 +02:00
|
|
|
/*
|
|
|
|
* one_way_sample.cpp
|
|
|
|
* outlet_detection
|
|
|
|
*
|
|
|
|
* Created by Victor Eruhimov on 8/5/09.
|
|
|
|
* Copyright 2009 Argus Corp. All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-26 18:59:40 +01:00
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/features2d/features2d.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc_c.h"
|
2012-03-15 15:36:01 +01:00
|
|
|
#include "opencv2/nonfree/nonfree.hpp"
|
|
|
|
#include "opencv2/legacy/legacy.hpp"
|
|
|
|
#include "opencv2/legacy/compat.hpp"
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
#include <string>
|
2011-07-14 14:50:07 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static void help()
|
2010-12-03 03:26:32 +01:00
|
|
|
{
|
2011-06-09 14:01:47 +02:00
|
|
|
printf("\nThis program demonstrates the one way interest point descriptor found in features2d.hpp\n"
|
|
|
|
"Correspondences are drawn\n");
|
2011-06-23 14:15:35 +02:00
|
|
|
printf("Format: \n./one_way_sample <path_to_samples> <image1> <image2>\n");
|
|
|
|
printf("For example: ./one_way_sample . ../c/scene_l.bmp ../c/scene_r.bmp\n");
|
2010-12-03 03:26:32 +01:00
|
|
|
}
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
Mat DrawCorrespondences(const Mat& img1, const vector<KeyPoint>& features1, const Mat& img2,
|
|
|
|
const vector<KeyPoint>& features2, const vector<int>& desc_idx);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
int main(int argc, char** argv)
|
2010-06-02 07:19:09 +02:00
|
|
|
{
|
2011-05-21 16:09:03 +02:00
|
|
|
const char images_list[] = "one_way_train_images.txt";
|
|
|
|
const CvSize patch_size = cvSize(24, 24);
|
2011-06-09 14:01:47 +02:00
|
|
|
const int pose_count = 50;
|
|
|
|
|
2011-06-23 14:15:35 +02:00
|
|
|
if (argc != 4)
|
2011-06-09 14:01:47 +02:00
|
|
|
{
|
|
|
|
help();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string path_name = argv[1];
|
|
|
|
std::string img1_name = path_name + "/" + std::string(argv[2]);
|
|
|
|
std::string img2_name = path_name + "/" + std::string(argv[3]);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
printf("Reading the images...\n");
|
2012-03-15 15:36:01 +01:00
|
|
|
Mat img1 = imread(img1_name, CV_LOAD_IMAGE_GRAYSCALE);
|
|
|
|
Mat img2 = imread(img2_name, CV_LOAD_IMAGE_GRAYSCALE);
|
2010-05-26 21:50:20 +02:00
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
// extract keypoints from the first image
|
|
|
|
SURF surf_extractor(5.0e3);
|
2010-05-26 21:50:20 +02:00
|
|
|
vector<KeyPoint> keypoints1;
|
2010-06-02 07:19:09 +02:00
|
|
|
|
|
|
|
// printf("Extracting keypoints\n");
|
2010-05-11 19:44:00 +02:00
|
|
|
surf_extractor(img1, Mat(), keypoints1);
|
2010-05-26 21:50:20 +02:00
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
printf("Extracted %d keypoints...\n", (int)keypoints1.size());
|
|
|
|
|
2010-06-02 07:19:09 +02:00
|
|
|
printf("Training one way descriptors... \n");
|
|
|
|
// create descriptors
|
|
|
|
OneWayDescriptorBase descriptors(patch_size, pose_count, OneWayDescriptorBase::GetPCAFilename(), path_name,
|
|
|
|
images_list);
|
2012-03-15 15:36:01 +01:00
|
|
|
IplImage img1_c = img1;
|
|
|
|
IplImage img2_c = img2;
|
|
|
|
descriptors.CreateDescriptorsFromImage(&img1_c, keypoints1);
|
2010-05-11 19:44:00 +02:00
|
|
|
printf("done\n");
|
2010-06-02 07:19:09 +02:00
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
// extract keypoints from the second image
|
|
|
|
vector<KeyPoint> keypoints2;
|
|
|
|
surf_extractor(img2, Mat(), keypoints2);
|
|
|
|
printf("Extracted %d keypoints from the second image...\n", (int)keypoints2.size());
|
|
|
|
|
|
|
|
printf("Finding nearest neighbors...");
|
|
|
|
// find NN for each of keypoints2 in keypoints1
|
|
|
|
vector<int> desc_idx;
|
|
|
|
desc_idx.resize(keypoints2.size());
|
2010-06-02 07:19:09 +02:00
|
|
|
for (size_t i = 0; i < keypoints2.size(); i++)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
int pose_idx = 0;
|
|
|
|
float distance = 0;
|
2012-03-15 15:36:01 +01:00
|
|
|
descriptors.FindDescriptor(&img2_c, keypoints2[i].pt, desc_idx[i], pose_idx, distance);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
printf("done\n");
|
2010-06-02 07:19:09 +02:00
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
Mat img_corr = DrawCorrespondences(img1, keypoints1, img2, keypoints2, desc_idx);
|
2010-06-02 07:19:09 +02:00
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
imshow("correspondences", img_corr);
|
|
|
|
waitKey(0);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
Mat DrawCorrespondences(const Mat& img1, const vector<KeyPoint>& features1, const Mat& img2,
|
|
|
|
const vector<KeyPoint>& features2, const vector<int>& desc_idx)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2012-03-15 15:36:01 +01:00
|
|
|
Mat part, img_corr(Size(img1.cols + img2.cols, MAX(img1.rows, img2.rows)), CV_8UC3);
|
|
|
|
img_corr = Scalar::all(0);
|
|
|
|
part = img_corr(Rect(0, 0, img1.cols, img1.rows));
|
|
|
|
cvtColor(img1, part, COLOR_GRAY2RGB);
|
|
|
|
part = img_corr(Rect(img1.cols, 0, img2.cols, img2.rows));
|
|
|
|
cvtColor(img1, part, COLOR_GRAY2RGB);
|
|
|
|
|
2010-06-02 07:19:09 +02:00
|
|
|
for (size_t i = 0; i < features1.size(); i++)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2012-03-15 15:36:01 +01:00
|
|
|
circle(img_corr, features1[i].pt, 3, CV_RGB(255, 0, 0));
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
2012-03-15 15:36:01 +01:00
|
|
|
|
2010-06-02 07:19:09 +02:00
|
|
|
for (size_t i = 0; i < features2.size(); i++)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2012-03-15 15:36:01 +01:00
|
|
|
Point pt((int)features2[i].pt.x + img1.cols, (int)features2[i].pt.y);
|
|
|
|
circle(img_corr, pt, 3, Scalar(0, 0, 255));
|
|
|
|
line(img_corr, features1[desc_idx[i]].pt, pt, Scalar(0, 255, 0));
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
2012-03-15 15:36:01 +01:00
|
|
|
|
2010-06-02 07:19:09 +02:00
|
|
|
return img_corr;
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|