Beispiel #1
0
size_t Database::LoadFromTextFile( std::istream  &is) 
{
	is.unsetf(std::ios_base::skipws);
	zones.clear();
	char c=0;
	while( is && !is.eof() ) 
	{
		q_zone z_temp(is, c);
		if (z_temp.size()==0) 
		{
			ErrorInZone(zones.size());		
		}
		zones.push_back(z_temp);
		is >> c;
		if (is) 
		{
			switch (c)
			{
			case '\n':
				c = 0;
				break;
			default:
				c = atoi(&c);
				is.get();				
				break;
			}
		}
	}
	return zones.size();
}
// Détermine les descripteurs de Fourier d'un contour
std::vector<std::complex<double>> descripteur_fourier_normal (std::vector< std::vector<cv::Point> > contour, int c_max) {
  int c_min = -c_max;
  int length = contour[0].size();
  double x_moy, y_moy;

  for (auto& pt : contour[0]) {
    x_moy += pt.x;
    y_moy += pt.y;
  }
  x_moy = x_moy / (length);
  y_moy = y_moy / (length);
  
  std::complex<double> z_moy(x_moy, y_moy);

  std::vector<std::complex<double>> serie;
  std::vector<std::complex<double>> spectre;

  for (auto& pt : contour[0]) {
    std::complex<double> z_temp(pt.x, pt.y);
    z_temp = z_temp - z_moy;
    serie.push_back(z_temp);
  }

  cv::dft(serie, spectre,cv::DFT_SCALE + cv::DFT_COMPLEX_OUTPUT);

  std::vector<std::complex<double>> coeff;

  for (int i = 0; i < c_max; ++i) {
    coeff.push_back(spectre[i + spectre.size() - c_max]);
  }

  for (int i = 0; i < c_max + 1; ++i) {
    coeff.push_back(spectre[i]);
  }

  if (std::abs(coeff[c_max - 1]) > std::abs(coeff[c_max + 1])) {
    std::reverse(coeff.begin(), coeff.end());
  }

  double phi = std::arg(coeff[c_max - 1] * coeff[c_max + 1]) / 2.0;

  for (auto& i : coeff) {
    i = i * std::polar ((double) 1.0, -phi);
    }

  double theta = std::arg(coeff[c_max + 1]);
  
  for (int k = c_min; k <= c_max; ++k) {
    coeff[k+c_max] = coeff[k+c_max] * std::polar((double) 1.0, (double) (-k) * theta); 
    }

  double module = std::abs(coeff[c_max + 1]);

  // Normalisation
  for (auto& value : coeff) {
    value = value / module;
    }

  return coeff;
}