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);	

}
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
    }
}
Esempio n. 3
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;
    }
   
  }