mrf.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "mrf.h"
  7. using namespace OBJREC;
  8. void MRF::initialize()
  9. {
  10. m_initialized = 1;
  11. if ( m_dataType == ARRAY )
  12. setData(m_e->m_dataCost->m_costArray);
  13. else setData(m_e->m_dataCost->m_costFn);
  14. if ( m_smoothType == FUNCTION )
  15. setSmoothness(m_e->m_smoothCost->m_costFn);
  16. else
  17. {
  18. if ( m_smoothType == ARRAY )
  19. {
  20. checkArray(m_e->m_smoothCost->m_V);
  21. setSmoothness(m_e->m_smoothCost->m_V);
  22. }
  23. else
  24. {
  25. int smoothExp = m_e->m_smoothCost->m_smoothExp;
  26. if ( smoothExp != 1 && smoothExp != 2)
  27. {
  28. fprintf(stderr, "Wrong exponent in setSmoothness()!\n");
  29. exit(1);
  30. }
  31. setSmoothness(smoothExp, m_e->m_smoothCost->m_smoothMax, m_e->m_smoothCost->m_lambda);
  32. }
  33. if (m_e->m_smoothCost->m_varWeights )
  34. {
  35. if (!m_grid_graph)
  36. {
  37. fprintf(stderr, "Edge multiplier cannot be used with non-grid graphs\n");
  38. exit(1);
  39. }
  40. setCues(m_e->m_smoothCost->m_hWeights, m_e->m_smoothCost->m_vWeights);
  41. }
  42. }
  43. initializeAlg();
  44. }
  45. void MRF::commonInitialization(EnergyFunction *e)
  46. {
  47. m_dataType = e->m_dataCost->m_type;
  48. m_smoothType = e->m_smoothCost->m_type;
  49. m_varWeights = e->m_smoothCost->m_varWeights;
  50. m_initialized = 0;
  51. m_e = e;
  52. m_allocateArrayForSmoothnessCostFn = true;
  53. }
  54. MRF::MRF(int width, int height, int nLabels, EnergyFunction *e)
  55. {
  56. m_width = width;
  57. m_height = height;
  58. m_nLabels = nLabels;
  59. m_nPixels = width * height;
  60. m_grid_graph = 1;
  61. commonInitialization(e);
  62. }
  63. MRF::MRF(int nPixels, int nLabels, EnergyFunction *e)
  64. {
  65. m_nLabels = nLabels;
  66. m_nPixels = nPixels;
  67. m_grid_graph = 0;
  68. commonInitialization(e);
  69. }
  70. char MRF::checkEnergy()
  71. {
  72. if ( !m_initialized) {
  73. fprintf(stderr, "Call initialize() first,exiting!");
  74. exit(1);
  75. }
  76. return(2);
  77. }
  78. MRF::EnergyVal MRF::totalEnergy()
  79. {
  80. if (!isValid()) {
  81. fprintf(stderr, "totalEnergy() cannot be called for invalid energy!\n");
  82. exit(1);
  83. }
  84. if (!m_initialized) {
  85. fprintf(stderr, "Call initialize() first!\n");
  86. exit(1);
  87. }
  88. return dataEnergy() + smoothnessEnergy();
  89. }
  90. void MRF::optimize(int nIterations, float& time)
  91. {
  92. if (!isValid()) {
  93. fprintf(stderr, "optimize() cannot be called for invalid energy!\n");
  94. exit(1);
  95. }
  96. if (!m_initialized ) {
  97. fprintf(stderr, "run initialize() first!\n");
  98. exit(1);
  99. }
  100. // start timer
  101. clock_t start = clock();
  102. optimizeAlg(nIterations);
  103. // stop timer
  104. clock_t finish = clock();
  105. time = (float) (((double)(finish - start)) / CLOCKS_PER_SEC);
  106. }
  107. void MRF::checkArray(CostVal *V)
  108. {
  109. int i, j;
  110. for (i = 0; i < m_nLabels; i++)
  111. for (j = i; j < m_nLabels; j++)
  112. if (V[i*m_nLabels + j] != V[j*m_nLabels + i])
  113. {
  114. fprintf(stderr, "Error in setSmoothness(): V is not symmetric!\n");
  115. exit(1);
  116. }
  117. }