File: 2dlerp.cc

package info (click to toggle)
mia 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,532 kB
  • ctags: 16,800
  • sloc: cpp: 137,909; python: 1,057; ansic: 998; sh: 146; xml: 127; csh: 24; makefile: 13
file content (205 lines) | stat: -rw-r--r-- 5,874 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* -*- mia-c++  -*-
 *
 * This file is part of MIA - a toolbox for medical image analysis 
 * Copyright (c) Leipzig, Madrid 1999-2014 Gert Wollny
 *
 * MIA is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <climits>
#include <boost/test/unit_test_suite.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

#include <mia/core.hh>
#include <mia/internal/main.hh>
#include <mia/2d.hh>
#include <mia/2d/filter.hh>
#include <sstream>
#include <iomanip>

NS_MIA_USE
using namespace boost;
using namespace std;

const SProgramDescription g_description = {
        {pdi_group, "Analysis, filtering, combining, and segmentation of 2D images"}, 
	{pdi_short, "Linearly combine two 2D images."}, 
	{pdi_description, "Merge two images by pixel-wise linearly combining their intensities."}, 
	{pdi_example_descr, "Combine image inputA.v and inputB.v by using position coordinates "
	 "4, 7, and 9 and write the result to output.v"}, 
	{pdi_example_code, "-1 inputA.v -2 inputB.v -p 4,7,9 -o output.v"}
}; 


struct FAddWeighted: public TFilter<P2DImage> {
	FAddWeighted(float w):
		m_w(w)
		{
		}
	
	template <typename T, typename S>
	P2DImage operator() (const T2DImage<T>& a, const T2DImage<S>& b) const	{
		if (a.get_size() != b.get_size()) {
			throw invalid_argument("input images cann not be combined because they differ in size");
		}
		T2DImage<T> *result = new T2DImage<T>(a.get_size());
		typename T2DImage<T>::iterator r = result->begin();
		typename T2DImage<T>::iterator e = result->end();
		
		typename T2DImage<T>::const_iterator ia = a.begin();
		typename T2DImage<S>::const_iterator ib = b.begin();
			
		float w2 = 1.0 - m_w;
		
		while ( r != e ) {
			*r = (T)(w2 * *ia + m_w * (float)*ib);
			cvdebug() << w2 << " * " <<*ia << " + " << m_w <<" * " <<  (float)*ib  << "->" << *r << "\n";
			++r;
			++ia;
			++ib;
		}
			
		return P2DImage(result);
	}
	
private:
	float m_w;
};

static void run_self_test()
{
	const C2DBounds size(1,2);
	
	C2DFImage *A = new C2DFImage(size);
	C2DUBImage *B = new C2DUBImage(size);
	
	P2DImage pA(A);
	P2DImage pB(B);
	
	(*A)(0,0) = 1.0; (*A)(0,1) = 2.0;
	(*B)(0,0) = 3.0; (*B)(0,1) = 4.0;
	
	FAddWeighted aw(0.25);
	
	P2DImage R = mia::filter(aw, *pA, *pB);
	
	
	const C2DFImage* result = dynamic_cast<const C2DFImage*>(R.get());
	BOOST_REQUIRE(result);
	
	BOOST_CHECK_CLOSE( (*result)(0,0), 1.5f, 0.1f);
	BOOST_CHECK_CLOSE( (*result)(0,1), 2.5f, 0.1f);
	
}

static bool init_unit_test_suite( )
{
	::boost::unit_test::framework::master_test_suite().add( BOOST_TEST_CASE( &run_self_test));
	return true;
}

template <typename F>
struct FFilter {
	FFilter(const F& f):
		m_f(f)
		{
		}
	
	P2DImage operator () (const P2DImage& a, const P2DImage& b) const {
		return ::mia::filter(m_f, *a, *b);
	}
private:
	const F& m_f;
};

int do_main(int argc, char **argv)
{

	CCmdOptionList options(g_description);
	string src1_filename;
	string src2_filename;
	string out_filename;
	bool self_test = false;

	vector<float> positions;

	const auto& imageio = C2DImageIOPluginHandler::instance();

	options.add(make_opt( src1_filename, "first", '1', "first input image ", 
			      CCmdOptionFlags::required_input, &imageio));
	options.add(make_opt( src2_filename, "second", '2', "second input image ", 
			      CCmdOptionFlags::required_input, &imageio));
	options.add(make_opt( out_filename, "out-file", 'o', "output vector field", 
			      CCmdOptionFlags::required_output, &imageio));
	options.add(make_opt( positions, "positions", 'p', 
			      "image series positions (first, target, second)", 
			      CCmdOptionFlags::required));
	options.add(make_opt( self_test, "self-test", 0, "run a self test of the tool"));
	
	if (options.parse(argc, argv) != CCmdOptionList::hr_no)
		return EXIT_SUCCESS; 

	if (self_test) {
		return ::boost::unit_test::unit_test_main( &init_unit_test_suite, argc, (char **)argv );
	}

	if (positions.size() != 3) {
		stringstream msg;
		msg << "positions must be 3 values: first, target, second, got " << positions.size() << "images";
		throw invalid_argument(msg.str());
	}

	C2DImageIOPlugin::PData source1 = imageio.load(src1_filename);
	C2DImageIOPlugin::PData source2 = imageio.load(src2_filename);


	if (!source1 || source1->size() < 1) {
		cerr << "no image found in " << src1_filename << "\n";
		return EXIT_FAILURE;
	}

	if (!source2 || source2->size() < 1) {
		cerr << "no image found in " << src2_filename << "\n";
		return EXIT_FAILURE;
	}

	float w = (positions[1] - positions[0]) / (positions[2] - positions[0]);



	if (source1->size() != source2->size())
		cvwarn() << "Number of images differ, only combining first " 
			 << (source1->size() < source2->size() ? source1->size() : source2->size()) << "images\n";

	if (source1->size() <= source2->size())
		transform( source1->begin(), source1->end(), source2->begin(), 
			   source1->begin(), FFilter<FAddWeighted>(FAddWeighted(w)));
	else {
		transform( source2->begin(), source2->end(), source1->begin(), 
			   source2->begin(), FFilter<FAddWeighted>(FAddWeighted(w)));
		source1 = source2;
	}

	// save the result;
	if ( !imageio.save(out_filename, *source1) ){
		string not_save = ("unable to save result to ") + out_filename;
		throw runtime_error(not_save);
	};

	return EXIT_SUCCESS;
}

MIA_MAIN(do_main);