Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

EMAN::ImageAverager Class Reference

ImageAverager averages a list of images. More...

#include <averager.h>

Inheritance diagram for EMAN::ImageAverager:

Inheritance graph
[legend]
Collaboration diagram for EMAN::ImageAverager:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ImageAverager ()
void add_image (EMData *image)
 To add an image to the Averager.
EMDatafinish ()
 Finish up the averaging and return the result.
string get_name () const
 Get the Averager's name.
string get_desc () const
TypeDict get_param_types () const
 Get Averager parameter information in a dictionary.
virtual void mult (const float &)
 Multiply the result image by some floating point constant This is useful when weighting the input images prior to calling add_image - a situation where it is likely you want to divide by the sum of the weights.

Static Public Member Functions

AveragerNEW ()

Static Public Attributes

const string NAME = "mean"

Private Attributes

EMDatasigma_image
int * nimg_n0
int ignore0
int nimg

Detailed Description

ImageAverager averages a list of images.

It optionally makes a sigma image.

Parameters:
sigma sigma value
ignore0 if set, ignore zero value pixels

Definition at line 170 of file averager.h.


Constructor & Destructor Documentation

ImageAverager::ImageAverager  ) 
 

Definition at line 188 of file averager.cpp.

00189         : sigma_image(0), nimg_n0(0), ignore0(0), nimg(0)
00190 {
00191 
00192 }


Member Function Documentation

void ImageAverager::add_image EMData image  )  [virtual]
 

To add an image to the Averager.

This image will be averaged in this function.

Parameters:
image The image to be averaged.

Implements EMAN::Averager.

Definition at line 194 of file averager.cpp.

References EMAN::EMData::copy_head(), EMAN::EMData::get_data(), get_name(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ignore0, EMAN::EMUtil::is_same_size(), LOGERR, nimg, nimg_n0, nx, ny, EMAN::Dict::set_default(), EMAN::EMData::set_size(), and sigma_image.

00195 {
00196         if (!image) {
00197                 return;
00198         }
00199 
00200         if (nimg >= 1 && !EMUtil::is_same_size(image, result)) {
00201                 LOGERR("%sAverager can only process same-size Image",
00202                            get_name().c_str());
00203                 return;
00204         }
00205 
00206         nimg++;
00207 
00208         int nx = image->get_xsize();
00209         int ny = image->get_ysize();
00210         int nz = image->get_zsize();
00211         size_t image_size = (size_t)nx * ny * nz;
00212 
00213         if (nimg == 1) {
00214                 result = image->copy_head();
00215                 result->set_size(nx, ny, nz);
00216                 sigma_image = params.set_default("sigma", (EMData*)0);
00217                 ignore0 = params["ignore0"];
00218 
00219                 nimg_n0 = new int[image_size];
00220                 for (size_t i = 0; i < image_size; ++i) {
00221                         nimg_n0[i] = 0;
00222                 }
00223         }
00224 
00225         float *result_data = result->get_data();
00226         float *sigma_image_data = 0;
00227         if (sigma_image) {
00228                 sigma_image->set_size(nx, ny, nz);
00229                 sigma_image_data = sigma_image->get_data();
00230         }
00231 
00232         float * image_data = image->get_data();
00233 
00234         if (!ignore0) {
00235                 for (size_t j = 0; j < image_size; ++j) {
00236                         float f = image_data[j];
00237                         result_data[j] += f;
00238                         if (sigma_image_data) {
00239                                 sigma_image_data[j] += f * f;
00240                         }
00241                 }
00242         }
00243         else {
00244                 for (size_t j = 0; j < image_size; ++j) {
00245                         float f = image_data[j];
00246                         if (f) {
00247                                 result_data[j] += f;
00248                                 if (sigma_image_data) {
00249                                         sigma_image_data[j] += f * f;
00250                                 }
00251                                 nimg_n0[j]++;
00252                         }
00253                 }
00254         }
00255 }

EMData * ImageAverager::finish  )  [virtual]
 

Finish up the averaging and return the result.

Returns:
The averaged image.

Implements EMAN::Averager.

Definition at line 257 of file averager.cpp.

References EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), nimg, nimg_n0, EMAN::EMData::set_attr(), sigma_image, sqrt(), and EMAN::EMData::update().

00258 {
00259         if (result && nimg > 1) {
00260                 size_t image_size = (size_t)result->get_xsize() * result->get_ysize() * result->get_zsize();
00261                 float * result_data = result->get_data();
00262 
00263                 if (!ignore0) {
00264                         for (size_t j = 0; j < image_size; ++j) {
00265                                 result_data[j] /= nimg;
00266                         }
00267 
00268                         if (sigma_image) {
00269                                 float * sigma_image_data = sigma_image->get_data();
00270 
00271                                 for (size_t j = 0; j < image_size; ++j) {
00272                                         float f1 = sigma_image_data[j] / nimg;
00273                                         float f2 = result_data[j];
00274                                         sigma_image_data[j] = sqrt(f1 - f2 * f2);
00275                                 }
00276 
00277                                 sigma_image->update();
00278                         }
00279                 }
00280                 else {
00281                         for (size_t j = 0; j < image_size; ++j) {
00282                                 if (nimg_n0[j]>0) result_data[j] /= nimg_n0[j];
00283                         }
00284                         if (sigma_image) {
00285                                 float * sigma_image_data = sigma_image->get_data();
00286 
00287                                 for (size_t j = 0; j < image_size; ++j) {
00288                                         float f1 = sigma_image_data[j] / nimg_n0[j];
00289                                         float f2 = result_data[j];
00290                                         sigma_image_data[j] = sqrt(f1 - f2 * f2);
00291                                 }
00292 
00293                                 sigma_image->update();
00294                         }
00295                 }
00296 
00297                 result->update();
00298 
00299         }
00300         result->set_attr("ptcl_repr",nimg);
00301 
00302         if( nimg_n0 )
00303         {
00304                 delete [] nimg_n0;
00305                 nimg_n0 = 0;
00306         }
00307 
00308         return result;
00309 }

string EMAN::ImageAverager::get_desc  )  const [inline, virtual]
 

Implements EMAN::Averager.

Definition at line 183 of file averager.h.

00184                 {
00185                         return "Simple mean average of images";
00186                 }

string EMAN::ImageAverager::get_name  )  const [inline, virtual]
 

Get the Averager's name.

Each Averager is identified by a unique name.

Returns:
The Averager's name.

Implements EMAN::Averager.

Definition at line 178 of file averager.h.

Referenced by add_image().

00179                 {
00180                         return NAME;
00181                 }

TypeDict EMAN::ImageAverager::get_param_types  )  const [inline, virtual]
 

Get Averager parameter information in a dictionary.

Each parameter has one record in the dictionary. Each record contains its name, data-type, and description.

Returns:
A dictionary containing the parameter info.

Reimplemented from EMAN::Averager.

Definition at line 193 of file averager.h.

References EMAN::TypeDict::put().

00194                 {
00195                         TypeDict d;
00196                         d.put("sigma", EMObject::EMDATA, "sigma value");
00197                         d.put("ignore0", EMObject::INT, "if set, ignore zero value pixels");
00198                         return d;
00199                 }

virtual void EMAN::ImageAverager::mult const float &   )  [inline, virtual]
 

Multiply the result image by some floating point constant This is useful when weighting the input images prior to calling add_image - a situation where it is likely you want to divide by the sum of the weights.

Hence call mult after all of the weighted images have been added.

Parameters:
s the scaling factor.
Exceptions:
NullPointerException if the EMData pointer (result) is NULL

Reimplemented from EMAN::Averager.

Definition at line 201 of file averager.h.

00201 { }

Averager* EMAN::ImageAverager::NEW  )  [inline, static]
 

Definition at line 188 of file averager.h.

00189                 {
00190                         return new ImageAverager();
00191                 }


Member Data Documentation

int EMAN::ImageAverager::ignore0 [private]
 

Definition at line 208 of file averager.h.

Referenced by add_image().

const string ImageAverager::NAME = "mean" [static]
 

Definition at line 45 of file averager.cpp.

int EMAN::ImageAverager::nimg [private]
 

Definition at line 209 of file averager.h.

Referenced by add_image(), and finish().

int* EMAN::ImageAverager::nimg_n0 [private]
 

Definition at line 207 of file averager.h.

Referenced by add_image(), and finish().

EMData* EMAN::ImageAverager::sigma_image [private]
 

Definition at line 206 of file averager.h.

Referenced by add_image(), and finish().


The documentation for this class was generated from the following files:
Generated on Mon Aug 13 13:41:33 2012 for EMAN2 by  doxygen 1.3.9.1