Beispiel #1
0
bool ZernikeMom::CalculateOneFrame(int frame){
  const double PI = 3.141592654;

  // cout << "ZernikeMom::CalculateOneFrame("<<frame<<")"<<endl;

   EnsureImage();
   SegmentData()->setCurrentFrame(frame);
   
   TabulateFactorials(param.maxorder);

   FindSegmentNormalisations();
   
   int nc=NumberOfCoefficients(param.maxorder);

   vector<double> zerovec(nc,0.0);

   vector<vector<double> > A_real(DataCount(),zerovec);
   vector<vector<double> > A_imag(DataCount(),zerovec);

    int width = Width(true), height = Height(true);

    int resind=0;

    for(int n=0;n<=param.maxorder;n++)
      for(int m=n&1;m<=n;m+=2){
	for (int y=0; y<height; y++)
	  for (int x=0; x<width; x++){
	    vector<int> svec = SegmentVector(frame, x, y);
	    for (size_t j=0; j<svec.size(); j++) {
	      int dataind = DataIndex(svec[j], true);
	      if(dataind != -1){
		int label=svec[j];
		double xtilde=(x-XAvg(label))*Scaling(label);
		double ytilde=(y-YAvg(label))*Scaling(label);
		double roo=sqrt(xtilde*xtilde+ytilde*ytilde);
		double theta;
		if(ytilde || xtilde)
		  theta=atan2(ytilde,xtilde);
		else
		  theta=0;

		double r=R(n,m,roo);
		r *= Scaling(label)*Scaling(label)*(n+1)/PI;
		A_real[dataind][resind] += r*cos(m*theta);
		A_imag[dataind][resind] -= r*sin(m*theta);
	      }
	    }
	  }
	resind++;
      }

    for(size_t i=0;i<A_real.size();i++){
      ((ZernikeMomData*)GetData(i))->SetData(A_real[i],A_imag[i],param);
    }
    calculated = true;

    return true;
}
Beispiel #2
0
bool ZernikeMom::CalculateOneLabel(int frame, int label)
{
  const double PI = 3.141592654;

    EnsureImage();
    SegmentData()->setCurrentFrame(frame);

    TabulateFactorials(param.maxorder);

    FindSegmentNormalisations(label);

    int nc=NumberOfCoefficients(param.maxorder);

    vector<double> A_real(nc,0.0);
    vector<double> A_imag(nc,0.0);

    int width = Width(true), height = Height(true);

    int resind=0;

    for(int n=0;n<=param.maxorder;n++)
      for(int m=n&1;m<=n;m+=2){
	for (int y=0; y<height; y++)
	  for (int x=0; x<width; x++){
	    vector<int> svec = SegmentVector(frame, x, y);
	    for (size_t j=0; j<svec.size(); j++) {
	      if(svec[j]==label){
		double xtilde=(x-XAvg(label))*Scaling(label);
		double ytilde=(y-YAvg(label))*Scaling(label);
		double roo=sqrt(xtilde*xtilde+ytilde*ytilde);
		double theta;
		if(ytilde || xtilde)
		  theta=atan2(ytilde,xtilde);
		else
		  theta=0;
		double r=R(n,m,roo);
		r *= Scaling(label)*Scaling(label)*(n+1)/PI;
		A_real[resind] += r*cos(m*theta);
		A_imag[resind] -= r*sin(m*theta);
	      }
	    }
	  }
	resind++;
      }


    int ind = DataIndex(label, true);
    if(ind==-1) return false;

    ((ZernikeMomData*)GetData(ind))->SetData(A_real,A_imag,param);

    calculated = true;

    return true;
}
Beispiel #3
0
void csImageMemory::Clear (const csRGBpixel &colour)
{
  if ((Format & CS_IMGFMT_MASK) != CS_IMGFMT_TRUECOLOR) return;

  EnsureImage ();

  uint32 *src = (uint32*) &colour;
  uint32 *dst = (uint32*)databuf->GetData ();

  int i;
  for (i = 0; i < Width*Height*Depth; i++, dst++)
    *dst = *src;
}
Beispiel #4
0
bool csImageMemory::Copy (iImage* simage_, int x, int y,
                          int width, int height )
{
  if (width<0) return false;
  if (height<0) return false;
  if (x+width>GetWidth()) return false;
  if (y+height>GetHeight()) return false;

  if (simage_->GetWidth()<width) return false;
  if (simage_->GetHeight()<height) return false;

  csRef<iImage> simage;
  if (simage_->GetFormat() != Format)
    simage.AttachNew (new csImageMemory (simage_, Format));
  else
    simage = simage_;
  EnsureImage();

  int i;
  if (Alpha)
  {
    for (i=0; i<height; i++)
      memcpy (Alpha + (i+y)*Width + x, simage->GetAlpha() + i*width, width);
  }

  if (databuf)
  {
    switch (Format & CS_IMGFMT_MASK)
    {
      case CS_IMGFMT_NONE:
        break;
      case CS_IMGFMT_PALETTED8:
        for ( i=0; i<height; i++ )
          memcpy ((uint8*)databuf->GetData () + (i+y)*Width + x,
                (uint8*)simage->GetImageData() + i*width,  width);
        break;
      case CS_IMGFMT_TRUECOLOR:
        for ( i=0; i<height; i++ )
          memcpy ((csRGBpixel*)databuf->GetData () + (i+y)*Width + x,
                (csRGBpixel*)simage->GetImageData() + i*width,
                width * sizeof (csRGBpixel));
        break;
    }
  }

  return true;
}
Beispiel #5
0
void csImageMemory::InternalConvertFromRGBA (iDataBuffer* imageData)
{
  int pixels = Width * Height * Depth;
  csRGBpixel* iImage = (csRGBpixel*)imageData->GetData();

  if ((Format & CS_IMGFMT_MASK) == CS_IMGFMT_ANY)
    Format = (Format & ~CS_IMGFMT_MASK) | CS_IMGFMT_TRUECOLOR;

  switch (Format & CS_IMGFMT_MASK)
  {
    case CS_IMGFMT_NONE:
    case CS_IMGFMT_PALETTED8:
      if (Format & CS_IMGFMT_ALPHA)
      {
        if (!Alpha)
          Alpha = new uint8 [pixels];
        int i;
        for (i = 0; i < pixels; i++)
          Alpha [i] = iImage [i].alpha;
      }
      if ((Format & CS_IMGFMT_MASK) == CS_IMGFMT_PALETTED8)
      {
        EnsureImage();
        // The most complex case: reduce an RGB image to a paletted image.
        int maxcolors = 256;
        csColorQuantizer quant;
        quant.Begin ();

        quant.Count (iImage, pixels);
        quant.Palette (Palette, maxcolors);
        uint8* img8 = (uint8*)databuf->GetData (); /* RemapDither() wants an uint8*&, casting
                                      * Image to that breaks strict-aliasing */
        quant.RemapDither (iImage, pixels, Width, Palette, maxcolors,
          img8, has_keycolour ? &keycolour : 0);
	CS_ASSERT (img8 == (uint8*)databuf->GetData ());

        quant.End ();
      }
      break;
    case CS_IMGFMT_TRUECOLOR:
      databuf = imageData;
      break;
  }
}
Beispiel #6
0
  bool VLFeat::CalculateCommon(int f, bool all, int l) {
    string msg = "VLFeat::CalculateCommon("+ToStr(f)+","+ToStr(all)+","+
      ToStr(l)+") : ";

    // if (!do_fisher && !do_vlad) {
    //   cerr << msg
    // 	   << "either encoding=fisher or encoding=vlad should be specified"
    // 	   << endl;
    //   return false;
    // }

    if (!gmm && !kmeans) {
      cerr << msg << "either gmm=xxx or kmeans=xxx option should be given"
	   << endl;
      return false;
    }

    cox::tictac::func tt(tics, "VLFeat::CalculateCommon");
    
    // obs! only some parameters here, should be in ProcessOptionsAndRemove()
    // too, also scales and geometry should be made specifiable...
    bool normalizeSift = false, renormalize = true, flat_window = true;
    size_t step = 3, binsize = 8;

    EnsureImage();

    int width = Width(true), height = Height(true);

    if (FrameVerbose())
      cout << msg+"wxh="
	   << width << "x" << height << "=" << width*height << endl;

    vector<float> rgbcoeff { 0.2989, 0.5870, 0.1140 };

    imagedata idata = CurrentFrame();
    idata.convert(imagedata::pixeldata_float);
    idata.force_one_channel(rgbcoeff);

    vector<float> dsift;
    size_t descr_size_orig = 0, descr_size_final = 0;
    vector<float> scales { 1.0000, 0.7071, 0.5000, 0.3536, 0.2500 };
    // vector<float> scales { 1.0000 };
    for (size_t i=0; i<scales.size(); i++) {
      if (KeyPointVerbose())
	cout << "Starting vl_dsift_process() in scale " << scales[i] << endl;
      
      imagedata simg = idata;
      if (scales[i]!=1) {
	scalinginfo si(simg.width(), simg.height(),
		       (int)floor(scales[i]*simg.width()+0.5),
		       (int)floor(scales[i]*simg.height()+0.5));
	simg.rescale(si, 1);
      }

      // VlDsiftFilter *sf = vl_dsift_new(simg.width(), simg.height());
      VlDsiftFilter *sf = vl_dsift_new_basic(simg.width(), simg.height(),
					     step, binsize);

      // opts.scales = logspace(log10(1), log10(.25), 5) ;
      // void vl_dsift_set_bounds	(	VlDsiftFilter * 	self,
      // 					int 	minX,
      // 					int 	minY,
      // 					int 	maxX,
      // 					int 	maxY 
      // 					);	
      
      // VlDsiftDescriptorGeometry geom = { 8, 4, 4, 0, 0 };
      // vl_dsift_set_geometry(sf, &geom);
      
      //vl_dsift_set_steps(sf, 3, 3);

      //vl_dsift_set_window_size(sf, 8);

      vl_dsift_set_flat_window(sf, flat_window); // aka fast in matlab

      vector<float> imgvec = simg.get_float();
      const float *img_fp = &imgvec[0];
      // cout << "IMAGE = " << img_fp[0] << " " << img_fp[1] << " "
      //      << img_fp[2] << " ... " << img_fp[41] << endl;

      vl_dsift_process(sf, img_fp);
      
      // if opts.rootSift // false
      // 		descrs{si} = sqrt(descrs{si}) ;
      // end
      // 	if opts.normalizeSift //true
      // 		  descrs{si} = snorm(descrs{si}) ;
      // end

      descr_size_orig = sf->descrSize;
      size_t nf = sf->numFrames;
      const VlDsiftKeypoint *k = sf->frames;
      float *d  = sf->descrs;
      
      if (KeyPointVerbose())
	cout << "  found " << sf->numFrames << " 'frames' in "
	     << simg.info() << endl
	     << "  descriptor dim " << descr_size_orig << endl;
      
      if (PixelVerbose())
	for (size_t i=0; i<nf; i++) {
	  cout << "  i=" << i << " x=" << k[i].x << " y=" << k[i].y
	       << " s=" << k[i].s << " norm=" << k[i].norm;
	  if (FullVerbose()) {
	    cout << " RAW";
	    for (size_t j=0; j<descr_size_orig; j++)
	      cout << " " << d[i*descr_size_orig+j];
	  }
	  cout << endl;
	}

      if (normalizeSift) {
	for (size_t i=0; i<nf; i++) {
	  if (PixelVerbose())
	    cout << "  i=" << i << " x=" << k[i].x << " y=" << k[i].y
		 << " s=" << k[i].s << " norm=" << k[i].norm;
	  double mul = 0.0;
	  for (size_t j=0; j<descr_size_orig; j++)
	    mul += d[i*descr_size_orig+j]*d[i*descr_size_orig+j];
	  if (mul)
	    mul = 1.0/sqrt(mul);
	  if (FullVerbose())
	    cout << " NORM";
	  for (size_t j=0; j<descr_size_orig; j++) {
	    d[i*descr_size_orig+j] *= mul;
	    if (FullVerbose())
	      cout << " " << d[i*descr_size_orig+j];
	  }
	  if (PixelVerbose())
	    cout << endl;
	}
      }

      if (!pca.vector_length()) {
	dsift.insert(dsift.end(), d, d+nf*descr_size_orig);
	descr_size_final = descr_size_orig;

      } else {
	for (size_t i=0; i<nf; i++) {
	  vector<float> vin(d+i*descr_size_orig, d+(i+1)*descr_size_orig);
	  vector<float> vout = pca.projection_coeff(vin);
	  dsift.insert(dsift.end(), vout.begin(), vout.end());
	}
	descr_size_final = pca.base_size();
      }
	  
      vl_dsift_delete(sf);
    }

    size_t numdata = dsift.size()/descr_size_final;
    const float *datain = &dsift[0];

    vector<float> enc((do_fisher?2:1)*descriptor_dim()*nclusters());
    float *dataout = &enc[0];
      
    if (do_fisher) {
      if (FrameVerbose())
	cout << msg << "fisher encoding " << numdata
	     << " descriptors of size " << descr_size_orig
	     << " => " << descr_size_final
	     << " with gmm dimensionality " << descriptor_dim()
	     << endl;
      
      if (descr_size_final!=descriptor_dim()) {
	cerr << msg << "dimensionality mismatch descr_size_final="
	     << descr_size_final << " descriptor_dim()=" << descriptor_dim() 
	     << endl;
	return false;
      }

      vl_fisher_encode(dataout, VL_TYPE_FLOAT, means(), descriptor_dim(),
		       nclusters(), covariances(), priors(),
		       datain, numdata, VL_FISHER_FLAG_IMPROVED) ;
    }

    if (do_vlad) {
      //obs! correct use of pca?

      if (FrameVerbose())
	cout << msg << "vlad encoding " << numdata
	     << " descriptors of size " << descr_size_final << endl;

      vector<vl_uint32> indexes(numdata);
      vector<float> distances(numdata);

      if (kdtree)
	vl_kdforest_query_with_array(kdtree, &indexes[0], 1, numdata, &distances[0], datain);
      else
	vl_kmeans_quantize(kmeans, &indexes[0], &distances[0], datain, numdata);

      vector<float> assignments(numdata*nclusters());
      for (size_t i=0; i<numdata; i++)
	assignments[i * nclusters() + indexes[i]] = 1;
      
      int vlad_flags = VL_VLAD_FLAG_SQUARE_ROOT|VL_VLAD_FLAG_NORMALIZE_COMPONENTS;

      vl_vlad_encode(dataout, VL_TYPE_FLOAT, means(), descriptor_dim(),
		     nclusters(), datain, numdata, &assignments[0],
		     vlad_flags);
    }

    if (renormalize) {
      if (PixelVerbose())
	cout << " RENORM:";
      double mul = 0.0;
      for (size_t j=0; j<enc.size(); j++)
	mul += enc[j]*enc[j];
      if (mul)
	mul = 1.0/sqrt(mul);
      for (size_t j=0; j<enc.size(); j++) {
	if (PixelVerbose())
	  cout << " " << enc[j];
	enc[j] *= mul;
	if (PixelVerbose())
	  cout << "->" << enc[j];
      }
      if (PixelVerbose())
	cout << endl;
    }

    ((VectorData*)GetData(0))->setVector(enc);
    
    return true;
  }
Beispiel #7
0
void csImageMemory::InternalConvertFromPal8 (iDataBuffer* imageData, 
                                             uint8* alpha, 
                                             csRGBpixel* iPalette,
                                             int nPalColors)
{
  int pixels = Width * Height * Depth;

  // ensure the palette has at least 256 entries.
  if (nPalColors < 256)
  {
    csRGBpixel *newPal = new csRGBpixel [256];
    memcpy ((void*)newPal, (const void*)iPalette,
      nPalColors * sizeof(csRGBpixel));
    delete[] iPalette;
    iPalette = newPal;
  }

  if ((Format & CS_IMGFMT_MASK) == CS_IMGFMT_ANY)
    Format = (Format & ~CS_IMGFMT_MASK) | CS_IMGFMT_PALETTED8;

  switch (Format & CS_IMGFMT_MASK)
  {
    case CS_IMGFMT_NONE:
      delete[] iPalette;
      delete[] alpha;
      break;
    case CS_IMGFMT_PALETTED8:
      {
        databuf = imageData;
        Palette = iPalette;
        Alpha = alpha;
      }
      break;
    case CS_IMGFMT_TRUECOLOR:
    {
      uint8 *in = imageData->GetUint8();
      csRGBpixel *out;
      EnsureImage ();
      out = (csRGBpixel *)databuf->GetData ();

      if ((Format & CS_IMGFMT_ALPHA) && alpha)
      {
        uint8 *a = alpha;
        while (pixels--)
        {
          *out = iPalette [*in++];
          out->alpha = *a++;
          out++;
        }
      }
      else
        while (pixels--)
          *out++ = iPalette [*in++];
      delete[] alpha;
      delete[] iPalette;
      break;
    }
  }
  if ((Format & CS_IMGFMT_ALPHA)
   && ((Format & CS_IMGFMT_MASK) != CS_IMGFMT_TRUECOLOR)
   && !Alpha)
    Format &= ~CS_IMGFMT_ALPHA;
}
Beispiel #8
0
uint8* csImageMemory::GetAlphaPtr ()
{
  EnsureImage();
  return Alpha;
}
Beispiel #9
0
csRGBpixel* csImageMemory::GetPalettePtr ()
{
  EnsureImage();
  return Palette;
}
Beispiel #10
0
void* csImageMemory::GetImagePtr ()
{
  EnsureImage();
  return (void*)databuf->GetData ();
}