EMAN2
Public Types | Public Member Functions | Static Public Member Functions | Static Private Attributes
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>

Collaboration diagram for EMAN::BoxingTools:
Collaboration graph
[legend]

List of all members.

Public Types

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

Enumerator:
SWARM_DIFFERENCE 
SWARM_RATIO 
SWARM_AVERAGE_RATIO 

Definition at line 106 of file boxingtools.h.


Constructor & Destructor Documentation

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

Definition at line 63 of file boxingtools.h.

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

Definition at line 64 of file boxingtools.h.

{}

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().

{
        if (mode < 0 || mode > 2 ) {
                throw InvalidValueException(mode,"Error, the mode can only be 0,1, or 2.");
        }

        if ( radius < 0) {
                throw InvalidValueException(radius,"Radius must be greater than 1");
        }

        if ( cradius < 0) {
                throw InvalidValueException(cradius,"CRadius must be greater than 1");
        }


        int nx = image->get_xsize();
        int ny = image->get_ysize();

        vector<IntPoint> solution;

        int r = radius+1;

        for(int j = r; j < ny-r;++j) {
                for(int k = r; k < nx-r;++k) {

                        if (exclusion->get_value_at(k,j) != 0 ) continue;

                        if (image->get_value_at(k,j) < threshold) continue;
                        if ( mode == 0 ) {
                                solution.push_back(IntPoint(k,j));
                                set_radial_non_zero(exclusion,k,j,radius);
                                continue;
                        }

                        vector<float> p(r,0);

                        if (hi_brid(image,k,j,r,exclusion,p)) {
                                if ( mode == 1 ) {
                                        if (p[cradius] >= profile[cradius]) {
                                                solution.push_back(IntPoint(k,j));
                                                set_radial_non_zero(exclusion,k,j,radius);
                                                continue;
                                        }
                                }
                                else /* mode == 2 */{
                                        bool bad = false;
                                        for (int ii = 0; ii <= cradius; ++ii) {
                                                if (p[ii] < profile[ii]) {
                                                        bad = true;
                                                        break;
                                                }
                                        }
                                        if (bad) continue;
                                        solution.push_back(IntPoint(k,j));
                                        set_radial_non_zero(exclusion,k,j,radius);
                                }


                        }
                }
        }
        return solution;
}
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().

{
        BoxSVDClassifier classifier(data, classes);
        map< unsigned int, unsigned int> mapping = classifier.go();

        mapping = BoxSVDClassifier::colorMappingByClassSize( mapping );

        return mapping;

}
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().

{
        float currentmax = map->get_value_at(x,y);

        IntPoint soln(x,y);

        int radius_squared = radius*radius;
        for(int k = -radius; k <= radius; ++k) {
                for(int j = -radius; j <= radius; ++j) {
                        // Translate coordinates
                        int xx = x+j;
                        int yy = y+k;

                        // Protect against accessing pixels out of bounds
                        if ( xx >= map->get_xsize() || xx < 0 ) continue;
                        if ( yy >= map->get_ysize() || yy < 0 ) continue;

                        // Protect against vector accesses beyond the boundary
                        int square_length = k*k + j*j;
                        if (square_length > radius_squared ) continue;

                        float val = map->get_value_at(xx,yy);

                        if (val > currentmax) {
                                currentmax = val;
                                soln[0] = xx;
                                soln[1] = yy;
                        }
                }
        }

        return soln;
}
Vec3f BoxingTools::get_color ( const unsigned int  index) [static]

Definition at line 866 of file boxingtools.cpp.

References colors.

{
        if ( colors.size() == 0 ) {
                colors.push_back(Vec3f(0,0,0));
                colors.push_back(Vec3f(0,0,1));
                colors.push_back(Vec3f(0,1,0));
                colors.push_back(Vec3f(1,0,0));
                colors.push_back(Vec3f(1,1,0));
                colors.push_back(Vec3f(1,0,1));
                colors.push_back(Vec3f(0,1,1));
                colors.push_back(Vec3f(1,1,1));
        }
        if ( index >= colors.size() )
        {
                while ( colors.size() <= index )
                {
                        bool found = false;
                        while ( !found )
                        {
                                unsigned int random_idx = rand() % colors.size();
                                unsigned int random_idx2 = rand() % colors.size();
                                while ( random_idx2 == random_idx )
                                {
                                        random_idx2 = rand() % colors.size();
                                }

                                Vec3f result = (colors[random_idx] + colors[random_idx2])/2.0;
                                if ( find( colors.begin(), colors.end(), result ) == colors.end() )
                                {
                                        colors.push_back( result );
                                        found = true;
                                }
                        }
                }
        }
        return colors[index];
}
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:
imagethe image containing the interesting pixels values (typically a correlation image)
xthe x coordinate of the pixel focal point
ythe y coordinate of the pixel focal point
radiusthe 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.

{
        float peakval = image->get_value_at(x,y);

        vector<float> profile(radius,0); // this sets the vectors size to radius, and the values to 0
        int radius_squared = radius*radius;

        static vector<float> squared_numbers;
        if ( (unsigned int)(radius+1) > squared_numbers.size() ) {
                for(int i = squared_numbers.size(); i <= radius; ++i) {
                        squared_numbers.push_back((float)(i*i));
                }
        }

        vector<unsigned int> tally;
        if (mode == SWARM_AVERAGE_RATIO) tally.resize(profile.size(),0);

        for(int k = -radius; k <= radius; ++k) {
                for(int j = -radius; j <= radius; ++j) {
                        // Translate coordinates
                        int xx = x+j;
                        int yy = y+k;

                        // Protect against accessing pixels out of bounds
                        if ( xx >= image->get_xsize() || xx < 0 ) continue;
                        if ( yy >= image->get_ysize() || yy < 0 ) continue;

                        // We don't need to pay attention to the origin
                        if ( xx == x && yy == y) continue;

                        // Protect against vector accesses beyond the boundary
                        int square_length = k*k + j*j;
                        if (square_length > radius_squared ) continue;

                        // The idx is the radius, rounded down. This creates a certain type of pattern that
                        // can only really be explained visually...
                        int idx = -1;
                        // This little loop avoids the use of sqrtf
                        for(int i = 1; i < radius; ++i) {
                                if ( square_length >= squared_numbers[i] && square_length <= squared_numbers[i+1] ) {
                                        idx = i;
                                }
                        }
//                      int idx = (int) sqrtf(k*k + j*j);
                        // decrement the idx, because the origin information is redundant
                        idx -= 1;

                        if ( mode == SWARM_DIFFERENCE ) {
                                // Finally, get the drop relative to the origin
                                float val = peakval - image->get_value_at(xx,yy);

                                // Store it if the drop is smaller than the current value (or if there is no value)
                                if ( profile[idx] > val || profile[idx] == 0 ) profile[idx] = val;
                        }
                        else if (mode == SWARM_RATIO) {
                                // Finally, get the drop relative to the origin
                                float val =  (peakval - image->get_value_at(xx,yy) ) / peakval;

                                // Store it if the drop is smaller than the current value (or if there is no value)
                                if ( profile[idx] > val || profile[idx] == 0 ) profile[idx] = val;
                        }
                        else if (mode == SWARM_AVERAGE_RATIO) {
                                profile[idx] += image->get_value_at(xx,yy);
                                tally[idx]++;
                        }

                }
        }

        if (mode == SWARM_AVERAGE_RATIO) {
                for(unsigned int i = 0; i < profile.size(); ++i) {
                        if (tally[i] != 0) {
                                profile[i] /= static_cast<float>(tally[i]);
                                profile[i] = (peakval - profile[i] ) / peakval;
                        }
                }
        }

        return profile;
}
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().

{

        float peakval = image->get_value_at(x,y);

        int radius_squared = radius*radius;

        static vector<float> squared_numbers;
        if ( (unsigned int)(radius+1) > squared_numbers.size() ) {
                for(int i = squared_numbers.size(); i <= radius; ++i) {
                        squared_numbers.push_back((float)(i*i));
                }
        }

        vector<unsigned int> tally;
        if (mode == SWARM_AVERAGE_RATIO) tally.resize(profile.size(),0);

        for(int k = -radius; k <= radius; ++k) {
                for(int j = -radius; j <= radius; ++j) {
                        // Translate coordinates
                        int xx = x+j;
                        int yy = y+k;

                        // Protect against accessing pixels out of bounds
                        if ( xx >= image->get_xsize() || xx < 0 ) continue;
                        if ( yy >= image->get_ysize() || yy < 0 ) continue;

                        // We don't need to pay attention to the origin
                        if ( xx == x && yy == y) continue;

                        // Protect against vector accesses beyond the boundary
                        int square_length = k*k + j*j;
                        if (square_length > radius_squared ) continue;

                        // It's not a local maximum!
                        if ( image->get_value_at(xx,yy) > peakval)  return false;

                        // The idx is the radius, rounded down. This creates a certain type of pattern that
                        // can only really be explained visually...
                        int idx = -1;
                        // This little loop avoids the use of sqrtf
                        for(int i = 1; i < radius; ++i) {
                                if ( square_length >= squared_numbers[i] && square_length <= squared_numbers[i+1] ) {
                                        idx = i;
                                }
                        }
//                      int idx = (int) sqrtf(k*k + j*j);
                        // decrement the idx, because the origin information is redundant
                        idx -= 1;

                        if (mode == SWARM_DIFFERENCE) {
                                // Finally, get the drop relative to the origin
                                float val = peakval - image->get_value_at(xx,yy);

                                // Store it if the drop is smaller than the current value (or if there is no value)
                                if ( profile[idx] > val || profile[idx] == 0 ) profile[idx] = val;
                        }
                        else if (mode == SWARM_RATIO) {
                                // Finally, get the drop relative to the origin
                                float val =  (peakval - image->get_value_at(xx,yy) ) / peakval;

                                // Store it if the drop is smaller than the current value (or if there is no value)
                                if ( profile[idx] > val || profile[idx] == 0 ) profile[idx] = val;
                        }
                        else if (mode == SWARM_AVERAGE_RATIO) {
                                profile[idx] += image->get_value_at(xx,yy);
                                tally[idx]++;
                        }

                }
        }

        if (mode == SWARM_AVERAGE_RATIO) {
                for(unsigned int i = 0; i < profile.size(); ++i) {
                        if (tally[i] != 0) {
                                profile[i] /= static_cast<float>(tally[i]);
                                profile[i] = (peakval - profile[i] ) / peakval;
                        }
                }
        }

        set_radial_non_zero(exclusion_map,x,y,radius);

        return true;
}
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:
imagethe image containing the interesting pixels values (typically a correlation image)
xthe x coordinate of the candidate pixel maximum
ythe y coordinate of the candidate pixel maximum
radiusthe constraining radius of the local neighborhood
exclusion_mapan 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().

{
        float peakval = image->get_value_at(x,y);
        int radius_squared = radius*radius;
        for(int k = -radius; k <= radius; ++k) {
                for(int j = -radius; j <= radius; ++j) {
                        // Translate coordinates
                        int xx = x+j;
                        int yy = y+k;

//                      // Protect against accessing pixel out of bounds
                        if ( xx >= image->get_xsize() || xx < 0 ) continue;
                        if ( yy >= image->get_ysize() || yy < 0 ) continue;

                        // We don't need to pay attention to the origin
                        if ( xx == x && yy == y) continue;

                        if ((k*k+j*j)>radius_squared) continue;

                        if ( image->get_value_at(xx,yy) > peakval)  return false;
                }
        }

        set_radial_non_zero(exclusion_map,x,y,radius);

        return true;

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

Definition at line 112 of file boxingtools.h.

References mode.

{ 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().

{
        int radius_squared = radius*radius;
        for(int k = -radius; k <= radius; ++k) {
                for(int j = -radius; j <= radius; ++j) {
                        // Translate coordinates
                        int xx = x+j;
                        int yy = y+k;

                        if ((k*k+j*j)>radius_squared) continue;
                        // Protect against accessing pixel out of bounds
                        if ( xx >= exclusion->get_xsize() || xx < 0 ) continue;
                        if ( yy >= exclusion->get_ysize() || yy < 0 ) continue;

                        exclusion->set_value_at(xx,yy,1);
                }
        }
}
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().

                                                                                                                       {

        // Works only in 2D
        int inx = image->get_xsize();
        int iny = image->get_ysize();
        int mnx = mask->get_xsize();
        int mny = mask->get_ysize();

        int startx = x-mnx/2;
        int endx =startx + mnx;
        int xoffset = 0;
        if (startx < 0) {
                xoffset = abs(startx);
                startx = 0;
        }
        if (endx > inx) endx = inx;

        int starty = y-mny/2;
        int endy =starty + mny;
        int yoffset = 0;
        if (starty < 0) {
                yoffset = abs(starty);
                starty = 0;
        }
        if (endy > iny) endy = iny;


        for (int j = starty; j < endy; ++j ) {
                for (int i = startx; i < endx; ++i) {
                        if (mask->get_value_at(xoffset+i-startx,yoffset+j-starty) != 0 ) {
                                image->set_value_at(i,j,val);
                        }
                }
        }
}

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: