Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. function [IGrad, tSol, k] = ...
  2. poissonSolverJacobi (IGrad, Mask, B, maxIter, tau)
  3. %
  4. % poissonSolverJacobi -- Jacobi iterative solver for poisson equation
  5. %
  6. % SYNTAX
  7. %
  8. % [IGrad, tSol, k] = poissonSolverJacobi (IGrad, Mask, B, nMaxIter, tau)
  9. %
  10. % INPUT
  11. %
  12. % IGrad Original gradient field [M-by-N]
  13. % Mask Mask specifies the solving domain [M-by-N, logical]
  14. % B Guidance gradient field [M-by-N]
  15. % maxIter The maximum iteration of the solver [integer]
  16. % tau The error tolerance level [scalar]
  17. %
  18. % OUTPUT
  19. %
  20. % IGrad Output gradient field [M-by-N]
  21. % tSol Time for solving the poisson equation [scalar]
  22. % k The maximum iteration number reached [scalar]
  23. %
  24. % DESCRIPTION
  25. %
  26. % [IGrad, tSol, k] = poissonSolverJacobi (IGrad, Mask, B, nMaxIter, tau)
  27. % solves the poisson equation using Jacobi iteration.
  28. %
  29. % NOTES
  30. %
  31. % Simple and naive Jacobi solver.
  32. %
  33. % REFERENCE(S)
  34. %
  35. % P?rez, Patrick, Michel Gangnet, and Andrew Blake.
  36. % "Poisson image editing." ACM Transactions on Graphics (TOG).
  37. % Vol. 22. No. 3. ACM, 2003.
  38. %
  39. % Solving the Discrete Poisson Equation using Jacobi, SOR,
  40. % Conjugate Gradients, and the FFT
  41. % https://people.eecs.berkeley.edu/~demmel/cs267/lecture24/lecture24.html
  42. %
  43. %
  44. tic;
  45. %% Masked domain
  46.  
  47.  
  48. Mask1 = double((Mask));
  49.  
  50. mIdx = find(Mask1 == 1);
  51.  
  52. Mask2 = mod(mIdx, 2);
  53. odds = mIdx(Mask2== 1);
  54. evens = mIdx(Mask2== 0);
  55.  
  56. %odd
  57. %even
  58.  
  59. %Find the black nodes
  60. %Find the red nodes
  61.  
  62. fprintf('starting');
  63. w = 1.97;
  64. %% Jacobi Iteration
  65. for k = 1 : maxIter
  66.  
  67.  
  68. % previous-step solution
  69. IGradPrev = IGrad;
  70.  
  71. % Update step
  72. IGrad(odds) = IGradPrev(odds) + w * ( IGrad(odds+1) + IGrad(odds-1) + ...
  73. IGrad(odds+size(IGrad, 1)) + ...
  74. IGrad(odds-size(IGrad, 1)) - B(odds) - 4*IGradPrev(odds) ) / 4;
  75.  
  76. IGrad(evens) = IGradPrev(evens)+ w*( IGrad(evens+1) + IGrad(evens-1) + ...
  77. IGrad(evens+size(IGrad, 1)) + ...
  78. IGrad(evens-size(IGrad, 1)) - B(evens) - 4*IGradPrev(evens) ) / 4;
  79.  
  80. % termination condition (convergence; solution stagnation)
  81. if (sqrt(sum((IGrad(mIdx) - IGradPrev(mIdx)).^2))) < tau
  82. break
  83. end
  84.  
  85. end
  86. tSol = toc;
  87.  
  88. end
  89.  
  90.  
  91. %%------------------------------------------------------------
  92. %
  93. % AUTHORS
  94. %
  95. % Tiancheng Liu tcliu@cs.duke.edu
  96. %
  97. % REVISIONS
  98. %
  99. % 0.1 (Spring 2017) - Tiancheng
  100. %
  101. % ------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement