EMAN2
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions
EMAN::MedianShrinkProcessor Class Reference

MeanShrinkProcessor shrinks an image by in an integer amount taking the median of the pixel neighbourhood. More...

#include <processor.h>

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

List of all members.

Public Member Functions

virtual EMDataprocess (const EMData *const image)
 The medianshrink processor has its own process function to minise memory usage - if this function was not over written the base Processor class would create copy of the input image and hand it to the process_inplace function.
virtual void process_inplace (EMData *image)
 Median shrink the image.
string get_desc () const
 Get the descrition of this specific processor.
virtual string get_name () const
 Get the processor's name.
virtual TypeDict get_param_types () const
 Get processor parameter information in a dictionary.

Static Public Member Functions

static ProcessorNEW ()

Static Public Attributes

static const string NAME = "math.medianshrink"

Private Member Functions

void accrue_median (EMData *to, const EMData *const from, const int shrink_factor)
 Accrue the local median in the image 'from' to the image 'to' using the given shrinkfactor An internal function that encapsulates a routine common to both process and process inplace.

Detailed Description

MeanShrinkProcessor shrinks an image by in an integer amount taking the median of the pixel neighbourhood.

Author:
David Woolford (But is basically a copy of the old EMData::median_shrink, probably written by Steven Ludtke )
Date:
May 2008
Parameters:
nThe shrink factor

Definition at line 3708 of file processor.h.


Member Function Documentation

void MedianShrinkProcessor::accrue_median ( EMData to,
const EMData *const  from,
const int  shrink_factor 
) [private]

Accrue the local median in the image 'from' to the image 'to' using the given shrinkfactor An internal function that encapsulates a routine common to both process and process inplace.

Parameters:
tothe smaller image that will store the calculated median values
fromthe larger image that will be used to calculate the median values
shrink_factorthe shrink amount

Definition at line 2031 of file processor.cpp.

References EMAN::EMData::get_const_data(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), nx, ny, rdata, and EMAN::EMData::scale_pixel().

{

        int nx_old = from->get_xsize();
        int ny_old = from->get_ysize();

        int threed_shrink_factor = shrink_factor * shrink_factor;
        int z_shrink_factor = 1;
        if (from->get_zsize() > 1) {
                threed_shrink_factor *= shrink_factor;
                z_shrink_factor = shrink_factor;
        }

        float *mbuf = new float[threed_shrink_factor];


        int nxy_old = nx_old * ny_old;

        int nx = to->get_xsize();
        int ny = to->get_ysize();
        int nz = to->get_zsize();
        int nxy_new = nx * ny;

        float * rdata = to->get_data();
        const float *const data_copy = from->get_const_data();

        for (int l = 0; l < nz; l++) {
                int l_min = l * shrink_factor;
                int l_max = l * shrink_factor + z_shrink_factor;
                size_t cur_l = (size_t)l * nxy_new;

                for (int j = 0; j < ny; j++) {
                        int j_min = j * shrink_factor;
                        int j_max = (j + 1) * shrink_factor;
                        size_t cur_j = j * nx + cur_l;

                        for (int i = 0; i < nx; i++) {
                                int i_min = i * shrink_factor;
                                int i_max = (i + 1) * shrink_factor;

                                size_t k = 0;
                                for (int l2 = l_min; l2 < l_max; l2++) {
                                        size_t cur_l2 = l2 * nxy_old;

                                        for (int j2 = j_min; j2 < j_max; j2++) {
                                                size_t cur_j2 = j2 * nx_old + cur_l2;

                                                for (int i2 = i_min; i2 < i_max; i2++) {
                                                        mbuf[k] = data_copy[i2 + cur_j2];
                                                        ++k;
                                                }
                                        }
                                }

                                for (k = 0; k < size_t(threed_shrink_factor / 2 + 1); k++) {
                                        for (int i2 = k + 1; i2 < threed_shrink_factor; i2++) {
                                                if (mbuf[i2] < mbuf[k]) {
                                                        float f = mbuf[i2];
                                                        mbuf[i2] = mbuf[k];
                                                        mbuf[k] = f;
                                                }
                                        }
                                }

                                rdata[i + cur_j] = mbuf[threed_shrink_factor / 2];
                        }
                }
        }

        if( mbuf )
        {
                delete[]mbuf;
                mbuf = 0;
        }

        to->scale_pixel((float)shrink_factor);
}
string EMAN::MedianShrinkProcessor::get_desc ( ) const [inline, virtual]

Get the descrition of this specific processor.

This function must be overwritten by a subclass.

Returns:
The description of this processor.

Implements EMAN::Processor.

Definition at line 3731 of file processor.h.

                {
                        return "Shrink an image by a given amount , using the median value found in the pixel neighborhood.";
                }
virtual string EMAN::MedianShrinkProcessor::get_name ( ) const [inline, virtual]

Get the processor's name.

Each processor is identified by a unique name.

Returns:
The processor's name.

Implements EMAN::Processor.

Definition at line 3736 of file processor.h.

References NAME.

                {
                        return NAME;
                }
virtual TypeDict EMAN::MedianShrinkProcessor::get_param_types ( ) const [inline, virtual]

Get processor 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::Processor.

Definition at line 3745 of file processor.h.

References EMAN::EMObject::INT, and EMAN::TypeDict::put().

                {
                        TypeDict d;
                        d.put("n", EMObject::INT, "The shrink factor");
                        return d;
                }
static Processor* EMAN::MedianShrinkProcessor::NEW ( ) [inline, static]

Definition at line 3740 of file processor.h.

                {
                        return new MedianShrinkProcessor();
                }
EMData * MedianShrinkProcessor::process ( const EMData *const  image) [virtual]

The medianshrink processor has its own process function to minise memory usage - if this function was not over written the base Processor class would create copy of the input image and hand it to the process_inplace function.

This latter approach mallocs and copies more memory than necessary

Parameters:
imagethe image that will be used to generate a 'median shrunken' image
Exceptions:
ImageFormatExceptionif the image is complex
InvalidValueExceptionif the shrink amount is not a non zero, positive integer
InvalidValueExceptionif any of the image dimensions are not divisible by the the shrink amount

Reimplemented from EMAN::Processor.

Definition at line 1998 of file processor.cpp.

References EMAN::EMData::copy_head(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageFormatException, InvalidValueException, EMAN::EMData::is_complex(), EMAN::Processor::params, EMAN::Dict::set_default(), EMAN::EMData::set_size(), and EMAN::EMData::update().

{
        if (image->is_complex()) throw ImageFormatException("Error, the median shrink processor does not work on complex images");

        int shrink_factor =  params.set_default("n",0);
        if (shrink_factor <= 1) {
                throw InvalidValueException(shrink_factor,
                                                                        "median shrink: shrink factor must > 1");
        }
        int nx = image->get_xsize();
        int ny = image->get_ysize();
        int nz = image->get_zsize();


//      if ((nx % shrink_factor != 0) || (ny % shrink_factor != 0) || (nz > 1 && (nz % shrink_factor != 0))) {
//              throw InvalidValueException(shrink_factor, "Image size not divisible by shrink factor");
//      }


        int shrunken_nx = nx / shrink_factor;
        int shrunken_ny = ny / shrink_factor;
        int shrunken_nz = 1;
        if (nz > 1) shrunken_nz = nz / shrink_factor;

//      EMData* ret = new EMData(shrunken_nx, shrunken_ny, shrunken_nz);
        EMData *ret = image->copy_head();
        ret->set_size(shrunken_nx, shrunken_ny, shrunken_nz);

        accrue_median(ret,image,shrink_factor);
        ret->update();
        return ret;
}
void MedianShrinkProcessor::process_inplace ( EMData image) [virtual]

Median shrink the image.

Parameters:
imagethe image the image that will be 'median shrunken' inplace
Exceptions:
ImageFormatExceptionif the image is complex
InvalidValueExceptionif the shrink amount is not a non zero, positive integer
InvalidValueExceptionif any of the image dimensions are not divisible by the the shrink amount

Implements EMAN::Processor.

Definition at line 1962 of file processor.cpp.

References EMAN::EMData::copy(), copy(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageFormatException, InvalidValueException, EMAN::EMData::is_complex(), EMAN::Processor::params, EMAN::Dict::set_default(), EMAN::EMData::set_size(), and EMAN::EMData::update().

{
        if (image->is_complex()) throw ImageFormatException("Error, the median shrink processor does not work on complex images");

        int shrink_factor =  params.set_default("n",0);
        if (shrink_factor <= 1) {
                throw InvalidValueException(shrink_factor,
                                                                        "median shrink: shrink factor must > 1");
        }

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

//      if ((nx % shrink_factor != 0) || (ny % shrink_factor != 0) || (nz > 1 && (nz % shrink_factor != 0))) {
//              throw InvalidValueException(shrink_factor, "Image size not divisible by shrink factor");
//      }


        int shrunken_nx = nx / shrink_factor;
        int shrunken_ny = ny / shrink_factor;
        int shrunken_nz = 1;
        if (nz > 1) shrunken_nz = nz / shrink_factor;

        EMData* copy = image->copy();
        image->set_size(shrunken_nx, shrunken_ny, shrunken_nz);
        accrue_median(image,copy,shrink_factor);
        image->update();
        if( copy )
        {
                delete copy;
                copy = 0;
        }
}

Member Data Documentation

const string MedianShrinkProcessor::NAME = "math.medianshrink" [static]

Definition at line 3752 of file processor.h.

Referenced by get_name().


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