EMAN2
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes
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

static AveragerNEW ()

Static Public Attributes

static const string NAME = "mean"

Private Attributes

EMDatasigma_image
EMDatanormimage
int ignore0
int nimg
int freenorm

Detailed Description

ImageAverager averages a list of images.

It optionally makes a sigma image.

Parameters:
sigmasigma value
ignore0if set, ignore zero value pixels

Definition at line 170 of file averager.h.


Constructor & Destructor Documentation

ImageAverager::ImageAverager ( )

Definition at line 183 of file averager.cpp.

Referenced by NEW().

        : sigma_image(0), ignore0(0), normimage(0), freenorm(0), nimg(0)
{

}

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:
imageThe image to be averaged.

Implements EMAN::Averager.

Definition at line 189 of file averager.cpp.

References EMAN::EMData::copy_head(), freenorm, EMAN::EMData::get_data(), get_name(), EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ignore0, EMAN::EMUtil::is_same_size(), LOGERR, nimg, normimage, nx, ny, EMAN::Averager::params, EMAN::Averager::result, EMAN::Dict::set_default(), EMAN::EMData::set_size(), EMAN::EMData::set_value_at_fast(), sigma_image, and EMAN::EMData::to_zero().

{
        if (!image) {
                return;
        }

        if (nimg >= 1 && !EMUtil::is_same_size(image, result)) {
                LOGERR("%sAverager can only process same-size Image",
                           get_name().c_str());
                return;
        }

        nimg++;

        int nx = image->get_xsize();
        int ny = image->get_ysize();
        int nz = image->get_zsize();
        size_t image_size = (size_t)nx * ny * nz;

        if (nimg == 1) {
                result = image->copy_head();
                result->set_size(nx, ny, nz);
                sigma_image = params.set_default("sigma", (EMData*)0);
                ignore0 = params["ignore0"];

                normimage = params.set_default("normimage", (EMData*)0);
                if (ignore0 && normimage==0) { normimage=new EMData(nx,ny,nz); freenorm=1; }
                if (normimage) normimage->to_zero();
        }

        float *result_data = result->get_data();
        float *sigma_image_data = 0;
        if (sigma_image) {
                sigma_image->set_size(nx, ny, nz);
                sigma_image_data = sigma_image->get_data();
        }

        float * image_data = image->get_data();

        if (!ignore0) {
                for (size_t j = 0; j < image_size; ++j) {
                        float f = image_data[j];
                        result_data[j] += f;
                        if (sigma_image_data) {
                                sigma_image_data[j] += f * f;
                        }
                }
        }
        else {
                for (size_t j = 0; j < image_size; ++j) {
                        float f = image_data[j];
                        if (f) {
                                result_data[j] += f;
                                if (sigma_image_data) {
                                        sigma_image_data[j] += f * f;
                                }
                                normimage->set_value_at_fast(j,normimage->get_value_at(j)+1.0);
                        }
                }
        }
}
EMData * ImageAverager::finish ( ) [virtual]

Finish up the averaging and return the result.

Returns:
The averaged image.

Implements EMAN::Averager.

Definition at line 251 of file averager.cpp.

References freenorm, EMAN::EMData::get_data(), EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ignore0, nimg, normimage, EMAN::Averager::result, EMAN::EMData::set_attr(), sigma_image, sqrt(), and EMAN::EMData::update().

{
        if (result && nimg > 1) {
                size_t image_size = (size_t)result->get_xsize() * result->get_ysize() * result->get_zsize();
                float * result_data = result->get_data();

                if (!ignore0) {
                        for (size_t j = 0; j < image_size; ++j) {
                                result_data[j] /= nimg;
                        }

                        if (sigma_image) {
                                float * sigma_image_data = sigma_image->get_data();

                                for (size_t j = 0; j < image_size; ++j) {
                                        float f1 = sigma_image_data[j] / nimg;
                                        float f2 = result_data[j];
                                        sigma_image_data[j] = sqrt(f1 - f2 * f2);
                                }

                                sigma_image->update();
                        }
                }
                else {
                        for (size_t j = 0; j < image_size; ++j) {
                                if (normimage->get_value_at(j)>0) result_data[j] /= normimage->get_value_at(j);
                        }
                        if (sigma_image) {
                                float * sigma_image_data = sigma_image->get_data();

                                for (size_t j = 0; j < image_size; ++j) {
                                        float f1 = 0;
                                        if (normimage->get_value_at(j)>0) f1=sigma_image_data[j] / normimage->get_value_at(j);
                                        float f2 = result_data[j];
                                        sigma_image_data[j] = sqrt(f1 - f2 * f2);
                                }

                                sigma_image->update();
                        }
                }

                result->update();

        }               
        result->set_attr("ptcl_repr",nimg);

        if (freenorm) { delete normimage; normimage=(EMData*)0; }

        return result;
}
string EMAN::ImageAverager::get_desc ( ) const [inline, virtual]

Implements EMAN::Averager.

Definition at line 183 of file averager.h.

                {
                        return "Simple mean average of images";
                }
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.

References NAME.

Referenced by add_image().

                {
                        return NAME;
                }
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::EMObject::EMDATA, EMAN::EMObject::INT, and EMAN::TypeDict::put().

                {
                        TypeDict d;
                        d.put("sigma", EMObject::EMDATA, "sigma value");
                        d.put("normimage", EMObject::EMDATA, "In conjunction with ignore0, the number of non zero values for each pixel will be stored in this image.");
                        d.put("ignore0", EMObject::INT, "if set, ignore zero value pixels");
                        return d;
                }
virtual void EMAN::ImageAverager::mult ( const float &  s) [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:
sthe scaling factor.
Exceptions:
NullPointerExceptionif the EMData pointer (result) is NULL

Reimplemented from EMAN::Averager.

Definition at line 202 of file averager.h.

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

Definition at line 188 of file averager.h.

References ImageAverager().

                {
                        return new ImageAverager();
                }

Member Data Documentation

Definition at line 210 of file averager.h.

Referenced by add_image(), and finish().

Definition at line 208 of file averager.h.

Referenced by add_image(), and finish().

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

Definition at line 204 of file averager.h.

Referenced by get_name().

Definition at line 209 of file averager.h.

Referenced by add_image(), and finish().

Definition at line 207 of file averager.h.

Referenced by add_image(), and finish().

Definition at line 207 of file averager.h.

Referenced by add_image(), and finish().


The documentation for this class was generated from the following files: