Home » Source Code » UAV aerial video mosaics » mydetector.cpp

mydetector.cpp ( File view )

  • By YU 2014-05-21
  • View(s):91
  • Download(s):18
  • Point(s): 3
			/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                          License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "stdafx.h"
#include "mydetector.h"



#ifdef HAVE_OPENCV_GPU
using namespace cv::gpu;
#endif

#ifdef HAVE_OPENCV_NONFREE


static bool makeUseOfNonfree = initModule_nonfree();
#endif

//using namespace cv;
//using cv::detail::ImageFeatures;
using namespace cv::gpu;
using namespace  my_namespace;

namespace {


//////////////////////////////////////////////////////////////////////////////
typedef set<pair<int,int> > MatchesSet;

//CPU or GPU optimal options
class CpuMatcher : public my_FeaturesMatcher
{

	public:
		CpuMatcher(float match_conf) : my_FeaturesMatcher(true), match_conf_(match_conf) {

}
		void match(const myImageFeatures &features1, const myImageFeatures &features2, MatchesInfo& matches_info);

	private:
		float match_conf_;

};

#ifdef HAVE_OPENCV_GPU
class GpuMatcher : public my_FeaturesMatcher
{

	public:
		GpuMatcher(float match_conf) : match_conf_(match_conf) {

}
		void match(const myImageFeatures &features1, const myImageFeatures &features2, MatchesInfo& matches_info);

		void collectGarbage();

	private:
		float match_conf_;
		GpuMat descriptors1_, descriptors2_;
		GpuMat train_idx_, distance_, all_dist_;
		vector< vector<DMatch> > pair_matches;

};
#endif

void CpuMatcher::match(const myImageFeatures &features1, const myImageFeatures &features2, MatchesInfo& matches_info)
{

		CV_Assert(features1.descriptors.type() == features2.descriptors.type());
		CV_Assert(features2.descriptors.depth() == CV_8U || features2.descriptors.depth() == CV_32F);

	#ifdef HAVE_TEGRA_OPTIMIZATION
		if (tegra::match2nearest(features1, features2, matches_info, match_conf_))
			return;
	#endif

		matches_info.matches.clear();

		Ptr<flann::IndexParams> indexParams = new flann::KDTreeIndexParams();
		Ptr<flann::SearchParams> searchParams = new flann::SearchParams();

		if (features2.descriptors.depth() == CV_8U)
		{

			indexParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);
			searchParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);
		
}

		FlannBasedMatcher matcher(indexParams, searchParams);
		vector< vector<DMatch> > pair_matches;
		MatchesSet matches;

		// Find 1->2 matches
		matcher.knnMatch(features1.descriptors, features2.descriptors, pair_matches, 2);
		for (size_t i = 0; i < pair_matches.size(); ++i)
		{

			if (pair_matches[i].size() < 2)
				continue;
			const DMatch& m0 = pair_matches[i][0];
			const DMatch& m1 = pair_matches[i][1];
			if (m0.distance < (1.f - match_conf_) * m1.distance)
			{

				matches_info.matches.push_back(m0);
				matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
			
}
		
}
		LOG("\n1->2 matches: " << matches_info.matches.size() << endl);

		// Find 2->1 matches
		pair_matches.clear();
		matcher.knnMatch(features2.descriptors, features1.descriptors, pair_matches, 2);
		for (size_t i = 0; i < pair_matches.size(); ++i)
		{

			if (pair_matches[i].size() < 2)
				continue;
			const DMatch& m0 = pair_matches[i][0];
			const DMatch& m1 = pair_matches[i][1];
			if (m0.distance < (1.f - match_conf_) * m1.distance)
				if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
					matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
		
}
		LOG("1->2 & 2->1 matches: " << matches_info.matches.size() << endl);

}

#ifdef HAVE_OPENCV_GPU
void GpuMatcher::match(const myImageFeatures &features1, const myImageFeatures &features2, MatchesInfo& matches_info)
{

		matches_info.matches.clear();

		ensureSizeIsEnough(features1.descriptors.size(), features1.descriptors.type(), descriptors1_);
		ensureSizeIsEnough(features2.descriptors.size(), features2.descriptors.type(), descriptors2_);

		descriptors1_.upload(features1.descriptors);
		descriptors2_.upload(features2.descriptors);

		BruteForceMatcher_GPU_base matcher(BruteForceMatcher_GPU_base::L2Dist);
		MatchesSet matches;

		// Find 1->2 matches
		pair_matches.clear();
		matcher.knnMatchSingle(descriptors1_, descriptors2_, train_idx_, distance_, all_dist_, 2);
		matcher.knnMatchDownload(train_idx_, distance_, pair_matches);
		for (size_t i = 0; i < pair_matches.size(); ++i)
		{

			if (pair_matches[i].size() < 2)
				continue;
			const DMatch& m0 = pair_matches[i][0];
			const DMatch& m1 = pair_matches[i][1];
			if (m0.distance < (1.f - match_conf_) * m1.distance)
			{

				matches_info.matches.push_back(m0);
				matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
			
}
		
}

		// Find 2->1 matches
		pair_matches.clear();
		matcher.knnMatchSingle(descriptors2_, descriptors1_, train_idx_, distance_, all_dist_, 2);
		matcher.knnMatchDownload(train_idx_, distance_, pair_matches);
		for (size_t i = 0; i < pair_matches.size(); ++i)
		{

			if (pair_matches[i].size() < 2)
				continue;
			const DMatch& m0 = pair_matches[i][0];
			const DMatch& m1 = pair_matches[i][1];
			if (m0.distance < (1.f - match_conf_) * m1.distance)
				if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
					matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
		
}

}

void GpuMatcher::collectGarbage()
{

	descriptors1_.release();
	descriptors2_.release();
	train_idx_.release();
	distance_.release();
	all_dist_.release();
	vector< vector<DMatch> >().swap(pair_matches);

}
#endif
//CPU or GPU optimal options end;

} // namespace

namespace my_namespace{

/////////////////////////////////////////////////////////////////////////
//match
struct DistIdxPair
{

	bool operator<(const DistIdxPair &other) const {
 return dist < other.dist; 
}
	double dist;
	int idx;

};

struct MatchPairsBody
{

	MatchPairsBody(const MatchPairsBody& other)
		: matcher(other.matcher), features(other.features),
		pairwise_matches(other.pairwise_matches), near_pairs(other.near_pairs) {

}

	MatchPairsBody(my_FeaturesMatcher &_matcher, const vector<myImageFeatures> &_features,
		vector<MatchesInfo> &_pairwise_matches, vector<pair<int,int> > &_near_pairs)
		: matcher(_matcher), features(_features),
		pairwise_matches(_pairwise_matches), near_pairs(_near_pairs) {

}
	
	//FeatureMatcher 的核心匹配算法
	void operator ()(const BlockedRange &r) const
	{

		FileStorage fs(".\\match.xml", FileStorage::WRITE);
		//ofstream fout("match.txt");
		const int num_images = static_cast<int>(features.size());
		for (int i = r.begin(); i < r.end(); ++i)
		{

			int from = near_pairs[i].first;
			int to = near_pairs[i].second;
			int pair_idx = from*num_images + to;

			pairwise_matches[pair_idx].src_img_idx = from;
			pairwise_matches[pair_idx].dst_img_idx = to;
			matcher(features[from], features[to], pairwise_matches[pair_idx]);
			//pairwise_matches[pair_idx].src_img_idx = from;
			//pairwise_matches[pair_idx].dst_img_idx = to;

			size_t dual_pair_idx = to*num_images + from;

			pairwise_matches[dual_pair_idx] = pairwise_matches[pair_idx];
			pairwise_matches[dual_pair_idx].src_img_idx = to;
			pairwise_matches[dual_pair_idx].dst_img_idx = from;

			if (!pairwise_matches[pair_idx].H.empty())
				pairwise_matches[dual_pair_idx].H = pairwise_matches[pair_idx].H.inv();

			for (size_t j = 0; j < pairwise_matches[dual_pair_idx].matches.size(); ++j)
				std::swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx,
				pairwise_matches[dual_pair_idx].matches[j].trainIdx);
			printf(".");
			cout<<from<<to<<pairwise_matches[pair_idx].H<<endl;//在这里
			if (!pairwise_matches[pair_idx].H.empty())
			cout<<to<<from<<pairwise_matches[pair_idx].H.inv()<<endl;
			fs<<"from"<<pairwise_matches[pair_idx].H;			
		
}
		//fout.flush();fout.close();
		fs.release();
		printf("  done!");
	
}

	my_FeaturesMatcher &matcher;
	const vector<myImageFeatures> &features;
	vector<MatchesInfo> &pairwise_matches;
	vector<pair<int,int> > &near_pairs;

private:
	void operator =(const MatchPairsBody&);

};

//bundle adjustment
struct IncDistance
{

	IncDistance(vector<int> &vdists) : dists(&vdists[0]) {

}
	void operator ()(const GraphEdge &edge) {
 dists[edge.to] = dists[edge.from] + 1; 
}
	int* dists;

};


struct CalcRotation
{

	CalcRotation(int _num_images, const vector<MatchesInfo> &_pairwise_matches, vector<my_CameraParams> &_cameras)
		: num_images(_num_images), pairwise_matches(&_pairwise_matches[0]), cameras(&_cam
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 3

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
autostitch.aps17.46 kB28-02-13 07:50
autostitch.cpp1.53 kB05-11-13 09:50
autostitch.h39.00 B10-12-12 20:48
autostitch.rc1.34 kB10-12-12 20:48
autostitch.vcproj5.58 kB05-11-13 09:52
autostitch.vcproj.HUANG.shui.user1.37 kB09-06-13 09:26
autostitch.vcproj.lenovo-PC.lenovo.user1.38 kB15-12-12 21:35
autostitch.vcproj.SHUI.shui.user1.37 kB05-11-13 09:58
autostitch.vcproj.WIN-B5OD78NL8FV.Administrator.user1.39 kB25-07-13 08:10
autostitch.vcproj.WM-20120911FOZO.shui.user1.39 kB17-12-12 08:38
autostitch.vcproj.WM-20140502VVLE.Administrator.user1.39 kB09-05-14 15:37
confidence.txt90.59 kB25-07-13 10:02
confidence14da.txt6.31 kB25-06-13 18:51
confidence200.txt1.92 MB23-06-13 23:18
confidence5.txt1.37 kB24-06-13 14:02
autostitch.pch32.25 MB05-11-13 09:54
BuildLog.htm77.02 kB05-11-13 09:54
stdafx.obj999.54 kB05-11-13 09:54
vc90.idb315.00 kB05-11-13 09:54
vc90.pdb2.55 MB05-11-13 09:54
Jacbion.xml60.00 B25-07-13 18:23
match.txt4.73 kB25-07-13 10:02
match.xml108.03 kB25-07-13 10:02
match14da.txt1.61 kB25-06-13 19:01
match200.txt8.56 kB24-06-13 08:25
match5.txt212.00 B24-06-13 15:00
match_features.txt5.16 kB25-07-13 10:02
match_features01.txt4.85 kB24-06-13 21:36
match_features14da.txt360.76 kB25-06-13 18:51
mat_xml.xml30.00 kB21-06-13 10:17
MyCompile.cpp71.44 kB27-07-13 14:30
MyCompile.h3.08 kB25-06-13 21:38
mydetector.cpp49.80 kB05-11-13 09:54
mydetector.h8.91 kB15-07-13 19:14
precomp.h2.47 kB28-07-12 06:59
qian.xml48.31 kB25-07-13 18:23
ReadMe.txt1.63 kB10-12-12 20:48
Resource.h393.00 B10-12-12 20:48
result14.00 B97% 14-07-13
result.jpg2.23 MB09-07-13 19:35
result0.jpg2.54 MB30-05-13 11:05
result00.jpg680.30 kB31-05-13 20:12
result10.jpg56.18 kB31-05-13 14:29
result10_.jpg59.45 kB31-05-13 14:35
result14.jpg1.63 MB22-06-13 12:22
result141.jpg748.67 kB25-06-13 18:57
result18.jpg77.57 kB17-06-13 18:26
result19.jpg322.94 kB19-06-13 03:36
result2.jpg1.69 MB30-05-13 18:21
result21.jpg483.43 kB30-05-13 19:12
result26.jpg375.77 kB15-07-13 13:32
<result27>0.00 B96% 24-07-13
result27.jpg2.23 MB15-07-13 20:02
result30.jpg2.52 MB24-07-13 19:25
resultff.jpg28.84 kB31-05-13 14:40
resultguangsu.jpg402.75 kB21-06-13 10:17
resultmulti.jpg366.37 kB30-05-13 16:39
result冲投影.jpg298.42 kB21-06-13 11:05
stdafx.cpp215.00 B10-12-12 20:48
stdafx.h813.00 B10-12-12 20:48
targetver.h1.00 kB10-12-12 20:48
TestFuc.cpp847.00 B23-05-13 10:54
TestFuc.h248.00 B07-05-13 09:01
transform.xml60.00 B25-07-13 18:23
result.jpg598.37 kB24-05-13 08:22
Thumbs.db12.50 kB31-05-13 09:46
发大水.jpg294.10 kB24-05-13 21:51
没有强行赋值.jpg363.02 kB24-05-13 10:37
<重投影>0.00 B99% 24-05-13
autostitch.ncb17.58 MB09-05-14 15:37
autostitch.sln896.00 B10-12-12 20:48
autostitch.suo138.00 kB09-05-14 15:37
<autostitch.ilk>0.00 B27-07-13 14:30
confidence.txt5.91 kB13-07-13 10:22
Jacbion.xml60.00 B22-06-13 08:20
match.txt1.34 kB13-07-13 10:22
match.xml13.32 kB13-07-13 10:22
match_features.txt21.78 kB13-07-13 10:22
mat_xml.xml1.14 MB21-06-13 09:54
qian.xml13.34 kB22-06-13 08:20
result.jpg1.11 kB22-06-13 08:20
result5.jpg297.38 kB21-06-13 09:54
<transform.xml>0.00 B13-07-13 10:12
readme.txt192.00 B24-05-13 14:11
<Debug>0.00 B11-01-14 07:12
<拼接结果>0.00 B11-01-14 07:12
<autostitch>0.00 B11-01-14 07:12
<Debug>0.00 B11-01-14 07:12
<autostitch_0610>0.00 B09-05-14 15:35
...
Sponsored links

mydetector.cpp (34.39 MB)

Need 3 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D