int processSet(Params *par, vector<char *> filenames, int findex, vector<BB> &bbs, clock_t start_t){
    // cout << "TEST\n";

    vector<DARY *> images;
    // cout << " OK 0 " << findex <<  " "<< filenames[findex]<< endl;
    //int t0 = printRuntime(start_t, clock(), "testpoint0");
    
    loadImages(filenames, findex, images, par);
    // cout << " OK 1 " << endl;
    //int t1 = printRuntime(t0, clock(), "testpoint1");
    
    if(findex>0){ computeDifferences(images); }
    // cout << " OK 2 " << endl;
    //int t2 = printRuntime(t1, clock(), "testpoint2");

    normalizeDifference(images);
    //int t3 = printRuntime(t2, clock(), "testpoint3");

    DARY *output = new DARY(images[0]->y(),images[0]->x(),UCHAR1FLOAT1);
    output->set(0.0);
    // cout << " OK 3 " << endl;
    detectDifferences(images, output, par->getValue("diff_thres.int"), par->getValue("median_size.int"));
    //int t4 = printRuntime(t3, clock(), "testpoint4");

    // Detect using HOG (histogram of oriented gradients)
    if((int)par->getValue("match_images.int")){ matchImages(images,output); }
    for(uint i=0; i<images.size(); i++){ delete images[i];images.clear(); }
    // cout << " OK 4 " << endl;

    // Bounding box method
    int labels;
    labelSegments(output, labels);
    //int t5 = printRuntime(t4, clock(), "testpoint5");
    
    int sel=-1;
    if(labels>1){
        findBBs(output,bbs,labels);
        sel=selectBBs(bbs, par);
        // if(bbs.size()>0){ sel=0; }
    }
    // cout << " OK 4 " << endl;
    //int t6 = printRuntime(t5, clock(), "testpoint6");
    
    if(findex==-1){ findex=1; }
    // coutBB used in drawBB prints to terminal
    if((int)par->getValue("draw_images.int")){ drawBB(filenames[findex],output,bbs,sel); }
    
    //int t7 = printRuntime(t6, clock(), "testpoint7");
    //int t8 = printRuntime(start_t, clock(), "out of total");

    delete output;
    return sel;
}
void computeMoments(DARY *img, FeatureDescriptor *ds){
	DARY * dx = new DARY(PATCH_SIZE,PATCH_SIZE);   
	DARY * dy = new DARY(PATCH_SIZE,PATCH_SIZE);   
	dX2(img,dx);
	dY2(img,dy);
	int rad=dx->x()>>1;
	ds->allocVec(20);//EIndexSize*EIndexSize*OriSize;
	float *vec = ds->getVec();
	float norm=computMoment000(dx,rad);
	//vec[0]=computMoment001(dx,rad)/norm;
	//float mean=computMoment002(grad,rad);

	vec[0]=computMoment101(dx,rad)/norm;
	vec[1]=computMoment011(dx,rad)/norm;
 	vec[2]=computMoment102(dx,rad)/norm;
	vec[3]=computMoment012(dx,rad)/norm;
      
 	vec[4]=computMoment111(dx,rad)/norm;
	vec[5]=computMoment201(dx,rad)/norm;
	vec[6]=computMoment021(dx,rad)/norm;
	vec[7]=computMoment112(dx,rad)/norm;
	vec[8]=computMoment202(dx,rad)/norm;
	vec[9]=computMoment022(dx,rad)/norm;

	//norm=computMoment000(dy,rad);
	vec[10]=computMoment101(dy,rad)/norm;
	vec[11]=computMoment011(dy,rad)/norm;
 	vec[12]=computMoment102(dy,rad)/norm;
	vec[13]=computMoment012(dy,rad)/norm;
      
 	vec[14]=computMoment111(dy,rad)/norm;
	vec[15]=computMoment201(dy,rad)/norm;
	vec[16]=computMoment021(dy,rad)/norm;
	vec[17]=computMoment112(dy,rad)/norm;
	vec[18]=computMoment202(dy,rad)/norm;
	vec[19]=computMoment022(dy,rad)/norm;
	//vec[0]=0;
	delete dx;delete dy;
	ds->changeBase(mom_base);
	//int mom_pca_size=20;
	//ds->pca(mom_pca_size, mom_pca_avg, mom_pca_base);	

}
  /**
     compute desriptors on the uniform grid 

     rect - define position, orientation and size of grid in the image

     desc_step - distance between descriptors
     desc_size - descriptor size
  */
  void computeDescriptorGrid(kma::ImageContent *input_image, FeatureGrid &grid, QString qsDESC)
  {
    initPatchMask(PATCH_SIZE);

    /* descriptor patch */
    DARY *patch = new DARY(PATCH_SIZE, PATCH_SIZE);

    /* descritor */
    int _ShapeSize = kma::shape::SrSize * kma::shape::ScSize * kma::shape::SOriSize;
    vector<float> ds(_ShapeSize);

    double patch_scale = (2*grid.desc_size + 1) / (float)PATCH_SIZE;

    DARY *source_image = input_image;
    DARY *smooth_input_image = NULL;

    /* smooth once for all descriptors if patch < descriptor */
    if (patch_scale > 1.0) {
      smooth_input_image = new DARY(input_image->y(), input_image->x());
      smooth(input_image, smooth_input_image, patch_scale);
      source_image = smooth_input_image;
    }

    boost_math::double_matrix patch_lcpos;
    boost_math::double_matrix patch_lrpos;

    assert(qsDESC == "SHAPEv5" && "unknown descriptor type");

    kma::precompute_patch_idx(patch_lcpos, patch_lrpos, (int)floor(PATCH_SIZE/2.0), kma::shape::SrSize, kma::shape::ScSize);

    assert((int)grid.desc.size() == grid.ny);

    for (int iy = 0; iy < grid.ny; ++iy) { 
      //assert((int)grid.desc[iy].size() == grid.nx);

      if (!((int)grid.desc[iy].size() == grid.nx)) {
        cout << "iy: " << iy << endl;
        cout << "required size: " << grid.nx << endl;
        cout << "current size: " << grid.desc[iy].size() << endl;
        assert(false);
      }

      for (int ix = 0; ix < grid.nx; ++ix) {
        ds.assign(ds.size(), 0.0);
        grid.desc[iy][ix].clear();

        /* grid coordinates */
        double x2 = grid.rect.min_proj_x + ix*grid.desc_step;
        double y2 = grid.rect.min_proj_y + iy*grid.desc_step;

        /* image coordinates */
        boost_math::double_vector imgpos = grid.origin() + x2*grid.x_axis() + y2*grid.y_axis();

        /* test that descriptor is inside the image */
        if (imgpos(0) >= grid.desc_size + 1 && imgpos(0) <= source_image->x() - (grid.desc_size + 1) && 
            imgpos(1) >= grid.desc_size + 1 && imgpos(1) <= source_image->y() - (grid.desc_size + 1)) {

          /* map image region to patch */
          patch->interpolate(source_image, imgpos(0), imgpos(1), 
                             patch_scale*grid.x_axis()(0), patch_scale*grid.y_axis()(0), 
                             patch_scale*grid.x_axis()(1), patch_scale*grid.y_axis()(1));

          /* mean/var normalization, rescale with x = 128 + 50*x, clip to [0, 255] (last 3 parameters are not important) */
          normalize(patch, 0, 0, 0);
          //patch->writePNG(qsFilename.toStdString().c_str());

          assert(patch_lcpos.size1() > 0 && patch_lcpos.size2() > 0);

          /* compute SHAPEv5 */
          kma::KMAcomputeShape(patch_lcpos, patch_lrpos, patch, ds);

          grid.desc[iy][ix].insert(grid.desc[iy][ix].end(), ds.begin(), ds.end());
        }
      }
    }// grid positions

    delete smooth_input_image;
    delete patch;    
  }
void loadImages(vector<char *> filenames, int start, vector<DARY *> &images, Params *par){
    // cout << filenames[start]<< endl;
    DARY *sim,*im;
    int width = par->getValue("image_width.int");  
    int height = par->getValue("image_height.int");
    int mtop = par->getValue("header_size.int");
    
    clock_t u = clock();

    if(start==-1){ // returns two images
        DARY *imin = new DARY(filenames[2]);
        int mhight = imin->y() - par->getValue("footnote_size.int") - mtop;

        im = new DARY(mhight, imin->x(), UCHAR3);    
        im->crop(imin, 0, mtop);                                                     
        
        // float scalex = width/(float)im->x(); 
        // height = (int)(im->y()/scalex);
        // sim = new DARY(height,width,UCHAR3);
        // sim->scale(im, scalex, scalex);
        // delete im;

        images.push_back(im);

        imin = new DARY(filenames[4]);
        im = new DARY(mhight, imin->x(), UCHAR3);    
        im->crop(imin, 0, mtop);

        // scalex=width/(float)im->x(); 
        // height = (int)(im->y()/scalex);
        // sim=new DARY(height,width,UCHAR3);
        // sim->scale(im, scalex, scalex);
        // delete im;

        images.push_back(im);
        delete imin;
    } else {
        DARY *imin =new DARY(filenames[start]);
        int mhight = imin->y() - par->getValue("footnote_size.int") - mtop;
        //cout << mhight << endl;
        //int u0 = printRuntime(u, clock(), "loadImages TP0");
        
        //cout << "x: " << imin->x() << endl;
        //cout << "y: " << imin->y() << endl;
        
        im = new DARY(mhight,imin->x(),UCHAR3);
        im->crop(imin,0,mtop);
        
        //int u1 = printRuntime(u0, clock(), "loadImages TP1");

        float scalex=(float)im->x()/width;    
        height = (int)(im->y()/scalex);
        sim=new DARY(height,width,UCHAR3);
        sim->decrease(im);
        //sim->writePNG("test1.png");
        delete imin;
        
        //int u2 = printRuntime(u1, clock(), "loadImages TP2");

        // images.push_back(sim);        
        images.push_back(new DARY(sim));        //images[0]
        images.push_back(new DARY(sim));        //images[1]
        images.push_back(sim);  //              //images[2]
        
        //int u3 = printRuntime(u2, clock(), "loadImages TP3");

        imin=new DARY(filenames[start+1]);
        //int u4a = printRuntime(u3, clock(), "loadImages TP4a");
        im->crop(imin,0,mtop);
        //int u4b = printRuntime(u4a, clock(), "loadImages TP4b");
        sim=new DARY(height,width,UCHAR3);
        //int u4c = printRuntime(u4b, clock(), "loadImages TP4c");
        sim->decrease(im);
        //sim->writePNG("test2.png");
        //delete imin;			// commented out
        images.push_back(sim);                  //images[3]
        
        //int u4 = printRuntime(u3, clock(), "loadImages TP4");
        
        imin=new DARY(filenames[start+2]);
        im->crop(imin,0,mtop);
        sim=new DARY(height,width,UCHAR3);
        sim->decrease(im);
        
        //int u5 = printRuntime(u4, clock(), "loadImages TP5");

        //sim->writePNG("test3.png");getchar();
        delete imin;
        delete im;
        images.push_back(sim);                  //images[4]     // returns five images
    }
}
示例#5
0
  void KMAcomputeShape(const boost_math::double_matrix &patch_lcpos, 
                       const boost_math::double_matrix &patch_lrpos, 
                       DARY *img, vector<float> &vec)
  {
    const int _ShapeSize = kma::shape::SrSize * kma::shape::ScSize * kma::shape::SOriSize;
    assert((int)vec.size() == _ShapeSize);

    uint patch_width = PATCH_SIZE;
    uint patch_height = PATCH_SIZE;

    assert(img->x() == patch_width && img->y() == patch_height);
    assert(patch_lcpos.size1() == patch_height && patch_lcpos.size2() == patch_width); 
    assert(patch_lrpos.size1() == patch_height && patch_lrpos.size2() == patch_width);

    int oriSize = kma::shape::SOriSize;
    int rSize = kma::shape::SrSize;
    int cSize = kma::shape::ScSize;

    DARY *grad = new DARY(PATCH_SIZE,PATCH_SIZE);
    DARY *ori = new DARY(PATCH_SIZE,PATCH_SIZE);    
    DARY *dx = new DARY(img->y(),img->x());
    DARY *dy = new DARY(img->y(),img->x());
    DARY *edge = new DARY(img->y(),img->x());

    dX2(img,dx);
    dY2(img,dy);
    for(uint j=0;j<grad->y();j++){
      for(uint i=0;i<grad->x();i++){
        grad->fel[j][i]=sqrt(dx->fel[j][i]*dx->fel[j][i]+dy->fel[j][i]*dy->fel[j][i]); 
        ori->fel[j][i]=atan2(dy->fel[j][i],dx->fel[j][i]);
      }
    } 

    /* initialize edge image */
    memset(edge->fel[0], 0, edge->x()*edge->y()*sizeof(float));

    cannyEdges(dx, dy, grad, edge, 5, 15);
    delete dx; delete dy; delete grad;

    /* begin of KeyLogPolSample(vec, edge, ori,  angle, SOriSize, SrSize, ScSize); */
    int iradius = (int)floor(PATCH_SIZE/2);

    for (int i = -iradius; i <= iradius; i++)
      for (int j = -iradius; j <= iradius; j++) {
        //         lcpos=(M_PI+atan2(rpos,cpos))*cspacing;
        //         lrpos=log(1+sqrt((float)i*i+j*j)/iradius)*rSize;

        double lcpos = patch_lcpos(iradius + j, iradius + i);
        double lrpos = patch_lrpos(iradius + j, iradius + i);
        double rx = lrpos;// + (rSize - 1) / 2.0;
        double cx = lcpos;// + (cSize - 1) / 2.0;

        if (rx > -1.0 && rx < (float) rSize  &&
            cx > -1.0 && cx < (float) cSize) {
          //cout << "in" << cpos << " " << rpos << endl;
          KMAAddLogPolSample(vec, edge, ori, 0.0, 
                            iradius + i, iradius + j, 
                            lrpos, lcpos,  /* not used ? */ 
                            lrpos, lcpos,  /* rx, cx */
                            oriSize, rSize, cSize);

        }

        

      }
    /* end of KeyLogPolSample */

    delete edge; delete ori;
    KMANormalizeVect(vec);

    int intval, changed = FALSE;
    //for (int i = 0; i < kma::shape::ShapeSize; i++)
    for (int i = 0; i < _ShapeSize; i++)
      if (vec[i] > kma::shape::MaxIndexVal) { 
        vec[i] = kma::shape::MaxIndexVal;
        changed = TRUE;
      }

    if (changed) {

      KMANormalizeVect(vec);
    }

    /* Convert float vector to integer. 
       Assume largest value in normalized
       vector is likely to be less than 0.5. */
    //for (int i = 0; i < kma::shape::ShapeSize; i++) {
    for (int i = 0; i < _ShapeSize; i++) {
      intval = (int) (512.0 * vec[i]);
      vec[i] = (255 < intval) ? 255 : intval;
    }
   
  }