#include <processor.h>
Inheritance diagram for EMAN::MedianShrinkProcessor:
Public Member Functions | |
virtual EMData * | process (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 Processor * | NEW () |
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. |
n | The shrink factor |
Definition at line 3515 of file processor.h.
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.
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 2036 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(), rdata, and EMAN::EMData::scale_pixel().
Referenced by process(), and process_inplace().
02037 { 02038 02039 int nx_old = from->get_xsize(); 02040 int ny_old = from->get_ysize(); 02041 02042 int threed_shrink_factor = shrink_factor * shrink_factor; 02043 int z_shrink_factor = 1; 02044 if (from->get_zsize() > 1) { 02045 threed_shrink_factor *= shrink_factor; 02046 z_shrink_factor = shrink_factor; 02047 } 02048 02049 float *mbuf = new float[threed_shrink_factor]; 02050 02051 02052 int nxy_old = nx_old * ny_old; 02053 02054 int nx = to->get_xsize(); 02055 int ny = to->get_ysize(); 02056 int nz = to->get_zsize(); 02057 int nxy_new = nx * ny; 02058 02059 float * rdata = to->get_data(); 02060 const float *const data_copy = from->get_const_data(); 02061 02062 for (int l = 0; l < nz; l++) { 02063 int l_min = l * shrink_factor; 02064 int l_max = l * shrink_factor + z_shrink_factor; 02065 int cur_l = l * nxy_new; 02066 02067 for (int j = 0; j < ny; j++) { 02068 int j_min = j * shrink_factor; 02069 int j_max = (j + 1) * shrink_factor; 02070 int cur_j = j * nx + cur_l; 02071 02072 for (int i = 0; i < nx; i++) { 02073 int i_min = i * shrink_factor; 02074 int i_max = (i + 1) * shrink_factor; 02075 02076 int k = 0; 02077 for (int l2 = l_min; l2 < l_max; l2++) { 02078 int cur_l2 = l2 * nxy_old; 02079 02080 for (int j2 = j_min; j2 < j_max; j2++) { 02081 int cur_j2 = j2 * nx_old + cur_l2; 02082 02083 for (int i2 = i_min; i2 < i_max; i2++) { 02084 mbuf[k] = data_copy[i2 + cur_j2]; 02085 ++k; 02086 } 02087 } 02088 } 02089 02090 for (k = 0; k < threed_shrink_factor / 2 + 1; k++) { 02091 for (int i2 = k + 1; i2 < threed_shrink_factor; i2++) { 02092 if (mbuf[i2] < mbuf[k]) { 02093 float f = mbuf[i2]; 02094 mbuf[i2] = mbuf[k]; 02095 mbuf[k] = f; 02096 } 02097 } 02098 } 02099 02100 rdata[i + cur_j] = mbuf[threed_shrink_factor / 2]; 02101 } 02102 } 02103 } 02104 02105 if( mbuf ) 02106 { 02107 delete[]mbuf; 02108 mbuf = 0; 02109 } 02110 02111 to->scale_pixel((float)shrink_factor); 02112 }
string EMAN::MedianShrinkProcessor::get_desc | ( | ) | const [inline, virtual] |
Get the descrition of this specific processor.
This function must be overwritten by a subclass.
Implements EMAN::Processor.
Definition at line 3538 of file processor.h.
03539 { 03540 return "Shrink an image by a given amount , using the median value found in the pixel neighborhood."; 03541 }
virtual string EMAN::MedianShrinkProcessor::get_name | ( | ) | const [inline, virtual] |
Get the processor's name.
Each processor is identified by a unique name.
Implements EMAN::Processor.
Definition at line 3543 of file processor.h.
References NAME.
03544 { 03545 return NAME; 03546 }
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.
Reimplemented from EMAN::Processor.
Definition at line 3552 of file processor.h.
References EMAN::EMObject::INT, and EMAN::TypeDict::put().
03553 { 03554 TypeDict d; 03555 d.put("n", EMObject::INT, "The shrink factor"); 03556 return d; 03557 }
static Processor* EMAN::MedianShrinkProcessor::NEW | ( | ) | [inline, static] |
Definition at line 3547 of file processor.h.
03548 { 03549 return new MedianShrinkProcessor(); 03550 }
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
image | the image that will be used to generate a 'median shrunken' image |
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 2003 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(), EMAN::Processor::params, EMAN::Dict::set_default(), EMAN::EMData::set_size(), and EMAN::EMData::update().
02004 { 02005 if (image->is_complex()) throw ImageFormatException("Error, the median shrink processor does not work on complex images"); 02006 02007 int shrink_factor = params.set_default("n",0); 02008 if (shrink_factor <= 1) { 02009 throw InvalidValueException(shrink_factor, 02010 "median shrink: shrink factor must > 1"); 02011 } 02012 int nx = image->get_xsize(); 02013 int ny = image->get_ysize(); 02014 int nz = image->get_zsize(); 02015 02016 02017 // if ((nx % shrink_factor != 0) || (ny % shrink_factor != 0) || (nz > 1 && (nz % shrink_factor != 0))) { 02018 // throw InvalidValueException(shrink_factor, "Image size not divisible by shrink factor"); 02019 // } 02020 02021 02022 int shrunken_nx = nx / shrink_factor; 02023 int shrunken_ny = ny / shrink_factor; 02024 int shrunken_nz = 1; 02025 if (nz > 1) shrunken_nz = nz / shrink_factor; 02026 02027 // EMData* ret = new EMData(shrunken_nx, shrunken_ny, shrunken_nz); 02028 EMData *ret = image->copy_head(); 02029 ret->set_size(shrunken_nx, shrunken_ny, shrunken_nz); 02030 02031 accrue_median(ret,image,shrink_factor); 02032 ret->update(); 02033 return ret; 02034 }
void MedianShrinkProcessor::process_inplace | ( | EMData * | image | ) | [virtual] |
Median shrink the image.
image | the image the image that will be 'median shrunken' inplace |
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 1967 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(), EMAN::Processor::params, EMAN::Dict::set_default(), EMAN::EMData::set_size(), and EMAN::EMData::update().
01968 { 01969 if (image->is_complex()) throw ImageFormatException("Error, the median shrink processor does not work on complex images"); 01970 01971 int shrink_factor = params.set_default("n",0); 01972 if (shrink_factor <= 1) { 01973 throw InvalidValueException(shrink_factor, 01974 "median shrink: shrink factor must > 1"); 01975 } 01976 01977 int nx = image->get_xsize(); 01978 int ny = image->get_ysize(); 01979 int nz = image->get_zsize(); 01980 01981 // if ((nx % shrink_factor != 0) || (ny % shrink_factor != 0) || (nz > 1 && (nz % shrink_factor != 0))) { 01982 // throw InvalidValueException(shrink_factor, "Image size not divisible by shrink factor"); 01983 // } 01984 01985 01986 int shrunken_nx = nx / shrink_factor; 01987 int shrunken_ny = ny / shrink_factor; 01988 int shrunken_nz = 1; 01989 if (nz > 1) shrunken_nz = nz / shrink_factor; 01990 01991 EMData* copy = image->copy(); 01992 image->set_size(shrunken_nx, shrunken_ny, shrunken_nz); 01993 accrue_median(image,copy,shrink_factor); 01994 image->update(); 01995 if( copy ) 01996 { 01997 delete copy; 01998 copy = 0; 01999 } 02000 }
const string MedianShrinkProcessor::NAME = "math.medianshrink" [static] |