Computes the linear Hough transform of the region of interest to detect lines.
Description:
Weighted Hough transform using the magnitude of the gradient (as given by a Sobel filter). Discretizes the theta parameter in 36 bins (from -90 to +85 degrees) and rho (the distance to the center, in the range +- sqrt(2) * roi_extent) in roi_size bins.
/** Author: Charles Dubout (charles.dubout@idiap.ch) * * Hough transform heuristic (weighted). Compute the linear hough transform of * the region of interest to detect lines. Discretize the theta parameter in 36 * bins (from -90 to +85 degrees) and rho (the distance to the center, in the * range +- sqrt(2) * roi_extent) in roi_size bins. */#include <mash/heuristic.h>#include <algorithm>#include <cmath>#include <numeric>#include <vector>usingnamespaceMash;//------------------------------------------------------------------------------/// The 'Hough' heuristic class//------------------------------------------------------------------------------classHoughHeuristic:publicHeuristic{//_____ Implementation of Heuristic __________public:HoughHeuristic();virtualunsignedintdim();virtualvoidprepareForCoordinates();virtualscalar_tcomputeFeature(unsignedintfeature_index);private:// The hough matrix of featuresstd::vector<scalar_t>features_;// Lookup tables of sines and cosinesscalar_tsines_[36];scalar_tcosines_[36];};//------------------------------------------------------------------------------/// Creation function of the heuristic//------------------------------------------------------------------------------extern"C"Heuristic*new_heuristic(){returnnewHoughHeuristic();}/************************* IMPLEMENTATION OF Heuristic ************************/HoughHeuristic::HoughHeuristic(){for(intt=0;t<36;++t){// Convert to radian (from -90 to +85 degrees)scalar_ttheta=(t-18)*scalar_t(3.141592653589793)/36;// Predivide by sqrt(2) so that we won't have to divide latersines_[t]=std::sin(theta)/scalar_t(1.414213562);cosines_[t]=std::cos(theta)/scalar_t(1.414213562);}}unsignedintHoughHeuristic::dim(){// We have 36 * roi_size featuresintroi_size=roi_extent*2+1;return36*roi_size;}voidHoughHeuristic::prepareForCoordinates(){// Compute the coordinates of the top-left pixel of the region of interestintx0=coordinates.x-roi_extent;inty0=coordinates.y-roi_extent;// Compute the size of the region of interestintroi_size=roi_extent*2+1;// Get the pixels values of the region of interestbyte_t**pLines=image->grayLines();// Resize the features to the correct dimensionfeatures_.clear();features_.resize(dim());for(inty=1;y<roi_size-1;++y){for(intx=1;x<roi_size-1;++x){// Sobel filterintdx=-(int)pLines[y0+y-1][x0+x-1]-2*(int)pLines[y0+y][x0+x-1]-(int)pLines[y0+y+1][x0+x-1]+(int)pLines[y0+y-1][x0+x+1]+2*(int)pLines[y0+y][x0+x+1]+(int)pLines[y0+y+1][x0+x+1];intdy=-(int)pLines[y0+y-1][x0+x-1]-2*(int)pLines[y0+y-1][x0+x]-(int)pLines[y0+y-1][x0+x+1]+(int)pLines[y0+y+1][x0+x-1]+2*(int)pLines[y0+y+1][x0+x]+(int)pLines[y0+y+1][x0+x+1];// Compute the magnitudescalar_tmagnitude=std::sqrt(scalar_t(dx*dx+dy*dy));if(magnitude>0){// For every possible thetafor(intt=0;t<36;++t){// Calculate rho (rho is in the range [0, 2 * roi_extent])scalar_trho=(x-(int)roi_extent)*cosines_[t]+(y-(int)roi_extent)*sines_[t]+roi_extent;// Increment the rho/theta pair in the feature matrix by the// magnitude of the edge// Bilinear interpolationintbefore=(int)rho;intafter=before+1;scalar_talpha=rho-before;features_[before*36+t]+=(1-alpha)*magnitude;features_[after*36+t]+=alpha*magnitude;}}}}}scalar_tHoughHeuristic::computeFeature(unsignedintfeature_index){returnfeatures_[feature_index];}