EMAN::KmeansSegmentProcessor Class Reference

Segment a volume into ~n subvolumes using K-means classification. More...

#include <processor.h>

Inheritance diagram for EMAN::KmeansSegmentProcessor:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

string get_name () const
 Get the processor's name.
virtual EMDataprocess (const EMData *const image)
 To proccess an image out-of-place.
void process_inplace (EMData *image)
 To process an image in-place.
TypeDict get_param_types () const
 Get processor parameter information in a dictionary.
string get_desc () const
 Get the descrition of this specific processor.

Static Public Member Functions

static ProcessorNEW ()

Static Public Attributes

static const string NAME = "segment.kmeans"

Detailed Description

Segment a volume into ~n subvolumes using K-means classification.

Author:
Steve Ludtke
Date:
2008/11/03
Parameters:
ctf[in] A Ctf object to use

Definition at line 745 of file processor.h.


Member Function Documentation

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

00776                 {
00777                         return "Performs K-means segmentation on a volume. Note that this method uses random seeds, and thus will return different results each time it is run. Returned map contains number of segment for each voxel (or 0 for unsegmented voxels). Segmentation centers are stored in 'segmentcenters' attribute, consisting of a list of 3n floats in x,y,z triples.";
00778                 }

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

References NAME.

00749                 {
00750                         return NAME;
00751                 }

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

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

00757                 {
00758                         TypeDict d ;
00759                         d.put("nseg", EMObject::INT, "Number of segments to divide the image into. default=12" );
00760                         d.put("thr",EMObject::FLOAT,"Isosurface threshold value. Pixels below this will not be segmented");
00761                         d.put("ampweight",EMObject::INT,"If set, will weight centers by voxel amplitude. default = 1");
00762                         d.put("maxsegsize",EMObject::FLOAT,"Maximum radial distance from segment center to member voxel. Default=10000");
00763                         d.put("minsegsep",EMObject::FLOAT,"Minimum segment separation. Segments too close will trigger a reseed");
00764                         d.put("maxiter",EMObject::FLOAT,"Maximum number of iterations to run before stopping. Default=100");
00765                         d.put("maxvoxmove",EMObject::FLOAT,"Maximum number of voxels that can move before quitting. Default=25");
00766                         d.put("verbose",EMObject::INT,"Be verbose while running");
00767                         return d;
00768                 }

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

Definition at line 770 of file processor.h.

00771                 {
00772                         return new KmeansSegmentProcessor();
00773                 }

EMData * KmeansSegmentProcessor::process ( const EMData *const   image  )  [virtual]

To proccess an image out-of-place.

For those processors which can only be processed out-of-place, override this function to give the right behavior.

Parameters:
image The image will be copied, actual process happen on copy of image.
Returns:
the image processing result, may or may not be the same size of the input image

Reimplemented from EMAN::Processor.

Definition at line 907 of file processor.cpp.

References EMAN::EMData::copy(), EMAN::Util::get_frand(), EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), EMAN::Util::hypot3(), nx, ny, EMAN::Processor::params, EMAN::EMData::set_attr(), EMAN::Dict::set_default(), EMAN::EMData::set_value_at(), x, and y.

00908 {
00909         EMData * result = image->copy();
00910 
00911         int nseg = params.set_default("nseg",12);
00912         float thr = params.set_default("thr",-1.0e30f);
00913         int ampweight = params.set_default("ampweight",1);
00914         float maxsegsize = params.set_default("maxsegsize",10000.0f);
00915         float minsegsep = params.set_default("minsegsep",0.0f);
00916         int maxiter = params.set_default("maxiter",100);
00917         int maxvoxmove = params.set_default("maxvoxmove",25);
00918         int verbose = params.set_default("verbose",0);
00919 
00920         vector<float> centers(nseg*3);
00921         vector<float> count(nseg);
00922         int nx=image->get_xsize();
00923         int ny=image->get_ysize();
00924         int nz=image->get_zsize();
00925 //      int nxy=nx*ny;
00926 
00927         // seed
00928         for (int i=0; i<nseg*3; i+=3) {
00929                 centers[i]=  Util::get_frand(0.0f,(float)nx);
00930                 centers[i+1]=Util::get_frand(0.0f,(float)ny);
00931                 centers[i+2]=Util::get_frand(0.0f,(float)nz);
00932         }
00933 
00934         for (int iter=0; iter<maxiter; iter++) {
00935                 // **** classify
00936                 size_t pixmov=0;                // count of moved pixels
00937                 for (int z=0; z<nz; z++) {
00938                         for (int y=0; y<ny; y++) {
00939                                 for (int x=0; x<nz; x++) {
00940                                         if (image->get_value_at(x,y,z)<thr) {
00941                                                 result->set_value_at(x,y,z,-1.0);               //below threshold -> -1 (unclassified)
00942                                                 continue;
00943                                         }
00944                                         int bcls=-1;                    // best matching class
00945                                         float bdist=(float)(nx+ny+nz);  // distance for best class
00946                                         for (int c=0; c<nseg; c++) {
00947                                                 float d=Util::hypot3(x-centers[c*3],y-centers[c*3+1],z-centers[c*3+2]);
00948                                                 if (d<bdist) { bdist=d; bcls=c; }
00949                                         }
00950                                         if ((int)result->get_value_at(x,y,z)!=bcls) pixmov++;
00951                                         if (bdist>maxsegsize) result->set_value_at(x,y,z,-1);           // pixel is too far from any center
00952                                         else result->set_value_at(x,y,z,(float)bcls);           // set the pixel to the class number
00953                                 }
00954                         }
00955                 }
00956 
00957                 // **** adjust centers
00958                 for (int i=0; i<nseg*3; i++) centers[i]=0;
00959                 for (int i=0; i<nseg; i++) count[i]=0;
00960 
00961                 // weighted sums
00962                 for (int z=0; z<nz; z++) {
00963                         for (int y=0; y<ny; y++) {
00964                                 for (int x=0; x<nz; x++) {
00965                                         int cls = (int)result->get_value_at(x,y,z);
00966                                         if (cls==-1) continue;
00967                                         float w=1.0;
00968                                         if (ampweight) w=image->get_value_at(x,y,z);
00969 
00970                                         centers[cls*3]+=x*w;
00971                                         centers[cls*3+1]+=y*w;
00972                                         centers[cls*3+2]+=z*w;
00973                                         count[cls]+=w;
00974                                 }
00975                         }
00976                 }
00977 
00978                 // now each becomes center of mass, or gets randomly reseeded
00979                 int nreseed=0;
00980                 for (int c=0; c<nseg; c++) {
00981                         // reseed
00982                         if (count[c]==0) {
00983                                 nreseed++;
00984                                 do {
00985                                         centers[c*3]=  Util::get_frand(0.0f,(float)nx);
00986                                         centers[c*3+1]=Util::get_frand(0.0f,(float)ny);
00987                                         centers[c*3+2]=Util::get_frand(0.0f,(float)nz);
00988                                 } while (image->get_value_at((int)centers[c*3],(int)centers[c*3+1],(int)centers[c*3+2])<thr);           // This makes sure the new point is inside density
00989                         }
00990                         // center of mass
00991                         else {
00992                                 centers[c*3]/=count[c];
00993                                 centers[c*3+1]/=count[c];
00994                                 centers[c*3+2]/=count[c];
00995                         }
00996                 }
00997 
00998                 // with minsegsep, check separation
00999                 if (minsegsep>0) {
01000                         for (int c1=0; c1<nseg-1; c1++) {
01001                                 for (int c2=c1+1; c2<nseg; c2++) {
01002                                         if (Util::hypot3(centers[c1*3]-centers[c2*3],centers[c1*3+1]-centers[c2*3+1],centers[c1*3+2]-centers[c2*3+2])<=minsegsep) {
01003                                                 nreseed++;
01004                                                 do {
01005                                                         centers[c1*3]=  Util::get_frand(0.0f,(float)nx);
01006                                                         centers[c1*3+1]=Util::get_frand(0.0f,(float)ny);
01007                                                         centers[c1*3+2]=Util::get_frand(0.0f,(float)nz);
01008                                                 } while (image->get_value_at((int)centers[c1*3],(int)centers[c1*3+1],(int)centers[c1*3+2])<thr);
01009                                         }
01010                                 }
01011                         }
01012                 }
01013 
01014 
01015                 if (verbose) printf("Iteration %3d: %6ld voxels moved, %3d classes reseeded\n",iter,pixmov,nreseed);
01016                 if (nreseed==0 && pixmov<(size_t)maxvoxmove) break;             // termination conditions met
01017         }
01018 
01019         result->set_attr("segment_centers",centers);
01020 
01021         return result;
01022 }

void KmeansSegmentProcessor::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 1024 of file processor.cpp.

01025 {
01026         printf("Process inplace not implemented. Please use process.\n");
01027         return;
01028 }


Member Data Documentation

const string KmeansSegmentProcessor::NAME = "segment.kmeans" [static]

Definition at line 780 of file processor.h.

Referenced by get_name().


The documentation for this class was generated from the following files:
Generated on Tue Jun 11 12:43:30 2013 for EMAN2 by  doxygen 1.4.7