EMAN::BoxingTools Class Reference

BoxingTools is class for encapsulating common boxing operations that may become expensive if they are implemented in python. More...

#include <boxingtools.h>

List of all members.

Public Types

 SWARM_DIFFERENCE
 SWARM_RATIO
 SWARM_AVERAGE_RATIO
enum  CmpMode { SWARM_DIFFERENCE, SWARM_RATIO, SWARM_AVERAGE_RATIO }

Public Member Functions

 BoxingTools ()
 ~BoxingTools ()

Static Public Member Functions

static vector< float > get_min_delta_profile (const EMData *const image, int x, int y, int radius)
 Gets a pixel minimum delta radial profile about some pixel focal point.
static bool is_local_maximum (const EMData *const image, int x, int y, int radius, EMData *const exclusion_map)
 Determines if a given pixel is the maximum in local radial neighborhood Useful for automated correlation-based boxing.
static vector< IntPointauto_correlation_pick (const EMData *const image, float threshold, int radius, const vector< float > &profile, EMData *const exclusion, const int cradius, int mode=1)
static bool hi_brid (const EMData *const image, int x, int y, int radius, EMData *const exclusion_map, vector< float > &profile)
static void set_radial_non_zero (EMData *const exclusion, int x, int y, int radius)
static IntPoint find_radial_max (const EMData *const map, int x, int y, int radius)
static map< unsigned int,
unsigned int > 
classify (const vector< vector< float > > &data, const unsigned int &classes=4)
static Vec3f get_color (const unsigned int index)
static void set_region (EMData *const image, const EMData *const mask, const int x, const int y, const float &val)
static void set_mode (const CmpMode m)

Static Private Attributes

static vector< Vec3fcolors = vector<Vec3f>()
static CmpMode mode = SWARM_AVERAGE_RATIO


Detailed Description

BoxingTools is class for encapsulating common boxing operations that may become expensive if they are implemented in python.

Author:
David Woolford
Date:
April 2008

Definition at line 60 of file boxingtools.h.


Member Enumeration Documentation

enum EMAN::BoxingTools::CmpMode

Enumerator:
SWARM_DIFFERENCE 
SWARM_RATIO 
SWARM_AVERAGE_RATIO 

Definition at line 106 of file boxingtools.h.

00106                              {
00107                         SWARM_DIFFERENCE,
00108                         SWARM_RATIO,
00109                         SWARM_AVERAGE_RATIO
00110                 };


Constructor & Destructor Documentation

EMAN::BoxingTools::BoxingTools (  )  [inline]

Definition at line 63 of file boxingtools.h.

00063 {}

EMAN::BoxingTools::~BoxingTools (  )  [inline]

Definition at line 64 of file boxingtools.h.

00064 {}


Member Function Documentation

vector< IntPoint > BoxingTools::auto_correlation_pick ( const EMData *const   image,
float  threshold,
int  radius,
const vector< float > &  profile,
EMData *const   exclusion,
const int  cradius,
int  mode = 1 
) [static]

Definition at line 612 of file boxingtools.cpp.

References EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), hi_brid(), InvalidValueException, nx, ny, and set_radial_non_zero().

00613 {
00614         if (mode < 0 || mode > 2 ) {
00615                 throw InvalidValueException(mode,"Error, the mode can only be 0,1, or 2.");
00616         }
00617 
00618         if ( radius < 0) {
00619                 throw InvalidValueException(radius,"Radius must be greater than 1");
00620         }
00621 
00622         if ( cradius < 0) {
00623                 throw InvalidValueException(cradius,"CRadius must be greater than 1");
00624         }
00625 
00626 
00627         int nx = image->get_xsize();
00628         int ny = image->get_ysize();
00629 
00630         vector<IntPoint> solution;
00631 
00632         int r = radius+1;
00633 
00634         for(int j = r; j < ny-r;++j) {
00635                 for(int k = r; k < nx-r;++k) {
00636 
00637                         if (exclusion->get_value_at(k,j) != 0 ) continue;
00638 
00639                         if (image->get_value_at(k,j) < threshold) continue;
00640                         if ( mode == 0 ) {
00641                                 solution.push_back(IntPoint(k,j));
00642                                 set_radial_non_zero(exclusion,k,j,radius);
00643                                 continue;
00644                         }
00645 
00646                         vector<float> p(r,0);
00647 
00648                         if (hi_brid(image,k,j,r,exclusion,p)) {
00649                                 if ( mode == 1 ) {
00650                                         if (p[cradius] >= profile[cradius]) {
00651                                                 solution.push_back(IntPoint(k,j));
00652                                                 set_radial_non_zero(exclusion,k,j,radius);
00653                                                 continue;
00654                                         }
00655                                 }
00656                                 else /* mode == 2 */{
00657                                         bool bad = false;
00658                                         for (int ii = 0; ii <= cradius; ++ii) {
00659                                                 if (p[ii] < profile[ii]) {
00660                                                         bad = true;
00661                                                         break;
00662                                                 }
00663                                         }
00664                                         if (bad) continue;
00665                                         solution.push_back(IntPoint(k,j));
00666                                         set_radial_non_zero(exclusion,k,j,radius);
00667                                 }
00668 
00669 
00670                         }
00671                 }
00672         }
00673         return solution;
00674 }

map< unsigned int, unsigned int > BoxingTools::classify ( const vector< vector< float > > &  data,
const unsigned int &  classes = 4 
) [static]

Definition at line 854 of file boxingtools.cpp.

References EMAN::BoxSVDClassifier::colorMappingByClassSize(), data, and EMAN::BoxSVDClassifier::go().

00855 {
00856         BoxSVDClassifier classifier(data, classes);
00857         map< unsigned int, unsigned int> mapping = classifier.go();
00858 
00859         mapping = BoxSVDClassifier::colorMappingByClassSize( mapping );
00860 
00861         return mapping;
00862 
00863 }

IntPoint BoxingTools::find_radial_max ( const EMData *const   map,
int  x,
int  y,
int  radius 
) [static]

Definition at line 783 of file boxingtools.cpp.

References EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), and EMAN::EMData::get_ysize().

00784 {
00785         float currentmax = map->get_value_at(x,y);
00786 
00787         IntPoint soln(x,y);
00788 
00789         int radius_squared = radius*radius;
00790         for(int k = -radius; k <= radius; ++k) {
00791                 for(int j = -radius; j <= radius; ++j) {
00792                         // Translate coordinates
00793                         int xx = x+j;
00794                         int yy = y+k;
00795 
00796                         // Protect against accessing pixels out of bounds
00797                         if ( xx >= map->get_xsize() || xx < 0 ) continue;
00798                         if ( yy >= map->get_ysize() || yy < 0 ) continue;
00799 
00800                         // Protect against vector accesses beyond the boundary
00801                         int square_length = k*k + j*j;
00802                         if (square_length > radius_squared ) continue;
00803 
00804                         float val = map->get_value_at(xx,yy);
00805 
00806                         if (val > currentmax) {
00807                                 currentmax = val;
00808                                 soln[0] = xx;
00809                                 soln[1] = yy;
00810                         }
00811                 }
00812         }
00813 
00814         return soln;
00815 }

Vec3f BoxingTools::get_color ( const unsigned int  index  )  [static]

Definition at line 866 of file boxingtools.cpp.

References colors.

00867 {
00868         if ( colors.size() == 0 ) {
00869                 colors.push_back(Vec3f(0,0,0));
00870                 colors.push_back(Vec3f(0,0,1));
00871                 colors.push_back(Vec3f(0,1,0));
00872                 colors.push_back(Vec3f(1,0,0));
00873                 colors.push_back(Vec3f(1,1,0));
00874                 colors.push_back(Vec3f(1,0,1));
00875                 colors.push_back(Vec3f(0,1,1));
00876                 colors.push_back(Vec3f(1,1,1));
00877         }
00878         if ( index >= colors.size() )
00879         {
00880                 while ( colors.size() <= index )
00881                 {
00882                         bool found = false;
00883                         while ( !found )
00884                         {
00885                                 unsigned int random_idx = rand() % colors.size();
00886                                 unsigned int random_idx2 = rand() % colors.size();
00887                                 while ( random_idx2 == random_idx )
00888                                 {
00889                                         random_idx2 = rand() % colors.size();
00890                                 }
00891 
00892                                 Vec3f result = (colors[random_idx] + colors[random_idx2])/2.0;
00893                                 if ( find( colors.begin(), colors.end(), result ) == colors.end() )
00894                                 {
00895                                         colors.push_back( result );
00896                                         found = true;
00897                                 }
00898                         }
00899                 }
00900         }
00901         return colors[index];
00902 }

vector< float > BoxingTools::get_min_delta_profile ( const EMData *const   image,
int  x,
int  y,
int  radius 
) [static]

Gets a pixel minimum delta radial profile about some pixel focal point.

Useful for automated correlation-based boxing.

Parameters:
image the image containing the interesting pixels values (typically a correlation image)
x the x coordinate of the pixel focal point
y the y coordinate of the pixel focal point
radius the constraining radius of the minimum delta profile
Author:
David Woolford
Date:
April 2008

Definition at line 502 of file boxingtools.cpp.

References EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), mode, SWARM_AVERAGE_RATIO, SWARM_DIFFERENCE, and SWARM_RATIO.

00503 {
00504         float peakval = image->get_value_at(x,y);
00505 
00506         vector<float> profile(radius,0); // this sets the vectors size to radius, and the values to 0
00507         int radius_squared = radius*radius;
00508 
00509         static vector<float> squared_numbers;
00510         if ( (unsigned int)(radius+1) > squared_numbers.size() ) {
00511                 for(int i = squared_numbers.size(); i <= radius; ++i) {
00512                         squared_numbers.push_back((float)(i*i));
00513                 }
00514         }
00515 
00516         vector<unsigned int> tally;
00517         if (mode == SWARM_AVERAGE_RATIO) tally.resize(profile.size(),0);
00518 
00519         for(int k = -radius; k <= radius; ++k) {
00520                 for(int j = -radius; j <= radius; ++j) {
00521                         // Translate coordinates
00522                         int xx = x+j;
00523                         int yy = y+k;
00524 
00525                         // Protect against accessing pixels out of bounds
00526                         if ( xx >= image->get_xsize() || xx < 0 ) continue;
00527                         if ( yy >= image->get_ysize() || yy < 0 ) continue;
00528 
00529                         // We don't need to pay attention to the origin
00530                         if ( xx == x && yy == y) continue;
00531 
00532                         // Protect against vector accesses beyond the boundary
00533                         int square_length = k*k + j*j;
00534                         if (square_length > radius_squared ) continue;
00535 
00536                         // The idx is the radius, rounded down. This creates a certain type of pattern that
00537                         // can only really be explained visually...
00538                         int idx = -1;
00539                         // This little loop avoids the use of sqrtf
00540                         for(int i = 1; i < radius; ++i) {
00541                                 if ( square_length >= squared_numbers[i] && square_length <= squared_numbers[i+1] ) {
00542                                         idx = i;
00543                                 }
00544                         }
00545 //                      int idx = (int) sqrtf(k*k + j*j);
00546                         // decrement the idx, because the origin information is redundant
00547                         idx -= 1;
00548 
00549                         if ( mode == SWARM_DIFFERENCE ) {
00550                                 // Finally, get the drop relative to the origin
00551                                 float val = peakval - image->get_value_at(xx,yy);
00552 
00553                                 // Store it if the drop is smaller than the current value (or if there is no value)
00554                                 if ( profile[idx] > val || profile[idx] == 0 ) profile[idx] = val;
00555                         }
00556                         else if (mode == SWARM_RATIO) {
00557                                 // Finally, get the drop relative to the origin
00558                                 float val =  (peakval - image->get_value_at(xx,yy) ) / peakval;
00559 
00560                                 // Store it if the drop is smaller than the current value (or if there is no value)
00561                                 if ( profile[idx] > val || profile[idx] == 0 ) profile[idx] = val;
00562                         }
00563                         else if (mode == SWARM_AVERAGE_RATIO) {
00564                                 profile[idx] += image->get_value_at(xx,yy);
00565                                 tally[idx]++;
00566                         }
00567 
00568                 }
00569         }
00570 
00571         if (mode == SWARM_AVERAGE_RATIO) {
00572                 for(unsigned int i = 0; i < profile.size(); ++i) {
00573                         if (tally[i] != 0) {
00574                                 profile[i] /= static_cast<float>(tally[i]);
00575                                 profile[i] = (peakval - profile[i] ) / peakval;
00576                         }
00577                 }
00578         }
00579 
00580         return profile;
00581 }

bool BoxingTools::hi_brid ( const EMData *const   image,
int  x,
int  y,
int  radius,
EMData *const   exclusion_map,
vector< float > &  profile 
) [static]

Definition at line 677 of file boxingtools.cpp.

References EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), mode, set_radial_non_zero(), SWARM_AVERAGE_RATIO, SWARM_DIFFERENCE, and SWARM_RATIO.

Referenced by auto_correlation_pick().

00678 {
00679 
00680         float peakval = image->get_value_at(x,y);
00681 
00682         int radius_squared = radius*radius;
00683 
00684         static vector<float> squared_numbers;
00685         if ( (unsigned int)(radius+1) > squared_numbers.size() ) {
00686                 for(int i = squared_numbers.size(); i <= radius; ++i) {
00687                         squared_numbers.push_back((float)(i*i));
00688                 }
00689         }
00690 
00691         vector<unsigned int> tally;
00692         if (mode == SWARM_AVERAGE_RATIO) tally.resize(profile.size(),0);
00693 
00694         for(int k = -radius; k <= radius; ++k) {
00695                 for(int j = -radius; j <= radius; ++j) {
00696                         // Translate coordinates
00697                         int xx = x+j;
00698                         int yy = y+k;
00699 
00700                         // Protect against accessing pixels out of bounds
00701                         if ( xx >= image->get_xsize() || xx < 0 ) continue;
00702                         if ( yy >= image->get_ysize() || yy < 0 ) continue;
00703 
00704                         // We don't need to pay attention to the origin
00705                         if ( xx == x && yy == y) continue;
00706 
00707                         // Protect against vector accesses beyond the boundary
00708                         int square_length = k*k + j*j;
00709                         if (square_length > radius_squared ) continue;
00710 
00711                         // It's not a local maximum!
00712                         if ( image->get_value_at(xx,yy) > peakval)  return false;
00713 
00714                         // The idx is the radius, rounded down. This creates a certain type of pattern that
00715                         // can only really be explained visually...
00716                         int idx = -1;
00717                         // This little loop avoids the use of sqrtf
00718                         for(int i = 1; i < radius; ++i) {
00719                                 if ( square_length >= squared_numbers[i] && square_length <= squared_numbers[i+1] ) {
00720                                         idx = i;
00721                                 }
00722                         }
00723 //                      int idx = (int) sqrtf(k*k + j*j);
00724                         // decrement the idx, because the origin information is redundant
00725                         idx -= 1;
00726 
00727                         if (mode == SWARM_DIFFERENCE) {
00728                                 // Finally, get the drop relative to the origin
00729                                 float val = peakval - image->get_value_at(xx,yy);
00730 
00731                                 // Store it if the drop is smaller than the current value (or if there is no value)
00732                                 if ( profile[idx] > val || profile[idx] == 0 ) profile[idx] = val;
00733                         }
00734                         else if (mode == SWARM_RATIO) {
00735                                 // Finally, get the drop relative to the origin
00736                                 float val =  (peakval - image->get_value_at(xx,yy) ) / peakval;
00737 
00738                                 // Store it if the drop is smaller than the current value (or if there is no value)
00739                                 if ( profile[idx] > val || profile[idx] == 0 ) profile[idx] = val;
00740                         }
00741                         else if (mode == SWARM_AVERAGE_RATIO) {
00742                                 profile[idx] += image->get_value_at(xx,yy);
00743                                 tally[idx]++;
00744                         }
00745 
00746                 }
00747         }
00748 
00749         if (mode == SWARM_AVERAGE_RATIO) {
00750                 for(unsigned int i = 0; i < profile.size(); ++i) {
00751                         if (tally[i] != 0) {
00752                                 profile[i] /= static_cast<float>(tally[i]);
00753                                 profile[i] = (peakval - profile[i] ) / peakval;
00754                         }
00755                 }
00756         }
00757 
00758         set_radial_non_zero(exclusion_map,x,y,radius);
00759 
00760         return true;
00761 }

bool BoxingTools::is_local_maximum ( const EMData *const   image,
int  x,
int  y,
int  radius,
EMData *const   exclusion_map 
) [static]

Determines if a given pixel is the maximum in local radial neighborhood Useful for automated correlation-based boxing.

Parameters:
image the image containing the interesting pixels values (typically a correlation image)
x the x coordinate of the candidate pixel maximum
y the y coordinate of the candidate pixel maximum
radius the constraining radius of the local neighborhood
exclusion_map an EMData object with the same dimensions as the main image. If a local maximum is found, the pixels in the radial neighborhood that was queried are set to 0. This can be exploited by the calling function to minimize queries.
Author:
David Woolford
Date:
April 2008

Definition at line 583 of file boxingtools.cpp.

References EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), and set_radial_non_zero().

00584 {
00585         float peakval = image->get_value_at(x,y);
00586         int radius_squared = radius*radius;
00587         for(int k = -radius; k <= radius; ++k) {
00588                 for(int j = -radius; j <= radius; ++j) {
00589                         // Translate coordinates
00590                         int xx = x+j;
00591                         int yy = y+k;
00592 
00593 //                      // Protect against accessing pixel out of bounds
00594                         if ( xx >= image->get_xsize() || xx < 0 ) continue;
00595                         if ( yy >= image->get_ysize() || yy < 0 ) continue;
00596 
00597                         // We don't need to pay attention to the origin
00598                         if ( xx == x && yy == y) continue;
00599 
00600                         if ((k*k+j*j)>radius_squared) continue;
00601 
00602                         if ( image->get_value_at(xx,yy) > peakval)  return false;
00603                 }
00604         }
00605 
00606         set_radial_non_zero(exclusion_map,x,y,radius);
00607 
00608         return true;
00609 
00610 }

static void EMAN::BoxingTools::set_mode ( const CmpMode  m  )  [inline, static]

Definition at line 112 of file boxingtools.h.

References mode.

00112 { mode = m; }

void BoxingTools::set_radial_non_zero ( EMData *const   exclusion,
int  x,
int  y,
int  radius 
) [static]

Definition at line 764 of file boxingtools.cpp.

References EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), and EMAN::EMData::set_value_at().

Referenced by auto_correlation_pick(), hi_brid(), and is_local_maximum().

00765 {
00766         int radius_squared = radius*radius;
00767         for(int k = -radius; k <= radius; ++k) {
00768                 for(int j = -radius; j <= radius; ++j) {
00769                         // Translate coordinates
00770                         int xx = x+j;
00771                         int yy = y+k;
00772 
00773                         if ((k*k+j*j)>radius_squared) continue;
00774                         // Protect against accessing pixel out of bounds
00775                         if ( xx >= exclusion->get_xsize() || xx < 0 ) continue;
00776                         if ( yy >= exclusion->get_ysize() || yy < 0 ) continue;
00777 
00778                         exclusion->set_value_at(xx,yy,1);
00779                 }
00780         }
00781 }

void BoxingTools::set_region ( EMData *const   image,
const EMData *const   mask,
const int  x,
const int  y,
const float &  val 
) [static]

Definition at line 818 of file boxingtools.cpp.

References abs, EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), and EMAN::EMData::set_value_at().

00818                                                                                                                        {
00819 
00820         // Works only in 2D
00821         int inx = image->get_xsize();
00822         int iny = image->get_ysize();
00823         int mnx = mask->get_xsize();
00824         int mny = mask->get_ysize();
00825 
00826         int startx = x-mnx/2;
00827         int endx =startx + mnx;
00828         int xoffset = 0;
00829         if (startx < 0) {
00830                 xoffset = abs(startx);
00831                 startx = 0;
00832         }
00833         if (endx > inx) endx = inx;
00834 
00835         int starty = y-mny/2;
00836         int endy =starty + mny;
00837         int yoffset = 0;
00838         if (starty < 0) {
00839                 yoffset = abs(starty);
00840                 starty = 0;
00841         }
00842         if (endy > iny) endy = iny;
00843 
00844 
00845         for (int j = starty; j < endy; ++j ) {
00846                 for (int i = startx; i < endx; ++i) {
00847                         if (mask->get_value_at(xoffset+i-startx,yoffset+j-starty) != 0 ) {
00848                                 image->set_value_at(i,j,val);
00849                         }
00850                 }
00851         }
00852 }


Member Data Documentation

vector< Vec3f > BoxingTools::colors = vector<Vec3f>() [static, private]

Definition at line 116 of file boxingtools.h.

Referenced by get_color().

BoxingTools::CmpMode BoxingTools::mode = SWARM_AVERAGE_RATIO [static, private]

Definition at line 117 of file boxingtools.h.

Referenced by get_min_delta_profile(), hi_brid(), and set_mode().


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