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

03673                 {
03674                         return "Remove gradient by least square plane fit";
03675                 }

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

References NAME.

Referenced by process_inplace().

03664                 {
03665                         return NAME;
03666                 }

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

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

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

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

Definition at line 3667 of file processor.h.

03668                 {
03669                         return new GradientPlaneRemoverProcessor();
03670                 }

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 2859 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.

02860 {
02861         if (!image) {
02862                 LOGWARN("NULL Image");
02863                 return;
02864         }
02865 
02866         int nz = image->get_zsize();
02867         if (nz > 1) {
02868                 LOGERR("%s Processor doesn't support 3D model", get_name().c_str());
02869                 throw ImageDimensionException("3D map not supported");
02870         }
02871 
02872         int nx = image->get_xsize();
02873         int ny = image->get_ysize();
02874         float *d = image->get_data();
02875         EMData *mask = 0;
02876         float *dm = 0;
02877         if (params.has_key("mask")) {
02878                 mask = params["mask"];
02879                 if (nx!=mask->get_xsize() || ny!=mask->get_ysize()) {
02880                         LOGERR("%s Processor requires same size mask image", get_name().c_str());
02881                         throw ImageDimensionException("wrong size mask image");
02882                 }
02883                 dm = mask->get_data();
02884         }
02885         int count = 0;
02886         if (dm) {
02887                 for(int i=0; i<nx*ny; i++) {
02888                         if(dm[i]) count++;
02889                 }
02890         }
02891         else {
02892                 count = nx * ny;
02893         }
02894         if(count<3) {
02895                 LOGERR("%s Processor requires at least 3 pixels to fit a plane", get_name().c_str());
02896                 throw ImageDimensionException("too few usable pixels to fit a plane");
02897         }
02898         // Allocate the working space
02899         gsl_vector *S=gsl_vector_calloc(3);
02900         gsl_matrix *A=gsl_matrix_calloc(count,3);
02901         gsl_matrix *V=gsl_matrix_calloc(3,3);
02902 
02903         double m[3] = {0, 0, 0};
02904         int index=0;
02905         if (dm) {
02906                 for(int j=0; j<ny; j++){
02907                         for(int i=0; i<nx; i++){
02908                                 int ij=j*nx+i;
02909                                 if(dm[ij]) {
02910                                         m[0]+=i;        // x
02911                                         m[1]+=j;        // y
02912                                         m[2]+=d[ij];    // z
02913                                         /*printf("index=%d/%d\ti,j=%d,%d\tval=%g\txm,ym,zm=%g,%g,%g\n", \
02914                                                 index,count,i,j,d[ij],m[0]/(index+1),m[1]/(index+1),m[2]/(index+1));*/
02915                                         index++;
02916                                 }
02917                         }
02918                 }
02919         }
02920         else {
02921                 for(int j=0; j<ny; j++){
02922                         for(int i=0; i<nx; i++){
02923                                 int ij=j*nx+i;
02924                                         m[0]+=i;        // x
02925                                         m[1]+=j;        // y
02926                                         m[2]+=d[ij];    // z
02927                                         /*printf("index=%d/%d\ti,j=%d,%d\tval=%g\txm,ym,zm=%g,%g,%g\n", \
02928                                                 index,count,i,j,d[ij],m[0]/(index+1),m[1]/(index+1),m[2]/(index+1));*/
02929                                         index++;
02930                         }
02931                 }
02932         }
02933 
02934         for(int i=0; i<3; i++) m[i]/=count;     // compute center of the plane
02935 
02936         index=0;
02937         if (dm) {
02938                 for(int j=0; j<ny; j++){
02939                         for(int i=0; i<nx; i++){
02940                                 int ij=j*nx+i;
02941                                 if(dm[ij]) {
02942                                         //printf("index=%d/%d\ti,j=%d,%d\tval=%g\n",index,count,i,j,d[index]);
02943                                         gsl_matrix_set(A,index,0,i-m[0]);
02944                                         gsl_matrix_set(A,index,1,j-m[1]);
02945                                         gsl_matrix_set(A,index,2,d[ij]-m[2]);
02946                                         index++;
02947                                 }
02948                         }
02949                 }
02950                 mask->update();
02951         }
02952         else {
02953                 for(int j=0; j<ny; j++){
02954                         for(int i=0; i<nx; i++){
02955                                 int ij=j*nx+i;
02956                                         //printf("index=%d/%d\ti,j=%d,%d\tval=%g\n",index,count,i,j,d[index]);
02957                                         gsl_matrix_set(A,index,0,i-m[0]);
02958                                         gsl_matrix_set(A,index,1,j-m[1]);
02959                                         gsl_matrix_set(A,index,2,d[ij]-m[2]);
02960                                         index++;
02961                         }
02962                 }
02963         }
02964 
02965         // SVD decomposition and use the V vector associated with smallest singular value as the plan normal
02966         gsl_linalg_SV_decomp_jacobi(A, V, S);
02967 
02968         double n[3];
02969         for(int i=0; i<3; i++) n[i] = gsl_matrix_get(V, i, 2);
02970 
02971         #ifdef DEBUG
02972         printf("S=%g,%g,%g\n",gsl_vector_get(S,0), gsl_vector_get(S,1), gsl_vector_get(S,2));
02973         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));
02974         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));
02975         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));
02976         printf("Fitted plane: p0=%g,%g,%g\tn=%g,%g,%g\n",m[0],m[1],m[2],n[0],n[1],n[2]);
02977         #endif
02978 
02979         int changeZero = 0;
02980         if (params.has_key("changeZero")) changeZero = params["changeZero"];
02981         if (changeZero) {
02982                 for(int j=0; j<nx; j++){
02983                         for(int i=0; i<ny; i++){
02984                                 int ij = j*nx+i;
02985                                 d[ij]-=static_cast<float>(-((i-m[0])*n[0]+(j-m[1])*n[1])/n[2]+m[2]);
02986                         }
02987                 }
02988         }
02989         else {
02990                 for(int j=0; j<nx; j++){
02991                         for(int i=0; i<ny; i++){
02992                                 int ij = j*nx+i;
02993                                 if(d[ij]) d[ij]-=static_cast<float>(-((i-m[0])*n[0]+(j-m[1])*n[1])/n[2]+m[2]);
02994                         }
02995                 }
02996         }
02997         image->update();
02998         // set return plane parameters
02999         vector< float > planeParam;
03000         planeParam.resize(6);
03001         for(int i=0; i<3; i++) planeParam[i] = static_cast<float>(n[i]);
03002         for(int i=0; i<3; i++) planeParam[i+3] = static_cast<float>(m[i]);
03003         params["planeParam"]=EMObject(planeParam);
03004 }


Member Data Documentation

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

Definition at line 3686 of file processor.h.

Referenced by get_name().


The documentation for this class was generated from the following files:
Generated on Thu May 3 10:10:14 2012 for EMAN2 by  doxygen 1.4.7