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>
41class MaskedLattice;
42template <class T>
43class ImageInterface;
44template <class T>
45class Lattice;
46template <class T>
47class LatticeIterator;
48
51class Coordinate;
52class ObsInfo;
53class IPosition;
54class Unit;
55class ProgressMeter;
56
57// <summary>This regrids one image to match the coordinate system of another</summary>
58
59// <use visibility=export>
60
61// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
62// </reviewed>
63
64// <prerequisite>
65// <li> <linkto class="ImageInterface">ImageInterface</linkto>
66// <li> <linkto class="CoordinateSystem">CoordinateSystem</linkto>
67// <li> <linkto class="Interpolate2D">Interpolate2D</linkto>
68// <li> <linkto class="InterpolateArray1D">InterpolateArray1D</linkto>
69// </prerequisite>
70//
71// <etymology>
72// Regrids, or resamples, images.
73// </etymology>
74//
75// <synopsis>
76// This class enables you to regrid one image to the coordinate
77// system of another. You can regrid any or all of the
78// axes in the image. A range of interpolation schemes are available.
79//
80// It will cope with coordinate systems being in different orders
81// (coordinate, world axes, pixel axes). The basic approach is to
82// make a mapping from the input to the output coordinate systems,
83// but the output CoordinateSystem order is preserved in the output
84// image.
85//
86// Any DirectionCoordinate or LinearCoordinate holding exactly two axes
87// is regridded in one pass with a 2-D interpolation scheme.
88// All other axes are regridded in separate passes with a 1D interpolation
89// scheme. This means that a LinearCoordinate holding say 3 axes
90// where some of them are coupled will not be correctly regridded.
91// StokesCoordinates cannot be regridded.
92//
93// Multiple passes are made through the data, and the output of
94// each pass is the input of the next pass. The intermediate
95// images are stored as TempImages which may be in memory or
96// on disk, depending on their size.
97//
98// It can also simply insert this image into that one via
99// an integer shift.
100// </synopsis>
101//
102// <example>
103//
104// <srcblock>
105// </srcblock>
106// </example>
107//
108// <motivation>
109// A common image analysis need is to regrid images, e.g. to compare
110// images from different telescopes.
111// </motivation>
112//
113// <thrown>
114// <li> AipsError
115// </thrown>
116//
117// <todo asof="1999/04/20">
118// </todo>
119
120template <class T>
122 public:
123 // Default constructor
125
126 // copy constructor (copy semantics)
128
129 // destructor
131
132 // Assignment copy semantics)
134
135 // Regrid inImage onto the grid specified by outImage.
136 // If outImage has a writable mask, it will be updated in that
137 // output pixels at which the regridding failed will be masked bad (False)
138 // and the pixel value set to zero. Otherwise the output mask is not changed.
139 // Specify which pixel axes of outImage are to be
140 // regridded. The coordinate and axis order of outImage
141 // is preserved, regardless of where the relevant coordinates
142 // are in inImage.
143 //
144 // decimate only applies when replicate=False. it is
145 // the coordinate grid computation decimation FACTOR
146 // (e.g. nCoordGrid ~ nIn / decimate). 0 means no decimation
147 // (slowest and most accurate)
148 void regrid(ImageInterface<T>& outImage, typename Interpolate2D::Method method,
149 const IPosition& whichOutPixelAxes, const ImageInterface<T>& inImage,
150 Bool replicate = False, uInt decimate = 0, Bool showProgress = False,
151 Bool forceRegrid = False, 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,
167 Bool notify = False);
168 // </group>
169 //
170 // Inserts inImage into outImage. The alignment is done by
171 // placing the blc of inImage at the specified
172 // absolute pixel of the outImage (outPixelLocation). If
173 // the outPixelLocation vector is of zero length, then the images
174 // are aligned by their reference pixels. Only integral shifts are done
175 // in the aligment process. If outImage has a mask, it will be updated.
176 // Returns False if no overlap of images, in which case the
177 // output is not updated.
178 Bool insert(ImageInterface<T>& outImage, 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
200 // the only axis in its corresponding coordinate, this coordinate will not be replaced even if the
201 // axis is specified in <src>axes</src>. Upon return, <src>coordsToBeRegridded</src> will contain
202 // a list of the coordinates that will be regridded.
204 LogIO& os, std::set<Coordinate::Type>& coordsToBeRegridded, const CoordinateSystem& cSysTo,
205 const CoordinateSystem& cSysFrom, const IPosition& axes,
206 const IPosition& inShape = IPosition(), Bool giveStokesWarning = True);
207
208 private:
211 //
214 //
218 //
219 // Check shape and axes. Exception if no good. If pixelAxes
220 // of length 0, set to all axes according to shape
221 void _checkAxes(IPosition& outPixelAxes, const IPosition& inShape, const IPosition& outShape,
222 const Vector<Int>& pixelAxisMap, const CoordinateSystem& outCoords, Bool verbose);
223
224 // Find maps between coordinate systems
225 void findMaps(uInt nDim, Vector<Int>& pixelAxisMap1, Vector<Int>& pixelAxisMap2,
226 const CoordinateSystem& inCoords, const CoordinateSystem& outCoords) const;
227
228 // Find scale factor to conserve flux
229 Double findScaleFactor(const Unit& units, const CoordinateSystem& inCoords,
230 const CoordinateSystem& outCoords, Int inCoordinate, Int outCoordinate,
231 LogIO& os) const;
232
233 // Regrid one Coordinate
234 void _regridOneCoordinate(LogIO& os, IPosition& outShape2, Vector<Bool>& doneOutPixelAxes,
235 MaskedLattice<T>*& finalOutPtr, MaskedLattice<T>*& inPtr,
236 MaskedLattice<T>*& outPtr, CoordinateSystem& outCoords,
237 const CoordinateSystem& inCoords, Int outPixelAxis,
238 const ImageInterface<T>& inImage, const IPosition& outShape,
239 Bool replicate, uInt decimate, Bool outIsMasked, Bool showProgress,
240 Bool forceRegrid, typename Interpolate2D::Method method, Bool verbose);
241
242 // Regrid DirectionCoordinate or 2-axis LinearCoordinate
244 const MaskedLattice<T>& inLattice, const Unit& imageUnit,
245 const CoordinateSystem& inCoords, const CoordinateSystem& outCoords,
246 Int inCoordinate, Int outCoordinate, const Vector<Int> inPixelAxes,
247 const Vector<Int> outPixelAxes, const Vector<Int> pixelAxisMap1,
248 const Vector<Int> pixelAxisMap2,
249 typename Interpolate2D::Method method, Bool replicate, uInt decimate,
250 Bool showProgress);
251
252 // Make regridding coordinate grid for this cursor.
253 void make2DCoordinateGrid(LogIO& os, Bool& allFail, Bool& missedIt, Double& minInX,
254 Double& minInY, Double& maxInX, Double& maxInY, Cube<Double>& in2DPos,
255 Matrix<Bool>& succeed, const CoordinateSystem& inCoords,
256 const CoordinateSystem& outCoords, Int inCoordinate, Int outCoordinate,
257 uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis,
258 const IPosition& inPixelAxes, const IPosition& outPixelAxes,
259 const IPosition& inShape, const IPosition& outPos,
260 const IPosition& cursorShape, uInt decimate = 0);
261
262 // Make replication coordinate grid for this cursor
263 void make2DCoordinateGrid(Cube<Double>& in2DPos, Double& minInX, Double& minInY, Double& maxInX,
264 Double& maxInY, const Vector<Double>& pixelScale, uInt xInAxis,
265 uInt yInAxis, uInt xOutAxis, uInt yOutAxis, uInt xInCorrAxis,
266 uInt yInCorrAxis, uInt xOutCorrAxis, uInt yOutCorrAxis,
267 const IPosition& outPos, const IPosition& cursorShape);
268
269 // Make regridding coordinate grid for this axis
271 Bool& allFailed, Bool& allGood, const Coordinate& inCoord,
272 const Coordinate& outCoord, Int inAxisInCoordinate,
273 Int outAxisInCoordinate, MFrequency::Convert& machine, Bool useMachine);
274
275 // Make replication coordinate grid for this axis
277 typename NumericTraits<T>::BaseType pixelScale) const;
278
279 // Regrid 1 axis
280 void regrid1D(MaskedLattice<T>& outLattice, const MaskedLattice<T>& inLattice,
281 const Coordinate& inCoord, const Coordinate& outCoord,
282 const Vector<Int>& inPixelAxes, const Vector<Int>& outPixelAxes,
283 Int inAxisInCoordinate, Int outAxisInCoordinate, const Vector<Int> pixelAxisMap,
284 typename Interpolate2D::Method method, MFrequency::Convert& machine, Bool replicate,
285 Bool useMachine, Bool showProgress);
286
287 //
288 void regrid2DMatrix(Lattice<T>& outCursor, LatticeIterator<Bool>*& outMaskIterPtr,
289 const Interpolate2D& interp, ProgressMeter*& pProgress, Double& iPix,
290 uInt nDim, uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis,
291 Double scale, Bool inIsMasked, Bool outIsMasked, const IPosition& outPos,
292 const IPosition& outCursorShape, const IPosition& inChunkShape,
293 const IPosition& inChunkBlc, const IPosition& pixelAxisMap2,
294 Array<T>& inDataChunk, Array<Bool>*& inMaskChunkPtr,
295 const Cube<Double>& pix2DPos, const Matrix<Bool>& succeed);
296
297 void findXYExtent(Bool& missedIt, Bool& allFailed, Double& minInX, Double& minInY, Double& maxInX,
298 Double& maxInY, Cube<Double>& in2DPos, const Matrix<Bool>& succeed,
299 uInt xInAxis, uInt yInAxis, uInt xOutAxis, uInt yOutAxis,
300 const IPosition& outPos, const IPosition& outCursorShape,
301 const IPosition& inShape);
302 //
303 Bool minmax(Double& minX, Double& maxX, Double& minY, Double& maxY, const Array<Double>& xData,
304 const Array<Double>& yData, const Array<Bool>& mask);
305};
306
307// # Declare extern templates for often used types.
308extern template class ImageRegrid<Float>;
309
310} // namespace casacore
311
312#ifndef CASACORE_NO_AUTO_TEMPLATES
313#include <casacore/images/Images/ImageRegrid.tcc>
314#endif // # CASACORE_NO_AUTO_TEMPLATES
315#endif
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.
MeasConvert< MFrequency > Convert
Measure conversion use (i.e.
Definition MFrequency.h:204
Char BaseType
Numeric type.
For temporary backward namespace compatibility, use casa as alias for casacore.
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.
String replicate(char c, String::size_type n)
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