//! write your image processing algorithms in this method
void RegionDistanceTransformation::_processImages() {

	Box extent = _getBox();

	if (_globalDistanceField) delete _globalDistanceField;
	_globalDistanceField = new labField((extent.x1-extent.x0),(extent.y1-extent.y0));
	_globalDistanceField->fillWith(32000);
	_componentIDs = new labField((extent.x1-extent.x0),(extent.y1-extent.y0));
	_componentIDs->fillWith(-1.0);

	calcConnectedComp();

	calcGlobalDistanceField();

	int min,max;
	min=320000;
	max=-320000;
	double pixel;

	int x, y;
	for (y = extent.y0+1; y < extent.y1-1; y++) {
		for (x = extent.x0+1; x < extent.x1-1; x++) {
			pixel = _globalDistanceField->getValueAt(x, y);
			if (pixel>max) max=pixel;
			if (pixel<min) min=pixel;
			_setPixelOut(0,x, y, 0, pixel);
			pixel = _componentIDs->getValueAt(x, y);			
			_setPixelOut(1,x, y, 0, pixel);
		}
	}
	std::cout << "min=" << min << "  max=" << max << std::endl;
}
void RegionDistanceTransformation::calcGlobalDistanceField()
{
	ML_TRACE_IN("void RegionDistanceTransformation::calcGlobalDistanceField()");

	//Create distance field
	//First only for background or not
	unsigned long int pos;
	float a,b,c,d;
	int currentDistance = 0;
	float currentComponentID;
	Box extent = _getBox();
	for (int i=extent.x0+1; i<extent.x1-1; i++) //Rand lassen
	{
		for (int j=extent.y0+1; j<extent.y1-1; j++) //Rand lassen
		{			
			pos = j*(extent.x1-extent.x0)+i;
			currentDistance = _globalDistanceField->getValueAt(pos);
			a=_globalDistanceField->getValueAt(pos-1);
			b=_globalDistanceField->getValueAt(pos+1);
			c=_globalDistanceField->getValueAt(pos-(extent.x1-extent.x0));
			d=_globalDistanceField->getValueAt(pos+(extent.x1-extent.x0));
			currentComponentID = _componentIDs->getValueAt(i,j);

			if (currentComponentID==0.0) //is pixel a background pixel
			{			
				currentDistance = min(static_cast<float>(currentDistance),min(a+1,min(b+1,min(c+1,d+1))));
			}
			else
			{
				//Is there a background pixel in the 4-nei. then its a border pixel with distance=0
				//better: pixel that not belongs to component
				if (_componentIDs->getValueAt(i-1,j)!=currentComponentID ||
					_componentIDs->getValueAt(i+1,j)!=currentComponentID ||
					_componentIDs->getValueAt(i,j-1)!=currentComponentID ||
					_componentIDs->getValueAt(i,j+1)!=currentComponentID)
				{
					currentDistance = 0;
				}
				else
				{
					currentDistance = min(static_cast<float>(currentDistance),min(a+1,min(b+1,min(c+1,d+1))));
				}
			}
			_globalDistanceField->setValueAt(pos,currentDistance);
		}
	}

	//And reverse
	for (int i=extent.x1-2; i>extent.x0; i--) //Rand lassen
	{
		for (int j=extent.y1-2; j>extent.y0; j--) //Rand lassen
		{			
			pos = j*(extent.x1-extent.x0)+i;
			currentDistance = _globalDistanceField->getValueAt(pos);
			a=_globalDistanceField->getValueAt(pos-1);
			b=_globalDistanceField->getValueAt(pos+1);
			c=_globalDistanceField->getValueAt(pos-(extent.x1-extent.x0));
			d=_globalDistanceField->getValueAt(pos+(extent.x1-extent.x0));
			currentComponentID = _componentIDs->getValueAt(i,j);

			if (currentComponentID==0.0) //is pixel a background pixel
			{			
				currentDistance = min(static_cast<float>(currentDistance),min(a+1,min(b+1,min(c+1,d+1))));
			}
			else
			{
				//Is there a background pixel in the 4-nei. then its a border pixel with distance=0
				if (_componentIDs->getValueAt(i-1,j)!=currentComponentID ||
					_componentIDs->getValueAt(i+1,j)!=currentComponentID  ||
					_componentIDs->getValueAt(i,j-1)!=currentComponentID  ||
					_componentIDs->getValueAt(i,j+1)!=currentComponentID)
				{
					currentDistance = 0;
				}
				else
				{
					currentDistance = min(static_cast<float>(currentDistance),min(a+1,min(b+1,min(c+1,d+1))));
				}
			}
			_globalDistanceField->setValueAt(pos,currentDistance);
		}
	}
}
int RegionDistanceTransformation::calcConnectedComp()
{
	ML_TRACE_IN("int RegionDistanceTransformation::calcConnectedComp()");

	//Calculate the connected components
	multimap<int,int> voxelToWatch;
	unsigned long int pos;
	multimap<int,int>::iterator tempVoxel;
	int currentCompID=0;
	int tempX,tempY;
	float currentObjID;
	Box extent = _getBox();
	for (int i=extent.x0; i<extent.x1; i++)
	{
		for (int j=extent.y0; j<extent.y1; j++)
		{
			pos = j*(extent.x1-extent.x0)+i;
			if (_componentIDs->getValueAt(pos)==-1.0)
			{				
				currentCompID++;
				currentObjID = _getPixel(i,j,0);

				//Find neigbourhs
				voxelToWatch.clear();
				voxelToWatch.insert(pair<int,int>(i,j));
				while (voxelToWatch.size()>0)
				{
					//pop first voxel
					tempVoxel = voxelToWatch.begin();
					tempX=(int)tempVoxel->first;
					tempY=(int)tempVoxel->second;
					pos = tempY*(extent.x1-extent.x0)+tempX;
					voxelToWatch.erase(voxelToWatch.begin());

					if (_componentIDs->getValueAt(pos)==-1.0)
					{
						if (tempX<extent.x1-1)						
							if (_getPixel(tempX+1,tempY,0)==currentObjID
								&& _componentIDs->getValueAt(pos+1)==-1.0)
								voxelToWatch.insert(pair<int,int>(tempX+1,tempY));												

						if (tempX>0)
							if (_getPixel(tempX-1,tempY,0)==currentObjID
								&& _componentIDs->getValueAt(pos-1)==-1.0)
								voxelToWatch.insert(pair<int,int>(tempX-1,tempY));

						if (tempY<extent.y1-1)
							if (_getPixel(tempX,tempY+1,0)==currentObjID 
								&& _componentIDs->getValueAt(pos+(extent.x1-extent.x0))==-1.0)
								voxelToWatch.insert(pair<int,int>(tempX,tempY+1));

						if (tempY>0)
							if (_getPixel(tempX,tempY-1,0)==currentObjID
								&& _componentIDs->getValueAt(pos-(extent.x1-extent.x0))==-1.0)
								voxelToWatch.insert(pair<int,int>(tempX,tempY-1));
					}

					if (currentObjID==0)
					{
						_componentIDs->setValueAt(pos,0);
					}
					else
					{
						int oldValue = _componentIDs->getValueAt(pos);
						if (oldValue!=currentCompID)
						{
							_componentIDs->setValueAt(pos,currentCompID);							
						}
					}
				}
			}
		}
	}

	return currentCompID;
}
Exemple #4
0
std::vector<double>
OBSpectrophore::GetSpectrophore(OpenBabel::OBMol* mol)
{
   // Clear the return variable
   _spectro.clear();

   // Atoms
   _nAtoms = mol->NumAtoms();
   if (_nAtoms < 3)
   {
      std::cerr << "OBSpectrophore::GetSpectrophore() error: not enough atoms in molecule" << std::endl;;
      return _spectro;
   }

   // Coordinate and property arrays
   _oricoor = new double*[_nAtoms];
   _coor = new double*[_nAtoms];
   double** REF1 = new double*[_nAtoms];
   double** REF2 = new double*[_nAtoms];
   _property = new double*[_nAtoms];
   for (unsigned int i = 0; i < _nAtoms; ++i)
   {
      _oricoor[i] = new double[3];
      _coor[i] = new double[3];
      REF1[i] = new double[3];
      REF2[i] = new double[3];
      _property[i] = new double[N_PROPERTIES];
   }

    // Atom radii and coordinates
   _radii = new double[_nAtoms];
   _getMoleculeData(mol);

   // Atom properties
   _calculateProperties(mol);

   // Shift molecule to its center of gravity and orient it in a standard way
   _orient();

   // Calculate the first spectrum to initiate values
   unsigned int sphoreSize(N_PROPERTIES * _numberOfProbes);
   std::vector<double> ENERGY(sphoreSize);
   std::vector<double> SPHORE(sphoreSize);

   // Properties
   _getBox(_oricoor);
   _getEnergies(_oricoor, &(ENERGY[0]));
   _initiateSpectrophore(&(ENERGY[0]), &(SPHORE[0]));

   // Rotate
   double psi;
   double cos_psi;
   double sin_psi;
   double theta;
   double cos_theta;
   double sin_theta;
   double phi;
   double cos_phi;
   double sin_phi;

   for (unsigned int i = 0; i < _rotationStepList.size(); ++i)
   {
      int rotationStep(_rotationStepList[i]);

      for (int iTheta = 0; iTheta < 180; iTheta += rotationStep)
      {
         theta = 0.017453292519943 * iTheta;
         cos_theta = cos(theta);
         sin_theta = sin(theta);
         _rotateY(_oricoor, REF1, cos_theta, sin_theta);

         for (int iPsi = 0; iPsi < 360; iPsi += rotationStep)
         {
            psi = 0.017453292519943 * iPsi;
            cos_psi = cos(psi);
            sin_psi = sin(psi);
            _rotateZ(REF1, REF2, cos_psi, sin_psi);

            for (int iPhi = 0; iPhi < 360; iPhi += rotationStep)
            {
               phi = 0.017453292519943 * iPhi;
               cos_phi = cos(phi);
               sin_phi = sin(phi);
               _rotateX(REF2, _coor, cos_phi, sin_phi);

               // Calculate energies
               _getBox(_coor);
               _getEnergies(_coor, &(ENERGY[0]));
               _updateSpectrophore(&(ENERGY[0]), &(SPHORE[0]));
            }
         }
      }
   }


   // Cleanup
   for (unsigned int i = 0; i < _nAtoms; ++i)
   {
      delete[] _property[i];
      delete[] _oricoor[i];
      delete[] REF1[i];
      delete[] REF2[i];
      delete[] _coor[i];
      _property[i] = NULL;
      _oricoor[i] = NULL;
      REF1[i] = NULL;
      REF2[i] = NULL;
      _coor[i] = NULL;
   }
   delete[] _radii;
   _radii = NULL;


   // Modify the actual sphore data
   _spectro.resize(sphoreSize);
   for (unsigned int i = 0; i < sphoreSize; ++i)
   {
      _spectro[i] = -100 * SPHORE[i];
   }

   // Normalisation
   double mean[N_PROPERTIES];
   double std[N_PROPERTIES];
   unsigned int m;
   for (unsigned int i(0); i < N_PROPERTIES; ++i)
   {
      mean[i] = 0.0;
      for (unsigned int n(0); n < 12; ++n)
      {
         m = (i * 12) + n;
         mean[i] += _spectro[m];
      }
      mean[i] /= 12.0;
      std[i] = 0.0;
      for (unsigned int n(0); n < 12; ++n)
      {
         m = (i * 12) + n;
         std[i] += (_spectro[m] - mean[i]) * (_spectro[m] - mean[i]);
      }
      std[i] /= 11.0;
      std[i] = sqrt(std[i]);
   }
   if ((_normalization == OBSpectrophore::NormalizationTowardsZeroMean) ||
       (_normalization == OBSpectrophore::NormalizationTowardsZeroMeanAndUnitStd))
   {
      for (unsigned int i(0); i < N_PROPERTIES; ++i)
      {
         for (unsigned int n(0); n < 12; ++n)
         {
            m = (i * 12) + n;
            _spectro[m] -= mean[i];
         }
      }
   }
   if ((_normalization == OBSpectrophore::NormalizationTowardsUnitStd) ||
       (_normalization == OBSpectrophore::NormalizationTowardsZeroMeanAndUnitStd))
   {
      for (unsigned int i(0); i < N_PROPERTIES; ++i)
      {
         for (unsigned int n(0); n < 12; ++n)
         {
            m = (i * 12) + n;
            _spectro[m] /= std[i];
         }
      }
   }

   // Return
   return _spectro;
}