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

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

ProcessorNEW ()

Static Public Attributes

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:
n The shrink factor

Definition at line 3299 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:
to the smaller image that will store the calculated median values
from the larger image that will be used to calculate the median values
shrink_factor the shrink amount

Definition at line 1884 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().

Referenced by process(), and process_inplace().

01885 {
01886 
01887         int nx_old = from->get_xsize();
01888         int ny_old = from->get_ysize();
01889 
01890         int threed_shrink_factor = shrink_factor * shrink_factor;
01891         int z_shrink_factor = 1;
01892         if (from->get_zsize() > 1) {
01893                 threed_shrink_factor *= shrink_factor;
01894                 z_shrink_factor = shrink_factor;
01895         }
01896 
01897         float *mbuf = new float[threed_shrink_factor];
01898 
01899 
01900         int nxy_old = nx_old * ny_old;
01901 
01902         int nx = to->get_xsize();
01903         int ny = to->get_ysize();
01904         int nz = to->get_zsize();
01905         int nxy_new = nx * ny;
01906 
01907         float * rdata = to->get_data();
01908         const float *const data_copy = from->get_const_data();
01909 
01910         for (int l = 0; l < nz; l++) {
01911                 int l_min = l * shrink_factor;
01912                 int l_max = l * shrink_factor + z_shrink_factor;
01913                 int cur_l = l * nxy_new;
01914 
01915                 for (int j = 0; j < ny; j++) {
01916                         int j_min = j * shrink_factor;
01917                         int j_max = (j + 1) * shrink_factor;
01918                         int cur_j = j * nx + cur_l;
01919 
01920                         for (int i = 0; i < nx; i++) {
01921                                 int i_min = i * shrink_factor;
01922                                 int i_max = (i + 1) * shrink_factor;
01923 
01924                                 int k = 0;
01925                                 for (int l2 = l_min; l2 < l_max; l2++) {
01926                                         int cur_l2 = l2 * nxy_old;
01927 
01928                                         for (int j2 = j_min; j2 < j_max; j2++) {
01929                                                 int cur_j2 = j2 * nx_old + cur_l2;
01930 
01931                                                 for (int i2 = i_min; i2 < i_max; i2++) {
01932                                                         mbuf[k] = data_copy[i2 + cur_j2];
01933                                                         ++k;
01934                                                 }
01935                                         }
01936                                 }
01937 
01938                                 for (k = 0; k < threed_shrink_factor / 2 + 1; k++) {
01939                                         for (int i2 = k + 1; i2 < threed_shrink_factor; i2++) {
01940                                                 if (mbuf[i2] < mbuf[k]) {
01941                                                         float f = mbuf[i2];
01942                                                         mbuf[i2] = mbuf[k];
01943                                                         mbuf[k] = f;
01944                                                 }
01945                                         }
01946                                 }
01947 
01948                                 rdata[i + cur_j] = mbuf[threed_shrink_factor / 2];
01949                         }
01950                 }
01951         }
01952 
01953         if( mbuf )
01954         {
01955                 delete[]mbuf;
01956                 mbuf = 0;
01957         }
01958 
01959         to->scale_pixel((float)shrink_factor);
01960 }

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 3322 of file processor.h.

03323                 {
03324                         return "Shrink an image by a given amount , using the median value found in the pixel neighborhood.";
03325                 }

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 3327 of file processor.h.

03328                 {
03329                         return NAME;
03330                 }

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 3336 of file processor.h.

References EMAN::TypeDict::put().

03337                 {
03338                         TypeDict d;
03339                         d.put("n", EMObject::INT, "The shrink factor");
03340                         return d;
03341                 }

Processor* EMAN::MedianShrinkProcessor::NEW  )  [inline, static]
 

Definition at line 3331 of file processor.h.

03332                 {
03333                         return new MedianShrinkProcessor();
03334                 }

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:
image the image that will be used to generate a 'median shrunken' image
Exceptions:
ImageFormatException if the image is complex
InvalidValueException if the shrink amount is not a non zero, positive integer
InvalidValueException if any of the image dimensions are not divisible by the the shrink amount

Reimplemented from EMAN::Processor.

Definition at line 1851 of file processor.cpp.

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

01852 {
01853         if (image->is_complex()) throw ImageFormatException("Error, the median shrink processor does not work on complex images");
01854 
01855         int shrink_factor =  params.set_default("n",0);
01856         if (shrink_factor <= 1) {
01857                 throw InvalidValueException(shrink_factor,
01858                                                                         "median shrink: shrink factor must > 1");
01859         }
01860         int nx = image->get_xsize();
01861         int ny = image->get_ysize();
01862         int nz = image->get_zsize();
01863 
01864 
01865 //      if ((nx % shrink_factor != 0) || (ny % shrink_factor != 0) || (nz > 1 && (nz % shrink_factor != 0))) {
01866 //              throw InvalidValueException(shrink_factor, "Image size not divisible by shrink factor");
01867 //      }
01868 
01869 
01870         int shrunken_nx = nx / shrink_factor;
01871         int shrunken_ny = ny / shrink_factor;
01872         int shrunken_nz = 1;
01873         if (nz > 1) shrunken_nz = nz / shrink_factor;
01874 
01875 //      EMData* ret = new EMData(shrunken_nx, shrunken_ny, shrunken_nz);
01876         EMData *ret = image->copy_head();
01877         ret->set_size(shrunken_nx, shrunken_ny, shrunken_nz);
01878 
01879         accrue_median(ret,image,shrink_factor);
01880         ret->update();
01881         return ret;
01882 }

void MedianShrinkProcessor::process_inplace EMData image  )  [virtual]
 

Median shrink the image.

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

Implements EMAN::Processor.

Definition at line 1815 of file processor.cpp.

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

01816 {
01817         if (image->is_complex()) throw ImageFormatException("Error, the median shrink processor does not work on complex images");
01818 
01819         int shrink_factor =  params.set_default("n",0);
01820         if (shrink_factor <= 1) {
01821                 throw InvalidValueException(shrink_factor,
01822                                                                         "median shrink: shrink factor must > 1");
01823         }
01824 
01825         int nx = image->get_xsize();
01826         int ny = image->get_ysize();
01827         int nz = image->get_zsize();
01828 
01829 //      if ((nx % shrink_factor != 0) || (ny % shrink_factor != 0) || (nz > 1 && (nz % shrink_factor != 0))) {
01830 //              throw InvalidValueException(shrink_factor, "Image size not divisible by shrink factor");
01831 //      }
01832 
01833 
01834         int shrunken_nx = nx / shrink_factor;
01835         int shrunken_ny = ny / shrink_factor;
01836         int shrunken_nz = 1;
01837         if (nz > 1) shrunken_nz = nz / shrink_factor;
01838 
01839         EMData* copy = image->copy();
01840         image->set_size(shrunken_nx, shrunken_ny, shrunken_nz);
01841         accrue_median(image,copy,shrink_factor);
01842         image->update();
01843         if( copy )
01844         {
01845                 delete copy;
01846                 copy = 0;
01847         }
01848 }


Member Data Documentation

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

Definition at line 121 of file processor.cpp.


The documentation for this class was generated from the following files:
Generated on Thu Dec 9 13:47:58 2010 for EMAN2 by  doxygen 1.3.9.1