Exemple #1
0
void SelfSimDescriptor::computeLogPolarMapping(Mat& mappingMask) const
{
    mappingMask.create(largeSize, largeSize, CV_8S);

    // What we want is
    //		 log_m (radius) = numberOfDistanceBuckets
    //	<==> log_10 (radius) / log_10 (m) = numberOfDistanceBuckets
    //	<==> log_10 (radius) / numberOfDistanceBuckets = log_10 (m)
    //	<==> m = 10 ^ log_10(m) = 10 ^ [log_10 (radius) / numberOfDistanceBuckets]
    //
    int radius = largeSize/2, angleBucketSize = 360 / numberOfAngles;
    int fsize = (int)getDescriptorSize();
    double inv_log10m = (double)numberOfDistanceBuckets/log10((double)radius);

    for (int y=-radius ; y<=radius ; y++)
    {
        schar* mrow = mappingMask.ptr<schar>(y+radius);
        for (int x=-radius ; x<=radius ; x++)
        {
            int index = fsize;
            float dist = (float)std::sqrt((float)x*x + (float)y*y);
            int distNo = dist > 0 ? cvRound(log10(dist)*inv_log10m) : 0;
            if( startDistanceBucket <= distNo && distNo < numberOfDistanceBuckets )
            {
                float angle = std::atan2( (float)y, (float)x ) / (float)CV_PI * 180.0f;
                if (angle < 0) angle += 360.0f;
                int angleInt = (cvRound(angle) + angleBucketSize/2) % 360;
                int angleIndex = angleInt / angleBucketSize;
                index = (distNo-startDistanceBucket)*numberOfAngles + angleIndex;
            }
            mrow[x + radius] = saturate_cast<schar>(index);
        }
    }
}
  void BlockCellRemapper::map(std::vector< float >& block_feats)
  {
    assert(block_feats.size() == computer.getDescriptorSize());
    vector<float> cell_feats(getDescriptorSize(),0);
    
    // getIndex(int blockX, int blockY, int cellN, int bin);
    assert(computer.cellsPerBlock() == 4);
    for(int blockX = 0; blockX < computer.blocks_x(); blockX++)
      for(int blockY = 0; blockY < computer.blocks_y(); blockY++)
	for(int cellN = 0; cellN < 4; cellN++)
	  for(int bin = 0; bin < computer.getNBins(); bin++)
	  {	    
	    // get from the celly form (cells)
	    // notes
	    // dst block depends on cell # and src block
	    int cell_cell_X, cell_cell_Y;
	    map_blockToCell4(blockX, blockY, cellN, cell_cell_X, cell_cell_Y);
	    // figure out where to put it in the dest
	    int icell = getIndex(cell_cell_X,cell_cell_Y,0,bin+cellN*computer.getNBins());
	    assert(icell >= 0 && icell < cell_feats.size());
	    float&cell_val = cell_feats[icell];
	    
	    // get each piece of data from in the blocky
	    int iblk = computer.getIndex(blockX,blockY,cellN,bin);
	    assert(iblk >= 0 && iblk < block_feats.size());	
	    float&blk_val = block_feats[iblk];    
	    
	    // do the copy
	    cell_val = blk_val;	    
	  }
    
    block_feats = cell_feats;
  }
  // Cell Format to block format
  void BlockCellRemapper::unmap(std::vector< double >& cell_feats)
  {
    if(cell_feats.size() != getDescriptorSize())
    {
      cout << printfpp("%d != %d",cell_feats.size(),getDescriptorSize()) << endl;
      throw std::logic_error("f**k!!!");
    }
    vector<double> block_feats(computer.getDescriptorSize(),0);
    
    // getIndex(int blockX, int blockY, int cellN, int bin);
    assert(computer.cellsPerBlock() == 4);
    for(int blockX = 0; blockX < computer.blocks_x(); blockX++)
      for(int blockY = 0; blockY < computer.blocks_y(); blockY++)
	for(int cellN = 0; cellN < 4; cellN++)
	  for(int bin = 0; bin < computer.getNBins(); bin++)
	  {	    
	    // get from the celly form (cells)
	    // notes
	    // dst block depends on cell # and src block
	    int cell_cell_X, cell_cell_Y;
	    map_blockToCell4(blockX, blockY, cellN, cell_cell_X, cell_cell_Y);
	    // figure out where to put it in the dest
	    int icell = getIndex(cell_cell_X,cell_cell_Y,0,bin+cellN*computer.getNBins());
	    if(!(icell >= 0 && icell < cell_feats.size()))
	    {
	      printf("cellX = %d cellY = %d\n",cell_cell_X,cell_cell_Y);
	      printf("icell = %d of %d\n",icell,(int)cell_feats.size());
	    }
	    assert(icell >= 0 && icell < cell_feats.size());
	    double&cell_val = cell_feats[icell];
	    
	    // get each piece of data from in the blocky
	    int iblk = computer.getIndex(blockX,blockY,cellN,bin);
	    assert(iblk >= 0 && iblk < block_feats.size());	
	    double&blk_val = block_feats[iblk];    
	    
	    // do the copy
	    blk_val = cell_val;	    
	  }
    
    cell_feats = block_feats;
  }
cl_int IntelAccelerator::getInfo(cl_accelerator_info_intel paramName,
                                 size_t paramValueSize,
                                 void *paramValue,
                                 size_t *paramValueSizeRet) const {
    cl_int result = CL_SUCCESS;
    size_t ret = 0;

    switch (paramName) {
    case CL_ACCELERATOR_DESCRIPTOR_INTEL: {
        ret = getDescriptorSize();
        result = ::getInfo(paramValue, paramValueSize, getDescriptor(), ret);
    }

    break;

    case CL_ACCELERATOR_REFERENCE_COUNT_INTEL: {
        auto v = getReference();

        ret = sizeof(cl_uint);
        result = ::getInfo(paramValue, paramValueSize, &v, ret);
    }

    break;

    case CL_ACCELERATOR_CONTEXT_INTEL: {
        ret = sizeof(cl_context);
        cl_context ctx = static_cast<cl_context>(pContext);
        result = ::getInfo(paramValue, paramValueSize, &ctx, ret);
    }

    break;

    case CL_ACCELERATOR_TYPE_INTEL: {
        auto v = getTypeId();
        ret = sizeof(cl_accelerator_type_intel);
        result = ::getInfo(paramValue, paramValueSize, &v, ret);
    }

    break;

    default:
        result = CL_INVALID_VALUE;
        break;
    }

    if (paramValueSizeRet) {
        *paramValueSizeRet = ret;
    }

    return result;
}
  void FeatureBinCombiner::compute(const ImRGBZ& im, std::vector< float >& feats)
  {
    // check the depth image for NaNs
    //assert(!any<float>(im.Z,[](float v){return !goodNumber(v);}));
    bool rows_match = im.rows() == blocks_y()*getCellSize().height;
    bool cols_match = im.cols() == blocks_x()*getCellSize().width;
    if(!(rows_match && cols_match))
    {
      cout << printfpp("imSize = %d %d",im.cols(),im.rows()) << endl;
      cout << printfpp("blocks_x[%d]*cell_size.width[%d] = %d",
		       blocks_x(),getCellSize().width,blocks_x()*getCellSize().width) << endl;
      cout << printfpp("blocks_y[%d]*cell_size.height[%d] = %d",
		       blocks_y(),getCellSize().height,blocks_y()*getCellSize().height) << endl;
      assert(rows_match && cols_match);
    }
    
    // precompute the features with each computer.
    vector<vector<float> > in_feats;
    int iter = 0;
    for(shared_ptr<DepthFeatComputer>&computer : computers)
    {
      in_feats.push_back(vector<float>());
      computer->compute(im,in_feats.back());
      // check for errors
      for(float&value : in_feats.back())
      {
	bool ok = goodNumber(value);
	if(!ok)
	  cout << "problem @ computer = " << iter << endl;
	assert(ok);
      }
      iter++;
    }
    
    // combine into output
    feats = vector<float>(getDescriptorSize(),0);
    for(int blockX = 0; blockX < blocks_x(); blockX++)
      for(int blockY = 0; blockY < blocks_y(); blockY++)
      {
	int outbin = 0;
	for(int compIter = 0; compIter < computers.size(); compIter++)
	{
	  shared_ptr<DepthFeatComputer> computer = computers[compIter];
	  assert(computer->cellsPerBlock() == 1);
	  for(int inbin = 0; inbin < computer->getNBins(); inbin++,outbin++)
	    feats[getIndex(blockX,blockY,0,outbin)] = 
	    in_feats[compIter][computer->getIndex(blockX,blockY,0,inbin)]*weights[compIter];
	}     
      }
  }
  /// pack into a form [ori1 ori2 ori .... oriN norm1 ... norm4]
  void HOGComputer18p4_General::compute(const ImRGBZ& im, std::vector< float >& feats)
  {
    vector<float> unreduced; hog18x4mapped.compute(im,unreduced);
    int raw_bins = hog18x4mapped.getNBins();
    int nbins = params::ORI_BINS;
    assert(raw_bins == nbins*4);
    
    /// now, iterate over all the cells in the remapped
    // 18x4 feature, reduce the block, and write the reduced
    // form to the 18p4 feature.
    feats = vector<float>(getDescriptorSize(),0);
    for(int yIter = 0; yIter < blocks_y(); yIter++)
      for(int xIter = 0; xIter < blocks_x(); xIter++)
      {
	// (1) compute the orientation sum
	for(int out_binIter = 0; out_binIter < nbins; out_binIter++)
	{
	  float ori_sum = 0;
	  for(int in_block = 0; in_block < 4; in_block++)
	  {
	    int bin_id = out_binIter + nbins*in_block;
	    ori_sum += unreduced[hog18x4mapped.getIndex(xIter,yIter,0,bin_id)];
	  }
	  feats[getIndex(xIter,yIter,0,out_binIter)] = .5*ori_sum;
	}
	
	// (2) compute the normalization sum
	for(int out_blockIter = 0; out_blockIter < 4; out_blockIter++)
	{
	  float block_sum = 0;
	  for(int in_oriBin = 0; in_oriBin < nbins; in_oriBin++)
	  {
	    int bin_id = in_oriBin + out_blockIter*nbins;
	    block_sum += unreduced[hog18x4mapped.getIndex(xIter,yIter,0,bin_id)];
	  }
	  feats[getIndex(xIter,yIter,0,nbins + out_blockIter)] = .2357*block_sum;
	}
      }
  }
  void PCAFeature::compute(const ImRGBZ& im, std::vector< float >& reduced_feats)
  {
    // allocate space
    vector<float> unreduced_feats; unreduced_computer.compute(im,unreduced_feats);
    // wrap with matrices
    int ncells = blocks_x()*blocks_y();
    Mat mat_unreduced_feats(
      ncells,unreduced_computer.getNBins(),DataType<float>::type,&unreduced_feats[0]);
    
    // compute the PCA
    Mat mat_reduced_feats = g_pca_reducer->project(mat_unreduced_feats);
    
    // must I now divide by the eigenvalues?
    assert(g_pca_reducer->eigenvalues.type() == DataType<float>::type);
    for(int rIter = 0; rIter < mat_reduced_feats.rows; rIter++)
      for(int cIter = 0; cIter < mat_reduced_feats.cols; cIter++)
	mat_reduced_feats.at<float>(rIter,cIter) /= g_pca_reducer->eigenvalues.at<float>(cIter);
      
    // convert to a flat feature vector
    reduced_feats = mat_reduced_feats.reshape(0,mat_reduced_feats.size().area());
    assert(reduced_feats.size() == getDescriptorSize());
  }
Exemple #8
0
void SelfSimDescriptor::compute(const Mat& img, vector<float>& descriptors, Size winStride,
                                const vector<Point>& locations) const
{
    CV_Assert( img.depth() == CV_8U );

    winStride.width = std::max(winStride.width, 1);
    winStride.height = std::max(winStride.height, 1);
    Size gridSize = getGridSize(img.size(), winStride);
    int i, nwindows = locations.empty() ? gridSize.width*gridSize.height : (int)locations.size();
    int border = largeSize/2 + smallSize/2;
    int fsize = (int)getDescriptorSize();
    vector<float> tempFeature(fsize+1);
    descriptors.resize(fsize*nwindows + 1);
    Mat ssd(largeSize, largeSize, CV_32F), mappingMask;
    computeLogPolarMapping(mappingMask);

#if 0 //def _OPENMP
    int nthreads = cvGetNumThreads();
    #pragma omp parallel for num_threads(nthreads)
#endif
    for( i = 0; i < nwindows; i++ )
    {
        Point pt;
        float* feature0 = &descriptors[fsize*i];
        float* feature = &tempFeature[0];
        int x, y, j;

        if( !locations.empty() )
        {
            pt = locations[i];
            if( pt.x < border || pt.x >= img.cols - border ||
                pt.y < border || pt.y >= img.rows - border )
            {
                for( j = 0; j < fsize; j++ )
                    feature0[j] = 0.f;
                continue;
            }
        }
        else
            pt = Point((i % gridSize.width)*winStride.width + border,
                       (i / gridSize.width)*winStride.height + border);

        SSD(img, pt, ssd);

        // Determine in the local neighborhood the largest difference and use for normalization
        float var_noise = 1000.f;
        for( y = -1; y <= 1 ; y++ )
            for( x = -1 ; x <= 1 ; x++ )
                var_noise = std::max(var_noise, ssd.at<float>(largeSize/2+y, largeSize/2+x));

        for( j = 0; j <= fsize; j++ )
            feature[j] = FLT_MAX;

        // Derive feature vector before exp(-x) computation
        // Idea: for all  x,a >= 0, a=const.   we have:
        //       max [ exp( -x / a) ] = exp ( -min(x) / a )
        // Thus, determine min(ssd) and store in feature[...]
        for( y = 0; y < ssd.rows; y++ )
        {
            const schar *mappingMaskPtr = mappingMask.ptr<schar>(y);
            const float *ssdPtr = ssd.ptr<float>(y);
            for( x = 0 ; x < ssd.cols; x++ )
            {
                int index = mappingMaskPtr[x];
                feature[index] = std::min(feature[index], ssdPtr[x]);
            }
        }

        var_noise = -1.f/var_noise;
        for( j = 0; j < fsize; j++ )
            feature0[j] = feature[j]*var_noise;
        Mat _f(1, fsize, CV_32F, feature0);
        cv::exp(_f, _f);
    }
}