Exemplo n.º 1
0
Img GaborImage::GaborTransform(Img Image, int Frequency, int Orientation) {
	orientation = Orientation;
	CalculateKernel(Orientation, Frequency);

	Img retImg  = (IplImage*) cvClone(Image);
	
	Img gabor_real = (IplImage*) cvClone(Image);
	Img gabor_img  = (IplImage*) cvClone(Image);
	cvFilter2D(Image, gabor_real, KernelRealData);	//image.Convolution(this.KernelRealData);
	cvFilter2D(Image, gabor_img , KernelImgData);	//image.Convolution(this.KernelImgData);
	
	cvPow(gabor_real, gabor_real, 2);
	cvPow(gabor_img,  gabor_img,  2);
	
	// Img gabor = (gabor_real + gabor_img).Pow(0.5);
	cvAdd(gabor_real, gabor_img, retImg);
	
	cv::Mat in = retImg;
	cv::Mat out;
	cv::sqrt(in, out); 
	
	IplImage dst_img = out;	
	
	cvReleaseImage(&gabor_real);
	cvReleaseImage(&gabor_img);
	
	retImg = (IplImage*) cvClone(&dst_img);
	
	return retImg;
}
void CBitPatternTreeMethod::buildFluxModes()
{
  CStepMatrix::const_iterator it = mpStepMatrix->begin();
  CStepMatrix::const_iterator end = mpStepMatrix->end();

  CVector< size_t > Indexes;
  C_INT NumSpecies = mExpandedStoiTranspose.numCols();

  for (; it != end; ++it)
    {
      getUnsetBitIndexes(*it, Indexes);

      C_INT NumReactions = Indexes.size();

      // Remove trivial modes, i.e., reversible reactions
      if (NumReactions == 2 &&
          (*mpReorderedReactions)[mReactionForward[Indexes[0]].first] ==
          (*mpReorderedReactions)[mReactionForward[Indexes[1]].first])
        {
          continue;
        }

      // Build the stoichiometry matrix reduced to the reactions participating in the current mode.
      CMatrix< C_INT64 > A(NumReactions, NumSpecies);

      size_t * pIndex = Indexes.array();
      size_t * pIndexEnd = pIndex + NumReactions;
      C_INT64 * pARow = A.array();

      for (; pIndex != pIndexEnd; ++pIndex, pARow += NumSpecies)
        {
          memcpy(pARow, &mExpandedStoiTranspose(*pIndex, 0), NumSpecies * sizeof(C_INT64));
        }

      // Calculate the kernel of the matrix
      CMatrix< C_INT64 > ExpandedStoiTranspose(A);
      CMatrix< C_INT64 > Kernel;
      CVector< size_t > Pivot;
      CalculateKernel(ExpandedStoiTranspose, Kernel, Pivot);

      size_t NumCols = Kernel.numCols();

      // Now we create the flux mode as we have the multiplier and reaction indexes.
      // We need to invert the sign of the multiplier for reactions which are not forward.
      // A flux mode is reversible if all reactions are reversible;

      C_INT64 * pColumn = Kernel.array();
      C_INT64 * pColumnEnd = pColumn + NumCols;

      for (; pColumn != pColumnEnd; ++pColumn)
        {
          std::map< size_t, C_FLOAT64 > Reactions;
          bool Reversible = true;

          pIndex = Indexes.array();
          C_INT64 * pFluxMultiplier = pColumn;

          for (; pIndex != pIndexEnd; ++pIndex, pFluxMultiplier += NumCols)
            {
              if (*pFluxMultiplier < 0)
                {
                  break;
                }
              else if (*pFluxMultiplier < 0)
                {
                  continue;
                }

              std::pair< size_t, bool > & ReactionForward = mReactionForward[*pIndex];

              Reactions[ReactionForward.first] =
                (ReactionForward.second == true) ? *pFluxMultiplier : -*pFluxMultiplier;

              if (!(*mpReorderedReactions)[ReactionForward.first]->isReversible())
                {
                  Reversible = false;
                }
            }

          if (pIndex != pIndexEnd)
            {
              continue;
            }

          addMode(CFluxMode(Reactions, Reversible));
        }
    }
}
void CBitPatternTreeMethod::buildKernelMatrix(CMatrix< C_INT64 > & kernelInt)
{
  // Calculate the kernel matrix
  // of the reduced stoichiometry matrix to get the kernel matrix for the:
  //   Nullspace Approach to Determine the Elementary Modes of Chemical Reaction Systems (Wagner 2004)

  CCopasiVector< CReaction >::const_iterator itReaction = mpModel->getReactions().begin();
  CCopasiVector< CReaction >::const_iterator endReaction = mpModel->getReactions().end();

  size_t ReactionCounter = 0;

  for (; itReaction != endReaction; ++itReaction, ++ReactionCounter)
    {
      if ((*itReaction)->isReversible())
        {
          // mpReorderedReactons->push_back(*itReaction);
          mReactionForward.push_back(std::make_pair(ReactionCounter, false));
        }

      mpReorderedReactions->push_back(*itReaction);
      mReactionForward.push_back(std::make_pair(ReactionCounter, true));
    }

  const CMatrix< C_FLOAT64 > & Stoi = mpModel->getRedStoi();

  size_t NumReactions = Stoi.numCols();

  size_t NumExpandedReactions = mReactionForward.size();

  size_t NumSpecies = Stoi.numRows();

  C_INT32 Dim = std::min(NumExpandedReactions, NumSpecies);

  if (Dim == 0)
    {
      return;
    }

  mExpandedStoiTranspose.resize(NumExpandedReactions, NumSpecies);

  const C_FLOAT64 *pStoi = Stoi.array();
  const C_FLOAT64 *pStoiEnd = pStoi + Stoi.size();
  const C_FLOAT64 *pStoiRowEnd;

  C_INT64 *pExpandedStoiTranspose;
  C_INT64 *pExpandedStoiTransposeColumn = mExpandedStoiTranspose.array();

  std::vector< const CReaction * >::const_iterator itReactionPivot;
  std::vector< const CReaction * >::const_iterator endReactionPivot;
  std::vector< std::pair< size_t, bool > >::const_iterator itReactionExpansion;

  for (; pStoi != pStoiEnd; ++pExpandedStoiTransposeColumn)
    {
      pStoiRowEnd = pStoi + NumReactions;
      pExpandedStoiTranspose = pExpandedStoiTransposeColumn;
      itReactionExpansion = mReactionForward.begin();

      for (; pStoi < pStoiRowEnd; ++pStoi, pExpandedStoiTranspose += NumSpecies, ++itReactionExpansion)
        {
          // TODO We should check the we have integer stoichiometry.
          if (itReactionExpansion->second == false)
            {
              *pExpandedStoiTranspose = -floor(*pStoi + 0.5);

              // Advance the iterators
              ++itReactionExpansion;
              pExpandedStoiTranspose += NumSpecies;
            }

          *pExpandedStoiTranspose = floor(*pStoi + 0.5);
        }
    }

  // Calculate the kernel of the matrix
  CMatrix< C_INT64 > ExpandedStoiTranspose(mExpandedStoiTranspose);
  CalculateKernel(ExpandedStoiTranspose, kernelInt, mReactionPivot);

  return;
}