source: tags/release-1.1.2/src/Utils/GaussSmooth.cc @ 1441

Last change on this file since 1441 was 393, checked in by MatthewWhiting, 17 years ago

Fixed up headers for trunk as well.

File size: 6.8 KB
Line 
1// -----------------------------------------------------------------------
2// GaussSmooth.cc: Member functions for the GaussSmooth class.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <math.h>
30#include <duchamp/Utils/GaussSmooth.hh>
31
32GaussSmooth::GaussSmooth()
33{
34  allocated=false;
35};
36GaussSmooth::~GaussSmooth()
37{
38  if(allocated) delete [] kernel;
39};
40
41GaussSmooth::GaussSmooth(const GaussSmooth& g)
42{
43  operator=(g);
44}
45
46GaussSmooth& GaussSmooth::operator=(const GaussSmooth& g)
47{
48  if(this==&g) return *this;
49  this->kernMaj   = g.kernMaj;
50  this->kernMin   = g.kernMin;
51  this->kernPA    = g.kernPA;
52  this->kernWidth = g.kernWidth;
53  this->stddevScale = g.stddevScale;
54  this->allocated = g.allocated;
55  if(this->allocated){
56    this->kernel = new float[this->kernWidth*this->kernWidth];
57    for(int i=0;i<this->kernWidth*this->kernWidth;i++)
58      this->kernel[i] = g.kernel[i];
59  }
60  return *this;
61}
62
63GaussSmooth::GaussSmooth(float maj, float min, float pa)
64{
65  this->allocated=false;
66  this->define(maj, min, pa);
67};
68
69GaussSmooth::GaussSmooth(float maj)
70{
71  this->allocated=false;
72  this->define(maj, maj, 0);
73};
74
75void GaussSmooth::define(float maj, float min, float pa)
76{
77
78  this->kernMaj = maj;
79  this->kernMin = min;
80  this->kernPA  = pa;
81
82  // The parameters kernMaj & kernMin are the FWHM in the major and
83  // minor axis directions. We correct these to the sigma_x and
84  // sigma_y parameters for the 2D gaussian by halving and dividing by
85  // sqrt(2 ln(2)). Actually work with sigma_x^2 to make things
86  // easier.
87  float sigmaX2 = (this->kernMaj*this->kernMaj/4.) / (2.*M_LN2);
88  float sigmaY2 = (this->kernMin*this->kernMin/4.) / (2.*M_LN2);
89
90  // First determine the size of the kernel.
91  // For the moment, just calculate the size based on the number of
92  // pixels needed to make the exponential drop to less than the
93  // stated precision. Use the major axis to get the largest square
94  // that includes the ellipse.
95  const float precision = 1.e-4;
96  int kernelHW = int(ceil(sqrt(-1.*log(precision)*sigmaX2)));
97  this->kernWidth = 2*kernelHW + 1;
98//   std::cerr << "Making a kernel of width " << this->kernWidth << "\n";
99
100  if(this->allocated) delete [] this->kernel;
101  this->kernel = new float[this->kernWidth*this->kernWidth];
102  this->allocated = true;
103  this->stddevScale=0.;
104  float posang = this->kernPA * M_PI/180.;
105
106
107  for(int i=0;i<this->kernWidth;i++){
108    for(int j=0;j<this->kernWidth;j++){
109      float xpt = (i-kernelHW)*sin(posang) - (j-kernelHW)*cos(posang);
110
111      float ypt = (i-kernelHW)*cos(posang) + (j-kernelHW)*sin(posang);
112      float rsq = (xpt*xpt/sigmaX2) + (ypt*ypt/sigmaY2);
113      kernel[i*this->kernWidth+j] = exp( -0.5 * rsq);
114      this->stddevScale +=
115        kernel[i*this->kernWidth+j]*kernel[i*this->kernWidth+j];
116    }
117  }
118  this->stddevScale = sqrt(this->stddevScale);
119//   std::cerr << "Stddev scaling factor = " << this->stddevScale << "\n";
120}
121
122float *GaussSmooth::smooth(float *input, int xdim, int ydim)
123{
124  /**
125   * Smooth a given two-dimensional array, of dimensions xdim
126   * \f$\times\f$ ydim, with an elliptical gaussian. Simply runs as a
127   * front end to GaussSmooth::smooth(float *, int, int, bool *) by
128   * defining a mask that allows all pixels in the input array.
129   *
130   *  \param input The 2D array to be smoothed.
131   *  \param xdim  The size of the x-dimension of the array.
132   *  \param ydim  The size of the y-dimension of the array.
133   *  \return The smoothed array.
134   */
135  float *smoothed;
136  bool *mask = new bool[xdim*ydim];
137  for(int i=0;i<xdim*ydim;i++) mask[i]=true;
138  smoothed = this->smooth(input,xdim,ydim,mask);
139  delete [] mask;
140  return smoothed;
141}
142
143float *GaussSmooth::smooth(float *input, int xdim, int ydim, bool *mask)
144{
145  /**
146   *  Smooth a given two-dimensional array, of dimensions xdim
147   *  \f$\times\f$ ydim, with an elliptical gaussian, where the
148   *  boolean array mask defines which values of the array are valid.
149   *
150   *  This function convolves the input array with the kernel that
151   *  needs to have been defined. If it has not, the input array is
152   *  returned unchanged.
153   *
154   *  The mask should be the same size as the input array, and have
155   *  values of true for entries that are considered valid, and false
156   *  for entries that are not. For instance, arrays from FITS files
157   *  should have the mask entries corresponding to BLANK pixels set
158   *  to false.
159   *
160   *  \param input The 2D array to be smoothed.
161   *  \param xdim  The size of the x-dimension of the array.
162   *  \param ydim  The size of the y-dimension of the array.
163   *  \param mask The array showing which pixels in the input array
164   *              are valid.
165   *  \return The smoothed array.
166   */
167
168
169  if(!this->allocated) return input;
170  else{
171
172    float *output = new float[xdim*ydim];
173
174    int pos,comp,xcomp,ycomp,fpos,ct;
175    float fsum;
176    int kernelHW = this->kernWidth/2;
177
178    for(int ypos = 0; ypos<ydim; ypos++){
179      for(int xpos = 0; xpos<xdim; xpos++){
180        pos = ypos*xdim + xpos;
181     
182        if(!mask[pos]) output[pos] = input[pos];
183        else{
184       
185          ct=0;
186          fsum=0.;
187          output[pos] = 0.;
188       
189          for(int yoff = -kernelHW; yoff<=kernelHW; yoff++){
190            ycomp = ypos + yoff;
191            if((ycomp>=0)&&(ycomp<ydim)){
192
193              for(int xoff = -kernelHW; xoff<=kernelHW; xoff++){
194                xcomp = xpos + xoff;         
195                if((xcomp>=0)&&(xcomp<xdim)){
196
197                  fpos = (xoff+kernelHW) + (yoff+kernelHW)*this->kernWidth;
198                  comp = ycomp*xdim + xcomp;
199                  if(mask[comp]){
200                    ct++;
201                    fsum += this->kernel[fpos];
202                    output[pos] += input[comp]*this->kernel[fpos];
203                  }
204
205                }
206              } // xoff loop
207
208            }
209          }// yoff loop
210          if(ct>0) output[pos] /= fsum;
211
212        } // else{
213
214      } //xpos loop
215    }   //ypos loop
216   
217    return output;
218  }
219
220}
Note: See TracBrowser for help on using the repository browser.