Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

RawImage.cc

Go to the documentation of this file.
00001 #include "RawImage.h"
00002 
00003 #include "DualCoding/DualCoding.h"
00004 #include "Shared/RobotInfo.h"
00005 
00006 using namespace DualCoding;
00007 
00008 RawImage::RawImage(unsigned int pWidth, unsigned int pHeight) : 
00009   width(pWidth), height(pHeight),
00010   imageData(pWidth * pHeight) {}
00011 
00012 unsigned int RawImage::clampX(int x) {
00013   if (x < 0)
00014     return 0;
00015   else if ((unsigned int)x >= width)
00016     return width-1;
00017   else
00018     return x;
00019 }
00020 
00021 unsigned int RawImage::clampY(int y) {
00022   if (y < 0)
00023     return 0;
00024   else if ((unsigned int)y >= height)
00025     return height-1;
00026   else
00027     return y;
00028 }
00029 
00030 /**
00031 
00032    Very Important Note:
00033    
00034    This works in x,y coordinates instead of in row,width coordinates.
00035 
00036  */
00037 float& RawImage::operator() (int x, int y) {
00038   //y - row
00039   //x - column
00040   return imageData[clampY(y) * width + clampX(x)];
00041 }
00042 
00043 float& RawImage::operator() (unsigned int x, unsigned int y) {
00044   return imageData[clampY(y) * width + clampX(x)];
00045 }
00046 
00047 
00048 /**
00049    This is an implementation of bilinear interpolation in
00050    order to handle non-integer pixel indices.
00051  */
00052 float RawImage::operator() (float x, float y) {
00053   int x_int, y_int;
00054   float x_remain, y_remain;
00055 
00056   x_int = (int)floor(x);
00057   y_int = (int)floor(y);
00058   x_remain = x - x_int;
00059   y_remain = y - y_int;
00060 
00061   return (1-x_remain)*(1-y_remain)*(*this)(x_int, y_int) +
00062     x_remain*(1-y_remain)*(*this)(x_int +1,y_int) + 
00063     (1-x_remain)*y_remain*(*this)(x_int,y_int + 1) +
00064     x_remain*y_remain*(*this)(x_int+1,y_int+1);
00065 }
00066 
00067 void RawImage::loadFromRawY() {
00068   int const incr = ProjectInterface::defRawCameraGenerator->getIncrement(ProjectInterface::halfLayer);
00069   int const skip = ProjectInterface::defRawCameraGenerator->getSkip(ProjectInterface::halfLayer);
00070   uchar* chan_ptr = ProjectInterface::defRawCameraGenerator->getImage(ProjectInterface::halfLayer, 
00071                       RawCameraGenerator::CHAN_Y);
00072 
00073   if (chan_ptr != NULL) {
00074     chan_ptr -= incr;
00075 
00076     for (unsigned int row = 0; row < height; row++) {
00077       for (unsigned int col = 0; col < width; col++) {
00078   imageData[row * width + col] = (float)*(chan_ptr += incr);
00079       }
00080       chan_ptr += skip;
00081     }
00082   }
00083 }
00084 
00085 void RawImage::buildNextLayer(RawImage &img) {
00086   //note that img must have dimensions width+1/2,height+1/2
00087   unsigned int nextWidth = (width+1)/2;
00088   unsigned int nextHeight = (height+1)/2;
00089 
00090   for (unsigned int x = 0; x < nextWidth; x++) {
00091     for (unsigned int y = 0; y < nextHeight; y++) {
00092       img(x,y) = .25 * (*this)(2*x,2*y) +
00093   .125 * ((*this)(2*x - 1, 2*y) + (*this)(2*x+1,2*y) +
00094     (*this)(2*x,2*y-1) + (*this)(2*x,2*y+1)) +
00095   .0625 * ((*this)(2*x-1,2*y-1) + (*this)(2*x+1,2*y+1) +
00096      (*this)(2*x-1,2*y+1) + (*this)(2*x+1,2*y-1));
00097     }
00098   }
00099 }
00100 
00101 void RawImage::printImage() {
00102   for (unsigned int x = 0; x < width; x++) {
00103     for (unsigned int y = 0; y < height; y++) {
00104       cout << (*this)(x,y) << endl;
00105     }
00106   }
00107 }
00108 
00109 fmat::Column<2> RawImage::gradient(float x, float y) {
00110   float gradX = ( (*this)(x+1,y) - (*this)(x-1,y) ) / 2.0;
00111   float gradY = ( (*this)(x,y+1) - (*this)(x,y-1) ) / 2.0;
00112 
00113   return fmat::pack(gradX, gradY);
00114 }
00115 
00116 fmat::Matrix<2,2> RawImage::gradientCov(float x, float y) {
00117   float gradX = ( (*this)(x+1,y) - (*this)(x-1,y) ) / 2.0;
00118   float gradY = ( (*this)(x,y+1) - (*this)(x,y-1) ) / 2.0;
00119 
00120   fmat::Matrix<2,2> cov;
00121   cov(0,0) = gradX*gradX;
00122   cov(1,1) = gradY*gradY;
00123   cov(0,1) = gradX*gradY;
00124   cov(1,0) =  cov(0,1);
00125   
00126   return cov;
00127 }
00128 
00129 fmat::Matrix<2,2> RawImage::spatialGradientMatrix(float center_x,
00130               float center_y,
00131               int window) {
00132   fmat::Matrix<2,2> M;
00133   for (float x = center_x - window; x <= center_x + window; x++) {
00134     for (float y = center_y - window; y <= center_y + window; y++) {
00135       M += gradientCov(x,y);
00136     }
00137   }
00138 
00139   return M;
00140 }
00141 
00142 float RawImage::imageScore(float x, float y, int window) {
00143   fmat::Matrix<2,2> M = spatialGradientMatrix(x,y,window);
00144   float d = fmat::det(M);
00145   float b = M(0,0) + M(1,1);
00146 
00147   if (d == 0.0f || (b*b - 4 * d) < 0)
00148     return 0;
00149   else
00150     return min(fabs((b + sqrt(b*b - 4 * d))/2), fabs((b - sqrt(b*b - 4 * d))/2));
00151 }

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:49 2016 by Doxygen 1.6.3