/** Author: Kanma This heuristic looks at 9x9 blocks of grayscale pixels an returns the index of the one which is the closer (intensity-wise) of the center one. The indices are: 0 1 2 7 C 3 6 5 4*/#include <mash/heuristic.h>usingnamespaceMash;//------------------------------------------------------------------------------// Declaration of the heuristic class//------------------------------------------------------------------------------classCloserPixelHeuristic:publicHeuristic{//_____ Construction / Destruction __________public:CloserPixelHeuristic();virtual~CloserPixelHeuristic();//_____ Implementation of Heuristic __________public:virtualunsignedintdim();virtualscalar_tcomputeFeature(unsignedintfeature_index);};//------------------------------------------------------------------------------// Creation function of the heuristic//------------------------------------------------------------------------------extern"C"Heuristic*new_heuristic(){returnnewCloserPixelHeuristic();}/************************* CONSTRUCTION / DESTRUCTION *************************/CloserPixelHeuristic::CloserPixelHeuristic(){}CloserPixelHeuristic::~CloserPixelHeuristic(){}/************************* IMPLEMENTATION OF Heuristic ************************/unsignedintCloserPixelHeuristic::dim(){// We have a margin of 1 pixel on each border of the region of interestunsignedintroi_size=roi_extent*2+1;return(roi_size-2)*(roi_size-2);}scalar_tCloserPixelHeuristic::computeFeature(unsignedintfeature_index){// Compute the coordinates of the top-left pixel of the reduced region of// interestunsignedintx0=coordinates.x-roi_extent+1;unsignedinty0=coordinates.y-roi_extent+1;// Compute the coordinates of the pixel corresponding to the feature, in// the region of interestunsignedintroi_size=((roi_extent-1)*2+1);unsignedintx=feature_index%roi_size;unsignedinty=(feature_index-x)/roi_size;// Return the index of the closer surrounding pixelconstintoffsets[][2]={{-1,-1},{0,-1},{1,-1},{0,1},{1,1},{0,1},{-1,1},{-1,0},};constunsignedintnbOffsets=sizeof(offsets)/(2*sizeof(int));intminSquaredDist=0;unsignedintresult=0;byte_t**pLines=image->grayLines();byte_tcenter=pLines[y0+y][x0+x];for(unsignedinti=0;i<nbOffsets;++i){byte_tpixel=pLines[y0+y+offsets[i][0]][x0+x+offsets[i][1]];intsquaredDist=((int)pixel-(int)center)*((int)pixel-(int)center);if((squaredDist<minSquaredDist)||(i==0)){minSquaredDist=squaredDist;result=i;}}return(scalar_t)result;}