28 #include <Base/Debug/Error.hh>
36 template <
class InputStorageType,
class OutputStorageType>
48 template <
class InputStorageType,
class OutputStorageType>
55 template <
class InputStorageType,
class OutputStorageType>
57 Init(
int width,
int height)
60 if (_SizeX!=0 || _SizeY!=0 || _OutNum!=0 || _out || _in || _p_forward){
61 BIASERR(
"FFT2D: call Release() before calling Init() a second time");
67 _Size = _SizeX*_SizeY;
68 _OutSizeY = (_SizeY>>1)+1;
69 _OutNum=_SizeX * _OutSizeY;
70 _out =(fftw_complex*)fftw_malloc(
sizeof(fftw_complex) * _OutNum);
71 _in = (
double*)fftw_malloc(
sizeof(
double) * _Size);
73 _p_forward = fftw_plan_dft_r2c_2d(_SizeX, _SizeY, _in, _out, FFTW_MEASURE);
74 _p_reverse = fftw_plan_dft_c2r_2d(_SizeX, _SizeY, _out, _in, FFTW_ESTIMATE);
82 template <
class InputStorageType,
class OutputStorageType>
86 if (_p_forward) fftw_destroy_plan(_p_forward);
87 if (_in) fftw_free(_in);
88 if (_out) fftw_free(_out);
92 _SizeX=_SizeY=_Size=_OutNum=0;
96 template <
class InputStorageType,
class OutputStorageType>
101 if (_SizeX==0 || _SizeY==0 ){
102 BIASERR(
"FFT2D: call Init() before calling Filter() ");
108 BIASASSERT((
int)src.
GetWidth() == _SizeY);
109 BIASASSERT((
int)src.
GetHeight() == _SizeX);
113 for (
int i=0;i<_Size; i++) {
114 inp[i] = (double) imgp[i];
118 fftw_execute(_p_forward);
123 template <
class InputStorageType,
class OutputStorageType>
128 if (_SizeX==0 || _SizeY==0 ){
129 BIASERR(
"FFT2D: call Init() before calling Filter() ");
135 BIASASSERT((
int)src.
GetWidth() == _OutSizeY);
136 BIASASSERT((
int)src.
GetHeight() == _SizeX);
138 fftw_complex *outp = _out;
140 for (
int i=0;i<_OutNum; i++) {
141 outp[i][0] = (double) imgp[2*i];
142 outp[i][1] = (double) imgp[2*i+1];
146 fftw_execute(_p_reverse);
154 template <
class InputStorageType,
class OutputStorageType>
161 dst.
Init(_OutSizeY, _SizeX, 2);
163 fftw_complex *outp = _out;
165 for (
int i=0;i<_OutNum; i++){
166 oimgp[2*i] = (OutputStorageType) outp[i][0];
167 oimgp[2*i+1] = (OutputStorageType) outp[i][1];
175 template <
class InputStorageType,
class OutputStorageType>
183 dst1.
Init(_OutSizeY, _SizeX,1);
185 dst2.
Init(_OutSizeY, _SizeX,1);
187 fftw_complex *outp = _out;
190 for (
int i=0;i<_OutNum; i++){
191 oimgp1[i] = (OutputStorageType) (outp[i][0]);
192 oimgp2[i] = (OutputStorageType) (outp[i][1]);
200 template <
class InputStorageType,
class OutputStorageType>
206 dst.
Init(_OutSizeY ,_SizeX,1);
208 fftw_complex *outp = _out;
210 for (
int i=0;i<_OutNum; i++){
213 (OutputStorageType) (sqrt((outp[i][0] * outp[i][0]) + (outp[i][1] * outp[i][1]))/_Size);
220 template <
class InputStorageType,
class OutputStorageType>
226 dst.
Init( _OutSizeY ,_SizeX,1);
228 fftw_complex *outp = _out;
230 for (
int i=0;i<_OutNum; i++){
231 oimgp[i] = (OutputStorageType) log(sqrt((outp[i][0]* outp[i][0]) + (outp[i][1]* outp[i][1])));
238 template <
class InputStorageType,
class OutputStorageType>
244 dst.
Init( _OutSizeY ,_SizeX,1);
246 fftw_complex *outp = _out;
248 for (
int i=0;i<_OutNum; i++){
249 oimgp[i] = (OutputStorageType) atan2f((
float)outp[i][0], (float)outp[i][1]);
255 #include <Base/Common/BIASpragmaStart.hh>
256 template <
class InputStorageType,
class OutputStorageType>
262 for (
unsigned int i=0; i<count; i++)
266 d[i] /= (OutputStorageType) _Size;
271 template <
class InputStorageType,
class OutputStorageType>
276 _out2 =(fftw_complex*)fftw_malloc(
sizeof(fftw_complex) * _OutNum);
279 fftw_complex *outp1 = _out;
280 fftw_complex *outp2 = _out2;
281 for (
int i=0;i<_OutNum; i++){
282 outp2[i][0] = outp1[i][0];
283 outp2[i][1] = outp1[i][1];
289 dst1.
Init(_OutSizeY, _SizeX,2);
296 for (
int i=0;i<_OutNum; i++){
297 abs = (double)(outp2[i][0] * outp2[i][0] + outp2[i][1] * outp2[i][1]);
298 oimgp1[2*i] = (OutputStorageType) ((outp1[i][0] * outp2[i][0] + outp1[i][1] * outp2[i][1])/abs);
299 oimgp1[2*i+1] = (OutputStorageType) ((outp2[i][0] * outp1[i][1] - outp1[i][0] * outp2[i][1])/abs);
305 #include <Base/Common/BIASpragmaEnd.hh>
309 template <
class InputStorageType,
class OutputStorageType>
316 dst.
Init(_SizeY, _SizeX,1);
320 for (
int i=0;i<_Size; i++){
321 oimgp[i] = (InputStorageType) inp[i];
326 template <
class InputStorageType,
class OutputStorageType>
330 BIASERR(
"no parameter support, yet");
340 #if defined(BUILD_IMAGE_FLOAT)
343 #if defined(BUILD_IMAGE_DOUBLE)
346 #if defined(BUILD_IMAGE_UCHAR) && defined(BUILD_IMAGE_FLOAT)
349 #if defined(BUILD_IMAGE_UCHAR) && defined(BUILD_IMAGE_DOUBLE)
void Release()
reimplemented from ImageBase
virtual int Forward_(const Image< InputStorageType > &src)
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
unsigned int GetWidth() const
virtual int TransformLogAbs(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
dstX.GetChannelCount()==src.GetCHannelCount()
virtual int TransformAbs(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
Transform forward and get absolute value from complex result The result is not normalized! ...
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
unsigned int GetHeight() const
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
void Init(int width, int height)
initializes for forward transformation on complete image.
virtual int TransformPhase(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
dstX.GetChannelCount()==src.GetCHannelCount()
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
dst.GetChannelCount()==2*src.GetCHannelCount() The result is not normalized!
virtual int Reverse_(const Image< OutputStorageType > &src)
virtual void GetBordersValid_(int &border_x, int &border_y) const
void Release()
call this before a second call to Init()
virtual int CrossPowerSpectrum(const Image< InputStorageType > &src1, const Image< InputStorageType > &src2, Image< OutputStorageType > &dst1)
virtual int Normalize(Image< OutputStorageType > &dst)
Normalize output after transformation.
Wrapper to the fftw3 library adapted for 2D image filtering.
virtual int TransformReverse(const Image< OutputStorageType > &src, Image< InputStorageType > &dst)
Transform reverse, src must be of _SizeX,_OutSizeY,2.