Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. package algorithms;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.Comparator;
  7. import java.util.Random;
  8.  
  9. import model.GreyImage;
  10. import model.StructElement;
  11.  
  12. public class InPainting {
  13.  
  14. GreyImage inputImage;
  15. GreyImage mask;
  16. GreyImage endomage;;
  17. GreyImage outputImage;
  18. int windowSize;
  19. int patchSize;
  20.  
  21.  
  22. public InPainting(GreyImage inputImage,GreyImage mask,int windowsSize,int patchSize) {
  23. this.inputImage=inputImage;
  24. this.mask=mask;
  25. this.endomage=new GreyImage(inputImage.getSizeX(),inputImage.getSizeY());
  26. this.outputImage=new GreyImage(inputImage.getSizeX(),inputImage.getSizeY());
  27. aplyMask();
  28. for(int i=0;i<endomage.getNbPixels();i++){
  29. this.outputImage.setOffset(i,endomage.getOffset(i));
  30. }
  31. this.windowSize=windowsSize;
  32. this.patchSize=patchSize;
  33. }
  34.  
  35.  
  36.  
  37. public void aplyMask() {
  38. for (int i = 0; i < inputImage.getNbPixels(); i++) {
  39. endomage.setOffset(i, inputImage.getOffset(i));
  40. if(mask.getOffset(i)!=0)
  41. endomage.setOffset(i, 0);
  42. }
  43. }
  44. private double computeSimilarity(int x1, int y1, int x2, int y2){
  45. double res=0;;
  46. for(int y=-patchSize/2;y<patchSize/2;y++){
  47. for(int x=-patchSize/2;x<patchSize/2;x++){
  48. int val1=mask.getPixel(x1+x, y1+y);
  49. int val2=mask.getPixel(x2+x, y2+y);
  50. if(val1!=0 || val2!=0)
  51. res+=255*255;
  52. else
  53. res+=(val1-val2)*(val1-val2);
  54. }
  55.  
  56. }
  57. return res;
  58. }
  59.  
  60. private double[] distanceMin(int x,int y){
  61.  
  62. double[] min={0,0,Double.POSITIVE_INFINITY,0,0};
  63. for(int j=y-windowSize/2;j<y+windowSize/2;j++){
  64. for(int i=x-windowSize/2;i<x+windowSize/2;i++){
  65. if(x!=i && y!=j && mask.getPixel(i, j)==0){
  66. double w=computeSimilarity(x,y,i,j);
  67. if(w<min[2]){
  68. min[2]=w;
  69. min[0]=x;
  70. min[1]=y;
  71. min[3]=i;
  72. min[4]=j;
  73. }
  74. }
  75. }
  76. }
  77. return min;
  78. }
  79.  
  80. public void process(){
  81. ArrayList pixelD=new ArrayList();
  82. for (int i = 0; i < inputImage.getNbPixels(); i++) {
  83. if (mask.getOffset(i) != 0)
  84. pixelD.add(distanceMin(i%inputImage.getSizeX(),i/inputImage.getSizeX()));
  85. }
  86. while(!pixelD.isEmpty()){
  87. for (int i = 0; i < pixelD.size(); i++) {
  88. if (mask.getOffset(i) != 0)
  89. pixelD.set(i, distanceMin((int)((double[])pixelD.get(i))[0],(int)((double[])pixelD.get(i))[1]));
  90. }
  91. Collections.sort(pixelD,new Comparator<Object>(){
  92. public int compare(Object p11,Object p22){
  93. double[] p1=(double[])p11;
  94. double[] p2=(double[])p22;
  95. return (int) ( p1[2] - p2[2]);
  96. }
  97. });
  98. double[] px=(double[])pixelD.get(o);
  99. pixelD.remove(o);
  100. if(mask.getPixel((int)px[0], (int)px[1])!=0){//si cassé !!
  101.  
  102. for(int y=-patchSize/2;y<patchSize/2;y++){
  103. for(int x=-patchSize/2;x<patchSize/2;x++){
  104. int val=outputImage.getPixel((int)px[3]+x, (int)px[4]+y);
  105. if(mask.getPixel((int)px[0]+x, (int)px[1]+y)!=0){
  106. outputImage.setPixel((int)px[0]+x, (int)px[1]+y, val);
  107. mask.setPixel((int)px[0]+x, (int)px[1]+y, 0);
  108. }
  109. }
  110.  
  111. }
  112.  
  113. }
  114. }
  115. }
  116. /*
  117. public void process(){
  118. ArrayList pixelD=new ArrayList();
  119. for (int i = 0; i < inputImage.getNbPixels(); i++) {
  120. if (mask.getOffset(i) != 0)
  121. pixelD.add(distanceMin(i%inputImage.getSizeX(),i/inputImage.getSizeX()));
  122. }
  123.  
  124.  
  125. while(!pixelD.isEmpty()){
  126. pixelD=new ArrayList();
  127. for (int i = 0; i < inputImage.getNbPixels(); i++) {
  128. if (mask.getOffset(i) != 0)
  129. pixelD.add(distanceMin(i%inputImage.getSizeX(),i/inputImage.getSizeX()));
  130. }
  131. Collections.sort(pixelD,new Comparator<Object>(){
  132. public int compare(Object p11,Object p22){
  133. double[] p1=(double[])p11;
  134. double[] p2=(double[])p22;
  135. return (int) ( p1[2] - p2[2]);
  136. }
  137. });
  138.  
  139. double[] px=(double[])pixelD.get(o);
  140. pixelD.remove(o);
  141. if(mask.getPixel((int)px[0], (int)px[1])!=0){//si cassé !!
  142.  
  143. for(int y=-patchSize/2;y<patchSize/2;y++){
  144. for(int x=-patchSize/2;x<patchSize/2;x++){
  145. int val=outputImage.getPixel((int)px[3]+x, (int)px[4]+y);
  146. if(mask.getPixel((int)px[0]+x, (int)px[1]+y)!=0){
  147. outputImage.setPixel((int)px[0]+x, (int)px[1]+y, val);
  148. mask.setPixel((int)px[0]+x, (int)px[1]+y, 0);
  149. }
  150. }
  151.  
  152. }
  153.  
  154. }
  155. }
  156. }*/
  157.  
  158. public GreyImage getResult() {
  159. return outputImage;
  160. }
  161.  
  162. public GreyImage getBordel() {
  163. return endomage;
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement