EMAN::IterationAverager Class Reference

IterationAverager averages images by doing the smoothing iteration. More...

#include <averager.h>

Inheritance diagram for EMAN::IterationAverager:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 IterationAverager ()
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

Static Public Member Functions

static AveragerNEW ()

Static Public Attributes

static const string NAME = "iteration"

Private Attributes

EMDatasigma_image
int nimg

Detailed Description

IterationAverager averages images by doing the smoothing iteration.

Definition at line 302 of file averager.h.


Constructor & Destructor Documentation

IterationAverager::IterationAverager (  ) 

Definition at line 467 of file averager.cpp.

Referenced by NEW().

00467                                      : nimg(0)
00468 {
00469 
00470 }


Member Function Documentation

void IterationAverager::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 472 of file averager.cpp.

References EMAN::EMData::get_data(), get_name(), EMAN::EMUtil::is_same_size(), LOGERR, nimg, EMAN::Averager::result, EMAN::EMData::set_size(), and sigma_image.

00473 {
00474         if (!image) {
00475                 return;
00476         }
00477 
00478         if (nimg >= 1 && !EMUtil::is_same_size(image, result)) {
00479                 LOGERR("%sAverager can only process same-size Image",
00480                                                          get_name().c_str());
00481                 return;
00482         }
00483 
00484         nimg++;
00485 
00486         int nx = image->get_xsize();
00487         int ny = image->get_ysize();
00488         int nz = image->get_zsize();
00489         size_t image_size = (size_t)nx * ny * nz;
00490 
00491         if (nimg == 1) {
00492                 result = new EMData();
00493                 result->set_size(nx, ny, nz);
00494                 sigma_image = new EMData();
00495                 sigma_image->set_size(nx, ny, nz);
00496         }
00497 
00498         float *image_data = image->get_data();
00499         float *result_data = result->get_data();
00500         float *sigma_image_data = sigma_image->get_data();
00501 
00502         for (size_t j = 0; j < image_size; ++j) {
00503                 float f = image_data[j];
00504                 result_data[j] += f;
00505                 sigma_image_data[j] += f * f;
00506         }
00507 
00508 
00509 }

EMData * IterationAverager::finish (  )  [virtual]

Finish up the averaging and return the result.

Returns:
The averaged image.

Implements EMAN::Averager.

Definition at line 511 of file averager.cpp.

References EMAN::EMData::append_image(), EMAN::Util::eman_erfc(), EMAN::EMData::get_attr(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), nimg, EMAN::Averager::result, sigma_image, sqrt(), and EMAN::EMData::update().

00512 {
00513         if (nimg < 1) {
00514                 return result;
00515         }
00516 
00517         int nx = result->get_xsize();
00518         int ny = result->get_ysize();
00519         int nz = result->get_zsize();
00520         size_t image_size = (size_t)nx * ny * nz;
00521 
00522         float *result_data = result->get_data();
00523         float *sigma_image_data = sigma_image->get_data();
00524 
00525         for (size_t j = 0; j < image_size; ++j) {
00526                 result_data[j] /= nimg;
00527                 float f1 = sigma_image_data[j] / nimg;
00528                 float f2 = result_data[j];
00529                 sigma_image_data[j] = sqrt(f1 - f2 * f2) / sqrt((float)nimg);
00530         }
00531 
00532         result->update();
00533         sigma_image->update();
00534 
00535         result->append_image("iter.hed");
00536         float sigma = sigma_image->get_attr("sigma");
00537         float *sigma_image_data2 = sigma_image->get_data();
00538         float *result_data2 = result->get_data();
00539         float *d2 = new float[nx * ny];
00540         size_t sec_size = nx * ny * sizeof(float);
00541 
00542         memcpy(d2, result_data2, sec_size);
00543         memcpy(sigma_image_data2, result_data2, sec_size);
00544 
00545         printf("Iter sigma=%f\n", sigma);
00546 
00547         for (int k = 0; k < 1000; k++) {
00548                 for (int i = 1; i < nx - 1; i++) {
00549                         for (int j = 1; j < ny - 1; j++) {
00550                                 int l = i + j * nx;
00551                                 float c1 = (d2[l - 1] + d2[l + 1] + d2[l - nx] + d2[l + nx]) / 4.0f - d2[l];
00552                                 float c2 = fabs(result_data2[l] - sigma_image_data2[l]) / sigma;
00553                                 result_data2[l] += c1 * Util::eman_erfc(c2) / 100.0f;
00554                         }
00555                 }
00556 
00557                 memcpy(d2, result_data2, sec_size);
00558         }
00559 
00560         if( d2 )
00561         {
00562                 delete[]d2;
00563                 d2 = 0;
00564         }
00565 
00566         sigma_image->update();
00567         if( sigma_image )
00568         {
00569                 delete sigma_image;
00570                 sigma_image = 0;
00571         }
00572 
00573         result->update();
00574         result->append_image("iter.hed");
00575 
00576 
00577         return result;
00578 }

string EMAN::IterationAverager::get_desc (  )  const [inline, virtual]

Implements EMAN::Averager.

Definition at line 314 of file averager.h.

00315                 {
00316                         return "Unknown";
00317                 }

string EMAN::IterationAverager::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 309 of file averager.h.

References NAME.

Referenced by add_image().

00310                 {
00311                         return NAME;
00312                 }

static Averager* EMAN::IterationAverager::NEW (  )  [inline, static]

Definition at line 319 of file averager.h.

References IterationAverager().

00320                 {
00321                         return new IterationAverager();
00322                 }


Member Data Documentation

const string IterationAverager::NAME = "iteration" [static]

Definition at line 324 of file averager.h.

Referenced by get_name().

int EMAN::IterationAverager::nimg [private]

Definition at line 328 of file averager.h.

Referenced by add_image(), and finish().

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

Definition at line 327 of file averager.h.

Referenced by add_image(), and finish().


The documentation for this class was generated from the following files:
Generated on Tue Jul 12 13:48:03 2011 for EMAN2 by  doxygen 1.4.7