[go: up one dir, main page]

Menu

[r994]: / libmona / src / crop3d.cc  Maximize  Restore  History

Download this file

330 lines (240 with data), 8.9 kB

  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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (c) 2004 Max-Planck-Institute Human Brain and Cognitive Science
*
* This program 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 2 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// $Id: crop3d.cc 938 2006-07-11 11:57:01Z write1 $
/*! \brief mona-crop -- crops an image or vector field
\par Description
This probram can be used to crop parts of an vector field or image.
\par Usage
<code>mona-crop3d --usage</code> (or <code> mona-crop --help</code>)
\param --in-file input image/vector field
\param --out-file output image/vector field
\param --start start of the crop area
\param --size size of the crop area
\par Examples
-# Crop a part of image starting from (1,2,3) with size (12,13,12)
\code
mona-crop -i image.v --start 1,2,3 --size 12,13,12 -o oimage.v
mona-crop -i image.v --start -10,2,-4 --size 30,13,12 -o oimage.v
\endcode
Note: The crop size can be larger then the input image - outside areas will be filled with zero, and cropping
can start "before" the actual image starts
\par Known bugs
\todo
\file crop3d.cc
\author G. Wollny, wollnyAtcbs.mpg.de, 2004
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
// MONA specific
#include <mona.hh>
using namespace std;
using namespace mona;
/* Provide revision string */
static const char *revision = "$Revision: 938 $";
// slow version to copy crop and possibly extend a 3D Datafield
template <class Data3D>
Data3D *crop3d (const Data3D& input, const T3DVector<int>& start, T3DVector<int> size)
{
T3DVector<int> ib = start;
T3DVector<int> kb = size;
int xi, yi, zi, xk, yk, zk, xyk;
typedef typename Data3D::value_type value_type;
cvdebug() << "Cropping an image of " << input.get_size()
<< " from " << start
<< " with size " << size << endl;
if (size.x < 0) size.x = input.get_size().x - start.x;
if (size.y < 0) size.y = input.get_size().y - start.y;
if (size.z < 0) size.z = input.get_size().z - start.z;
cvdebug() << "Corrected size is " << size << endl;
xyk = size.x * size.y;
Data3D *result = new Data3D(size, input.get_attribute_list());
// fill start if crop enlarges
for (zi = start.z, zk = 0; zk < size.z && zi < 0;++zi, ++zk) {
value_type *p = &(*result)(0, 0, zk);
fill( p, &p[xyk], value_type());
}
for (; zk < size.z && zi < (int)input.get_size().z; ++zi, ++zk) {
// fill empty rows
for (yi = start.y, yk = 0; yk < size.y && yi < 0;++yi, ++yk){
value_type *p = &(*result)(0, yk, zk);
fill(p , &p[size.x], value_type());
}
for (; yk < size.y && yi < (int)input.get_size().y; ++yi, ++yk) {
for (xi = start.x, xk = 0; xk < size.x && xi < 0;++xi, ++xk)
(*result)(xk, yk, zk) = value_type();
for (; xk < size.x && xi < (int)input.get_size().x; ++xi, ++xk)
(*result)(xk, yk, zk) = input(xi, yi, zi);
for (; xk < size.x; ++xk)
(*result)(xk, yk, zk) = value_type();
}
for (; yk < size.y; ++yk) {
value_type *p = &(*result)(0, yk, zk);
fill(p , &p[size.x], value_type());
}
}
for (; zk < size.z; ++zk) {
value_type *p = &(*result)(0, 0, zk);
fill( p, &p[xyk], value_type());
}
return result;
}
class CImageCrop: public TUnaryImageFilter<C3DImageWrap> {
public:
CImageCrop(const T3DVector<int>& start,const T3DVector<int>& size):
_M_start(start),
_M_size(size)
{
}
template <class Image>
typename CImageCrop::result_type operator () (const Image& image)
{
return C3DImageWrap(crop3d(image, _M_start, _M_size));
}
private:
T3DVector<int> _M_start;
T3DVector<int> _M_size;
};
int main( int argc, const char *argv[] )
{
using namespace std;
using namespace mona;
try {
T3DVector<int> start(0,0,0);
T3DVector<int> size(-1,-1,-1);
string in_filename;
string out_filename;
vector<int> vstart;
vector<int> vsize;
popt::COptions opts;
opts.push_back(popt::option( in_filename, "in-file", 'i',
"input data (image or mesh)", "" ));
opts.push_back(popt::option( out_filename, "out-file", 'o',
"displaced data (image or mesh)", "-" ));
opts.push_back(popt::option( vstart, "start", 's', "start of crop region","0,0,0"));
opts.push_back(popt::option( vsize, "size", 'z', "size of crop region","-1,-1,-1"));
vector<string> non_options;
popt::parse_options(argc, argv, opts, non_options);
if ( non_options.size() > 0 )
throw invalid_argument("unknown options");
if ( in_filename.empty() )
throw mona_runtime_error("'--in-file' ('i') option required");
if (vstart.size() == 3) {
start = T3DVector<int>(vstart[0], vstart[1], vstart[2]);
}else if (vstart.size() != 0) {
cvfatal() << "Bougus start argument" << endl;
return 1;
}
if (vsize.size() == 3)
size = T3DVector<int>(vsize[0], vsize[1], vsize[2]);
else if (vsize.size() != 0) {
cvfatal() << "Bougus size argument" << endl;
return 1;
}
C3DVFieldIOPluginHandler fieldio;
// allocate history instance
// CHistory::instance().append(argv[0], revision);
C3DVectorfieldPtr *field = fieldio.load(in_filename);
if (field) { // crop the field ...
C3DFVectorfield *cropped = crop3d (field->get_data(), start, size);
auto_ptr<C3DVectorfieldPtr> result(new C3DVectorfieldPtr(cropped));
return !fieldio.save(field->get_sourceformat(), *result, out_filename);
}
// 2. checking whether inpout contains an image
C3DImageIOPluginHandler imageio;
auto_ptr<C3DImageList> image_list(imageio.load(in_filename));
if ( image_list.get() && (image_list->size() > 0)) {
CImageCrop cropper(start,size);
for (C3DImageList::iterator i = image_list->begin(); i != image_list->end(); ++i) {
*i = image_filter(cropper,*i);
}
CHistory::instance().append(argv[0], revision, opts);
return !imageio.save(image_list->get_sourceformat(), *image_list, out_filename);
}else {
string not_found = ("No supported data found in ") + in_filename;
throw mona_runtime_error(not_found);
}
return EXIT_SUCCESS;
} // try
catch (const mona_runtime_error& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const mona_fatal_error& e){
cerr << argv[0] << " fatal: " << e.what() << endl;
}
catch (const mona_exception& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const invalid_argument &e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const exception& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (...){
cerr << argv[0] << " unknown exception" << endl;
}
return EXIT_FAILURE;
}
/*
$Log$
Revision 1.11 2005/06/29 13:32:02 wollny
move to libmona-version 0.7
Revision 1.2 2005/06/02 13:33:23 gerddie
adapt code to new plugin handling
Revision 1.1.1.1 2005/03/17 13:48:32 gerddie
initial checkin
Revision 1.10 2005/03/07 10:29:14 wollny
enlarging cropping bug removed
Revision 1.9 2005/01/31 11:12:08 tittge
updated to doxygen 1.4.0 compatible documenation
Revision 1.8 2005/01/25 14:12:09 wollny
support for typed attributes
Revision 1.7 2005/01/24 12:03:43 tittge
add history string
Revision 1.6 2005/01/20 09:18:43 tittge
append program history to result
Revision 1.5 2005/01/19 11:10:07 wollny
adapted to new verbose handling of libmona
Revision 1.4 2005/01/18 12:54:22 wollny
Changes in option handling made it neccessary to update all programs. The
default vale of an option is now given as the last !string! vale of that
option.
Revision 1.3 2005/01/17 18:18:54 wollny
bugs removed
Revision 1.2 2004/12/28 15:33:46 wollny
add some tools
Revision 1.1 2004/12/28 13:54:12 wollny
add some tools
Revision 1.7 2004/11/01 16:24:54 wollny
move from old error-report to cverb
Revision 1.6 2004/08/11 07:26:45 wollny
added support for multiple images
Revision 1.5 2004/08/10 15:25:40 tittge
add inversion of vector fields
Revision 1.4 2004/07/19 07:00:56 tittge
some more documentation
Revision 1.3 2004/06/03 09:57:45 wollny
Changed (hopefully) all instancable class names to Cxxxxx
Revision 1.2 2004/05/13 14:29:26 wollny
adapt to new message report
Revision 1.1 2004/04/05 08:45:46 tittge
initial
*/