EMAN::GradientPlaneRemoverProcessor Class Reference

Gradient removed by least square plane fit. More...

#include <processor.h>

Inheritance diagram for EMAN::GradientPlaneRemoverProcessor:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

void process_inplace (EMData *image)
 To process an image in-place.
string get_name () const
 Get the processor's name.
string get_desc () const
 Get the descrition of this specific processor.
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 = "filter.gradientPlaneRemover"

Detailed Description

Gradient removed by least square plane fit.

Parameters:
mask[in] optional EMData object to mask the pixels used to fit the plane
changeZero[in] optional bool to specify if the zero value pixels are modified
planeParam[out] optional return parameters [nx, ny, nz, cx, cy, cz] for the fitted plane defined as (x-cx)*nx+(y-cy)*ny+(z-cz)*nz=0
Author:
Wen Jiang
Date:
2006/7/18

Definition at line 3654 of file processor.h.


Member Function Documentation

string EMAN::GradientPlaneRemoverProcessor::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 3668 of file processor.h.

03669                 {
03670                         return "Remove gradient by least square plane fit";
03671                 }

string EMAN::GradientPlaneRemoverProcessor::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 3659 of file processor.h.

References NAME.

Referenced by process_inplace().

03660                 {
03661                         return NAME;
03662                 }

TypeDict EMAN::GradientPlaneRemoverProcessor::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 3673 of file processor.h.

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

03674                 {
03675                         TypeDict d;
03676                         d.put("mask", EMObject::EMDATA, "mask object: nonzero pixel positions will be used to fit plane. default = 0");
03677                         d.put("changeZero", EMObject::INT, "if zero pixels are modified when removing gradient. default = 0");
03678                         d.put("planeParam", EMObject::FLOATARRAY, "fitted plane parameters output");
03679                         return d;
03680                 }

static Processor* EMAN::GradientPlaneRemoverProcessor::NEW (  )  [inline, static]

Definition at line 3663 of file processor.h.

03664                 {
03665                         return new GradientPlaneRemoverProcessor();
03666                 }

void GradientPlaneRemoverProcessor::process_inplace ( EMData image  )  [virtual]

To process an image in-place.

For those processors which can only be processed out-of-place, override this function to just print out some error message to remind user call the out-of-place version.

Parameters:
image The image to be processed.

Implements EMAN::Processor.

Definition at line 2800 of file processor.cpp.

References dm, EMAN::EMData::get_data(), get_name(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), EMAN::Dict::has_key(), ImageDimensionException, LOGERR, LOGWARN, EMAN::Processor::params, EMAN::EMData::update(), and V.

02801 {
02802         if (!image) {
02803                 LOGWARN("NULL Image");
02804                 return;
02805         }
02806 
02807         int nz = image->get_zsize();
02808         if (nz > 1) {
02809                 LOGERR("%s Processor doesn't support 3D model", get_name().c_str());
02810                 throw ImageDimensionException("3D map not supported");
02811         }
02812 
02813         int nx = image->get_xsize();
02814         int ny = image->get_ysize();
02815         float *d = image->get_data();
02816         EMData *mask = 0;
02817         float *dm = 0;
02818         if (params.has_key("mask")) {
02819                 mask = params["mask"];
02820                 if (nx!=mask->get_xsize() || ny!=mask->get_ysize()) {
02821                         LOGERR("%s Processor requires same size mask image", get_name().c_str());
02822                         throw ImageDimensionException("wrong size mask image");
02823                 }
02824                 dm = mask->get_data();
02825         }
02826         int count = 0;
02827         if (dm) {
02828                 for(int i=0; i<nx*ny; i++) {
02829                         if(dm[i]) count++;
02830                 }
02831         }
02832         else {
02833                 count = nx * ny;
02834         }
02835         if(count<3) {
02836                 LOGERR("%s Processor requires at least 3 pixels to fit a plane", get_name().c_str());
02837                 throw ImageDimensionException("too few usable pixels to fit a plane");
02838         }
02839         // Allocate the working space
02840         gsl_vector *S=gsl_vector_calloc(3);
02841         gsl_matrix *A=gsl_matrix_calloc(count,3);
02842         gsl_matrix *V=gsl_matrix_calloc(3,3);
02843 
02844         double m[3] = {0, 0, 0};
02845         int index=0;
02846         if (dm) {
02847                 for(int j=0; j<ny; j++){
02848                         for(int i=0; i<nx; i++){
02849                                 int ij=j*nx+i;
02850                                 if(dm[ij]) {
02851                                         m[0]+=i;        // x
02852                                         m[1]+=j;        // y
02853                                         m[2]+=d[ij];    // z
02854                                         /*printf("index=%d/%d\ti,j=%d,%d\tval=%g\txm,ym,zm=%g,%g,%g\n", \
02855                                                 index,count,i,j,d[ij],m[0]/(index+1),m[1]/(index+1),m[2]/(index+1));*/
02856                                         index++;
02857                                 }
02858                         }
02859                 }
02860         }
02861         else {
02862                 for(int j=0; j<ny; j++){
02863                         for(int i=0; i<nx; i++){
02864                                 int ij=j*nx+i;
02865                                         m[0]+=i;        // x
02866                                         m[1]+=j;        // y
02867                                         m[2]+=d[ij];    // z
02868                                         /*printf("index=%d/%d\ti,j=%d,%d\tval=%g\txm,ym,zm=%g,%g,%g\n", \
02869                                                 index,count,i,j,d[ij],m[0]/(index+1),m[1]/(index+1),m[2]/(index+1));*/
02870                                         index++;
02871                         }
02872                 }
02873         }
02874 
02875         for(int i=0; i<3; i++) m[i]/=count;     // compute center of the plane
02876 
02877         index=0;
02878         if (dm) {
02879                 for(int j=0; j<ny; j++){
02880                         for(int i=0; i<nx; i++){
02881                                 int ij=j*nx+i;
02882                                 if(dm[ij]) {
02883                                         //printf("index=%d/%d\ti,j=%d,%d\tval=%g\n",index,count,i,j,d[index]);
02884                                         gsl_matrix_set(A,index,0,i-m[0]);
02885                                         gsl_matrix_set(A,index,1,j-m[1]);
02886                                         gsl_matrix_set(A,index,2,d[ij]-m[2]);
02887                                         index++;
02888                                 }
02889                         }
02890                 }
02891                 mask->update();
02892         }
02893         else {
02894                 for(int j=0; j<ny; j++){
02895                         for(int i=0; i<nx; i++){
02896                                 int ij=j*nx+i;
02897                                         //printf("index=%d/%d\ti,j=%d,%d\tval=%g\n",index,count,i,j,d[index]);
02898                                         gsl_matrix_set(A,index,0,i-m[0]);
02899                                         gsl_matrix_set(A,index,1,j-m[1]);
02900                                         gsl_matrix_set(A,index,2,d[ij]-m[2]);
02901                                         index++;
02902                         }
02903                 }
02904         }
02905 
02906         // SVD decomposition and use the V vector associated with smallest singular value as the plan normal
02907         gsl_linalg_SV_decomp_jacobi(A, V, S);
02908 
02909         double n[3];
02910         for(int i=0; i<3; i++) n[i] = gsl_matrix_get(V, i, 2);
02911 
02912         #ifdef DEBUG
02913         printf("S=%g,%g,%g\n",gsl_vector_get(S,0), gsl_vector_get(S,1), gsl_vector_get(S,2));
02914         printf("V[0,:]=%g,%g,%g\n",gsl_matrix_get(V,0,0), gsl_matrix_get(V,0,1),gsl_matrix_get(V,0,2));
02915         printf("V[1,:]=%g,%g,%g\n",gsl_matrix_get(V,1,0), gsl_matrix_get(V,1,1),gsl_matrix_get(V,1,2));
02916         printf("V[2,:]=%g,%g,%g\n",gsl_matrix_get(V,2,0), gsl_matrix_get(V,2,1),gsl_matrix_get(V,2,2));
02917         printf("Fitted plane: p0=%g,%g,%g\tn=%g,%g,%g\n",m[0],m[1],m[2],n[0],n[1],n[2]);
02918         #endif
02919 
02920         int changeZero = 0;
02921         if (params.has_key("changeZero")) changeZero = params["changeZero"];
02922         if (changeZero) {
02923                 for(int j=0; j<nx; j++){
02924                         for(int i=0; i<ny; i++){
02925                                 int ij = j*nx+i;
02926                                 d[ij]-=static_cast<float>(-((i-m[0])*n[0]+(j-m[1])*n[1])/n[2]+m[2]);
02927                         }
02928                 }
02929         }
02930         else {
02931                 for(int j=0; j<nx; j++){
02932                         for(int i=0; i<ny; i++){
02933                                 int ij = j*nx+i;
02934                                 if(d[ij]) d[ij]-=static_cast<float>(-((i-m[0])*n[0]+(j-m[1])*n[1])/n[2]+m[2]);
02935                         }
02936                 }
02937         }
02938         image->update();
02939         // set return plane parameters
02940         vector< float > planeParam;
02941         planeParam.resize(6);
02942         for(int i=0; i<3; i++) planeParam[i] = static_cast<float>(n[i]);
02943         for(int i=0; i<3; i++) planeParam[i+3] = static_cast<float>(m[i]);
02944         params["planeParam"]=EMObject(planeParam);
02945 }


Member Data Documentation

const string GradientPlaneRemoverProcessor::NAME = "filter.gradientPlaneRemover" [static]

Definition at line 3682 of file processor.h.

Referenced by get_name().


The documentation for this class was generated from the following files:
Generated on Mon Jul 19 12:43:33 2010 for EMAN2 by  doxygen 1.4.7