casacore
Loading...
Searching...
No Matches
ImageRegrid.h
Go to the documentation of this file.
1//# ImageRegrid.h: Regrid Images
2//# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef IMAGES_IMAGEREGRID_H
27#define IMAGES_IMAGEREGRID_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/Arrays/Matrix.h>
31#include <casacore/casa/Arrays/Cube.h>
32#include <casacore/measures/Measures/MDirection.h>
33#include <casacore/measures/Measures/MFrequency.h>
34#include <casacore/scimath/Mathematics/Interpolate2D.h>
35#include <casacore/scimath/Mathematics/NumericTraits.h>
36#include <set>
37
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40template<class T> class MaskedLattice;
41template<class T> class ImageInterface;
42template<class T> class Lattice;
43template<class T> class LatticeIterator;
44
45class CoordinateSystem;
46class DirectionCoordinate;
47class Coordinate;
48class ObsInfo;
49class IPosition;
50class Unit;
51class ProgressMeter;
52
53// <summary>This regrids one image to match the coordinate system of another</summary>
54
55// <use visibility=export>
56
57// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
58// </reviewed>
59
60// <prerequisite>
61// <li> <linkto class="ImageInterface">ImageInterface</linkto>
62// <li> <linkto class="CoordinateSystem">CoordinateSystem</linkto>
63// <li> <linkto class="Interpolate2D">Interpolate2D</linkto>
64// <li> <linkto class="InterpolateArray1D">InterpolateArray1D</linkto>
65// </prerequisite>
66//
67// <etymology>
68// Regrids, or resamples, images.
69// </etymology>
70//
71// <synopsis>
72// This class enables you to regrid one image to the coordinate
73// system of another. You can regrid any or all of the
74// axes in the image. A range of interpolation schemes are available.
75//
76// It will cope with coordinate systems being in different orders
77// (coordinate, world axes, pixel axes). The basic approach is to
78// make a mapping from the input to the output coordinate systems,
79// but the output CoordinateSystem order is preserved in the output
80// image.
81//
82// Any DirectionCoordinate or LinearCoordinate holding exactly two axes
83// is regridded in one pass with a 2-D interpolation scheme.
84// All other axes are regridded in separate passes with a 1D interpolation
85// scheme. This means that a LinearCoordinate holding say 3 axes
86// where some of them are coupled will not be correctly regridded.
87// StokesCoordinates cannot be regridded.
88//
89// Multiple passes are made through the data, and the output of
90// each pass is the input of the next pass. The intermediate
91// images are stored as TempImages which may be in memory or
92// on disk, depending on their size.
93//
94// It can also simply insert this image into that one via
95// an integer shift.
96// </synopsis>
97//
98// <example>
99//
100// <srcblock>
101// </srcblock>
102// </example>
103//
104// <motivation>
105// A common image analysis need is to regrid images, e.g. to compare
106// images from different telescopes.
107// </motivation>
108//
109// <thrown>
110// <li> AipsError
111// </thrown>
112//
113// <todo asof="1999/04/20">
114// </todo>
115
116template <class T> class ImageRegrid
117{
118public:
119
120 // Default constructor
122
123 // copy constructor (copy semantics)
125
126 // destructor
128
129 // Assignment copy semantics)
131
132 // Regrid inImage onto the grid specified by outImage.
133 // If outImage has a writable mask, it will be updated in that
134 // output pixels at which the regridding failed will be masked bad (False)
135 // and the pixel value set to zero. Otherwise the output mask is not changed.
136 // Specify which pixel axes of outImage are to be
137 // regridded. The coordinate and axis order of outImage
138 // is preserved, regardless of where the relevant coordinates
139 // are in inImage.
140 //
141 // decimate only applies when replicate=False. it is
142 // the coordinate grid computation decimation FACTOR
143 // (e.g. nCoordGrid ~ nIn / decimate). 0 means no decimation
144 // (slowest and most accurate)
145 void regrid(ImageInterface<T>& outImage,
146 typename Interpolate2D::Method method,
147 const IPosition& whichOutPixelAxes,
148 const ImageInterface<T>& inImage,
149 Bool replicate=False, uInt decimate=0,
150 Bool showProgress=False, Bool forceRegrid=False,
151 Bool verbose=False);
152
153// Get and set the 2-D coordinate grid. After a call to function <src>regrid</src>
154// in which coupled 2D coordinate (presently only DirectionCoordinate) is
155// regridded, this coordinate grid will be available. It can be reused
156// via the <src>set2DCoordinateGrid</src> function for another like plane
157// (e.g. if you choose to regrid planes of a cube separately). When you provide
158// the coordinate grid, it will no longer (for that 2D coordinate only) be
159// computed internally, which may save a lot of time. Ordinarily, if you
160// regridded many planes of a cube in one call to regrid, the coordinate grid
161// is cached for you. To trigger successive calls to regrid to go back to
162// internal computation, set zero length Cube and Matrix. <src>gridMask</src>
163// is True for successfull coordinate conversions, and False otherwise.
164// <group>
165 void get2DCoordinateGrid (Cube<Double>& grid, Matrix<Bool>& gridMask) const;
166 void set2DCoordinateGrid (const Cube<Double>& grid, const Matrix<Bool>& gridMask, Bool notify=False);
167// </group>
168//
169 // Inserts inImage into outImage. The alignment is done by
170 // placing the blc of inImage at the specified
171 // absolute pixel of the outImage (outPixelLocation). If
172 // the outPixelLocation vector is of zero length, then the images
173 // are aligned by their reference pixels. Only integral shifts are done
174 // in the aligment process. If outImage has a mask, it will be updated.
175 // Returns False if no overlap of images, in which case the
176 // output is not updated.
178 const Vector<Double>& outPixelLocation,
179 const ImageInterface<T>& inImage);
180
181 // Print out useful debugging information (level 0 is none,
182 // 1 is some, 2 is too much)
183 void showDebugInfo(Int level=0) {itsShowLevel = level;};
184
185 // Enable/disable Measures Reference conversions
187
188 // Helper function. We are regridding from cSysFrom to cSysTo for the
189 // specified pixel axes of cSyFrom. This function returns a CoordinateSystem which,
190 // for the pixel axes being regridded, copies the coordinates from cSysTo
191 // (if coordinate type present in cSysTo) or cSysFrom (coordinate
192 // type not present in cSysTo).
193 // For the axes not being regridded, it copies the coordinates from
194 // cSysFrom. This helps you build the cSys for function regrid.
195 // The ObsInfo from cSysFrom is copied to the output CoordinateSystem.
196 // If inShape has one or more elements it represenents the size of the
197 // image to be regridded. It this must have the same number of elements
198 // as the number of pixel axes in <src>cSysFrom</src>. If any of the values
199 // are unity (ie the axes are degenerate), and the corresponding axis in <src>csysFrom</src> is the only
200 // axis in its corresponding coordinate, this coordinate will not be replaced
201 // even if the axis is specified in <src>axes</src>.
202 // Upon return, <src>coordsToBeRegridded</src> will contain a list of the coordinates that will
203 // be regridded.
205 LogIO& os,
206 std::set<Coordinate::Type>& coordsToBeRegridded,
207 const CoordinateSystem& cSysTo,
208 const CoordinateSystem& cSysFrom,
209 const IPosition& axes,
210 const IPosition& inShape=IPosition(),
211 Bool giveStokesWarning=True
212 );
213
214 private:
215
218//
221//
225//
226 // Check shape and axes. Exception if no good. If pixelAxes
227 // of length 0, set to all axes according to shape
228 void _checkAxes(IPosition& outPixelAxes,
229 const IPosition& inShape,
230 const IPosition& outShape,
231 const Vector<Int>& pixelAxisMap,
232 const CoordinateSystem& outCoords,
233 Bool verbose);
234
235 // Find maps between coordinate systems
236 void findMaps (uInt nDim,
237 Vector<Int>& pixelAxisMap1,
238 Vector<Int>& pixelAxisMap2,
239 const CoordinateSystem& inCoords,
240 const CoordinateSystem& outCoords) const;
241
242 // Find scale factor to conserve flux
244 const CoordinateSystem& inCoords,
245 const CoordinateSystem& outCoords,
246 Int inCoordinate, Int outCoordinate,
247 LogIO& os) const;
248
249 // Regrid one Coordinate
250 void _regridOneCoordinate (LogIO& os, IPosition& outShape2,
251 Vector<Bool>& doneOutPixelAxes,
252 MaskedLattice<T>* &finalOutPtr,
253 MaskedLattice<T>* &inPtr,
254 MaskedLattice<T>* &outPtr,
255 CoordinateSystem& outCoords,
256 const CoordinateSystem& inCoords,
257 Int outPixelAxis,
258 const ImageInterface<T>& inImage,
259 const IPosition& outShape,
260 Bool replicate, uInt decimate,
261 Bool outIsMasked, Bool showProgress,
262 Bool forceRegrid,
263 typename Interpolate2D::Method method,
264 Bool verbose);
265
266 // Regrid DirectionCoordinate or 2-axis LinearCoordinate
268 const MaskedLattice<T>& inLattice,
269 const Unit& imageUnit,
270 const CoordinateSystem& inCoords,
271 const CoordinateSystem& outCoords,
272 Int inCoordinate, Int outCoordinate,
273 const Vector<Int> inPixelAxes,
274 const Vector<Int> outPixelAxes,
275 const Vector<Int> pixelAxisMap1,
276 const Vector<Int> pixelAxisMap2,
277 typename Interpolate2D::Method method,
278 Bool replicate, uInt decimate,
279 Bool showProgress);
280
281 // Make regridding coordinate grid for this cursor.
282 void make2DCoordinateGrid (LogIO& os, Bool& allFail, Bool&missedIt,
283 Double& minInX, Double& minInY,
284 Double& maxInX, Double& maxInY,
285 Cube<Double>& in2DPos,
286 Matrix<Bool>& succeed,
287 const CoordinateSystem& inCoords,
288 const CoordinateSystem& outCoords,
289 Int inCoordinate, Int outCoordinate,
290 uInt xInAxis, uInt yInAxis,
291 uInt xOutAxis, uInt yOutAxis,
292 const IPosition& inPixelAxes,
293 const IPosition& outPixelAxes,
294 const IPosition& inShape,
295 const IPosition& outPos,
296 const IPosition& cursorShape,
297 uInt decimate=0);
298
299 // Make replication coordinate grid for this cursor
301 Double& minInX, Double& minInY,
302 Double& maxInX, Double& maxInY,
303 const Vector<Double>& pixelScale,
304 uInt xInAxis, uInt yInAxis,
305 uInt xOutAxis, uInt yOutAxis,
306 uInt xInCorrAxis, uInt yInCorrAxis,
307 uInt xOutCorrAxis, uInt yOutCorrAxis,
308 const IPosition& outPos, const IPosition& cursorShape);
309
310 // Make regridding coordinate grid for this axis
312 Vector<Bool>& failed,
313 Bool& allFailed,
314 Bool& allGood,
315 const Coordinate& inCoord,
316 const Coordinate& outCoord,
317 Int inAxisInCoordinate,
318 Int outAxisInCoordinate,
319 MFrequency::Convert& machine,
320 Bool useMachine);
321
322
323 // Make replication coordinate grid for this axis
325 typename NumericTraits<T>::BaseType pixelScale) const;
326
327 // Regrid 1 axis
328 void regrid1D (MaskedLattice<T>& outLattice,
329 const MaskedLattice<T>& inLattice,
330 const Coordinate& inCoord,
331 const Coordinate& outCoord,
332 const Vector<Int>& inPixelAxes,
333 const Vector<Int>& outPixelAxes,
334 Int inAxisInCoordinate,
335 Int outAxisInCoordinate,
336 const Vector<Int> pixelAxisMap,
337 typename Interpolate2D::Method method,
338 MFrequency::Convert& machine,
339 Bool replicate,
340 Bool useMachine, Bool showProgress);
341
342//
343 void regrid2DMatrix(Lattice<T>& outCursor,
344 LatticeIterator<Bool>*& outMaskIterPtr,
345 const Interpolate2D& interp,
346 ProgressMeter*& pProgress,
347 Double& iPix,
348 uInt nDim,
349 uInt xInAxis, uInt yInAxis,
350 uInt xOutAxis, uInt yOutAxis,
351 Double scale,
352 Bool inIsMasked, Bool outIsMasked,
353 const IPosition& outPos,
354 const IPosition& outCursorShape,
355 const IPosition& inChunkShape,
356 const IPosition& inChunkBlc,
357 const IPosition& pixelAxisMap2,
358 Array<T>& inDataChunk,
359 Array<Bool>*& inMaskChunkPtr,
360 const Cube<Double>& pix2DPos,
361 const Matrix<Bool>& succeed);
362
363 void findXYExtent (Bool& missedIt, Bool& allFailed,
364 Double& minInX, Double& minInY,
365 Double& maxInX, Double& maxInY,
366 Cube<Double>& in2DPos,
367 const Matrix<Bool>& succeed,
368 uInt xInAxis, uInt yInAxis,
369 uInt xOutAxis, uInt yOutAxis,
370 const IPosition& outPos,
371 const IPosition& outCursorShape,
372 const IPosition& inShape);
373//
374 Bool minmax(Double &minX, Double &maxX, Double& minY, Double& maxY,
375 const Array<Double> &xData,
376 const Array<Double> &yData,
377 const Array<Bool>& mask);
378};
379
380//# Declare extern templates for often used types.
381 extern template class ImageRegrid<Float>;
382
383} //# NAMESPACE CASACORE - END
384
385#ifndef CASACORE_NO_AUTO_TEMPLATES
386#include <casacore/images/Images/ImageRegrid.tcc>
387#endif //# CASACORE_NO_AUTO_TEMPLATES
388#endif
389
simple 1-D array
Definition Block.h:198
ImageRegrid()
Default constructor.
ImageRegrid(const ImageRegrid &other)
copy constructor (copy semantics)
Double findScaleFactor(const Unit &units, const CoordinateSystem &inCoords, const CoordinateSystem &outCoords, Int inCoordinate, Int outCoordinate, LogIO &os) const
Find scale factor to conserve flux
void get2DCoordinateGrid(Cube< Double > &grid, Matrix< Bool > &gridMask) const
Get and set the 2-D coordinate grid.
Matrix< Bool > itsUser2DCoordinateGridMask
ImageRegrid< T > & operator=(const ImageRegrid &other)
Assignment copy semantics)
Cube< Double > itsUser2DCoordinateGrid
void make2DCoordinateGrid(LogIO &os, Bool &allFail, Bool &missedIt, Double &minInX, Double &minInY, Double &maxInX, Double &maxInY, Cube< Double > &in2DPos, Matrix< Bool > &succeed, const CoordinateSystem &inCoords, const CoordinateSystem &outCoords, Int inCoordinate, Int outCoordinate, uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis, const IPosition &inPixelAxes, const IPosition &outPixelAxes, const IPosition &inShape, const IPosition &outPos, const IPosition &cursorShape, uInt decimate=0)
Make regridding coordinate grid for this cursor.
void regridTwoAxisCoordinate(LogIO &os, MaskedLattice< T > &outLattice, const MaskedLattice< T > &inLattice, const Unit &imageUnit, const CoordinateSystem &inCoords, const CoordinateSystem &outCoords, Int inCoordinate, Int outCoordinate, const Vector< Int > inPixelAxes, const Vector< Int > outPixelAxes, const Vector< Int > pixelAxisMap1, const Vector< Int > pixelAxisMap2, typename Interpolate2D::Method method, Bool replicate, uInt decimate, Bool showProgress)
Regrid DirectionCoordinate or 2-axis LinearCoordinate.
void regrid2DMatrix(Lattice< T > &outCursor, LatticeIterator< Bool > *&outMaskIterPtr, const Interpolate2D &interp, ProgressMeter *&pProgress, Double &iPix, uInt nDim, uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis, Double scale, Bool inIsMasked, Bool outIsMasked, const IPosition &outPos, const IPosition &outCursorShape, const IPosition &inChunkShape, const IPosition &inChunkBlc, const IPosition &pixelAxisMap2, Array< T > &inDataChunk, Array< Bool > *&inMaskChunkPtr, const Cube< Double > &pix2DPos, const Matrix< Bool > &succeed)
void make1DCoordinateGrid(Block< typename NumericTraits< T >::BaseType > &xOut, Vector< Bool > &failed, Bool &allFailed, Bool &allGood, const Coordinate &inCoord, const Coordinate &outCoord, Int inAxisInCoordinate, Int outAxisInCoordinate, MFrequency::Convert &machine, Bool useMachine)
Make regridding coordinate grid for this axis.
void regrid(ImageInterface< T > &outImage, typename Interpolate2D::Method method, const IPosition &whichOutPixelAxes, const ImageInterface< T > &inImage, Bool replicate=False, uInt decimate=0, Bool showProgress=False, Bool forceRegrid=False, Bool verbose=False)
Regrid inImage onto the grid specified by outImage.
void set2DCoordinateGrid(const Cube< Double > &grid, const Matrix< Bool > &gridMask, Bool notify=False)
Matrix< Bool > its2DCoordinateGridMask
void make2DCoordinateGrid(Cube< Double > &in2DPos, Double &minInX, Double &minInY, Double &maxInX, Double &maxInY, const Vector< Double > &pixelScale, uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis, uInt xInCorrAxis, uInt yInCorrAxis, uInt xOutCorrAxis, uInt yOutCorrAxis, const IPosition &outPos, const IPosition &cursorShape)
Make replication coordinate grid for this cursor.
void make1DCoordinateGrid(Block< typename NumericTraits< T >::BaseType > &xOut, typename NumericTraits< T >::BaseType pixelScale) const
Make replication coordinate grid for this axis.
void _checkAxes(IPosition &outPixelAxes, const IPosition &inShape, const IPosition &outShape, const Vector< Int > &pixelAxisMap, const CoordinateSystem &outCoords, Bool verbose)
Check shape and axes.
Bool minmax(Double &minX, Double &maxX, Double &minY, Double &maxY, const Array< Double > &xData, const Array< Double > &yData, const Array< Bool > &mask)
void findMaps(uInt nDim, Vector< Int > &pixelAxisMap1, Vector< Int > &pixelAxisMap2, const CoordinateSystem &inCoords, const CoordinateSystem &outCoords) const
Find maps between coordinate systems.
void findXYExtent(Bool &missedIt, Bool &allFailed, Double &minInX, Double &minInY, Double &maxInX, Double &maxInY, Cube< Double > &in2DPos, const Matrix< Bool > &succeed, uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis, const IPosition &outPos, const IPosition &outCursorShape, const IPosition &inShape)
void regrid1D(MaskedLattice< T > &outLattice, const MaskedLattice< T > &inLattice, const Coordinate &inCoord, const Coordinate &outCoord, const Vector< Int > &inPixelAxes, const Vector< Int > &outPixelAxes, Int inAxisInCoordinate, Int outAxisInCoordinate, const Vector< Int > pixelAxisMap, typename Interpolate2D::Method method, MFrequency::Convert &machine, Bool replicate, Bool useMachine, Bool showProgress)
Regrid 1 axis.
Cube< Double > its2DCoordinateGrid
void _regridOneCoordinate(LogIO &os, IPosition &outShape2, Vector< Bool > &doneOutPixelAxes, MaskedLattice< T > *&finalOutPtr, MaskedLattice< T > *&inPtr, MaskedLattice< T > *&outPtr, CoordinateSystem &outCoords, const CoordinateSystem &inCoords, Int outPixelAxis, const ImageInterface< T > &inImage, const IPosition &outShape, Bool replicate, uInt decimate, Bool outIsMasked, Bool showProgress, Bool forceRegrid, typename Interpolate2D::Method method, Bool verbose)
Regrid one Coordinate.
void disableReferenceConversions(Bool disable=True)
Enable/disable Measures Reference conversions.
~ImageRegrid()
destructor
void showDebugInfo(Int level=0)
Print out useful debugging information (level 0 is none, 1 is some, 2 is too much)
Bool insert(ImageInterface< T > &outImage, const Vector< Double > &outPixelLocation, const ImageInterface< T > &inImage)
Inserts inImage into outImage.
static CoordinateSystem makeCoordinateSystem(LogIO &os, std::set< Coordinate::Type > &coordsToBeRegridded, const CoordinateSystem &cSysTo, const CoordinateSystem &cSysFrom, const IPosition &axes, const IPosition &inShape=IPosition(), Bool giveStokesWarning=True)
Helper function.
A read/write lattice iterator.
Char BaseType
Numeric type.
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53