Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.36 KB | None | 0 0
  1. package domainTransform;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.BufferedImage;
  5.  
  6. public class FilteringOptions {
  7.  
  8. public BufferedImage NormalizedConvolution(BufferedImage localImage, double sigma_space, double sigma_range) {
  9. int imageHeight = localImage.getHeight();
  10. int imageWidth = localImage.getWidth();
  11. int channels = 3; //MUDAR DEPOIS
  12.  
  13. double dIcdy[][][] = new double[imageHeight-1][imageWidth][4];
  14. double dIcdx[][][] = new double[imageHeight][imageWidth-1][4];
  15.  
  16. double diffX[][] = new double[imageHeight][imageWidth];
  17. double diffY[][] = new double[imageHeight][imageWidth];
  18.  
  19. double dHdx[][] = new double[imageHeight][imageWidth];
  20. double dVdy[][] = new double[imageHeight][imageWidth];
  21.  
  22. double ct_H[][] = new double[imageHeight][imageWidth];
  23. double ct_V[][] = new double[imageHeight][imageWidth];
  24.  
  25. Color rgbRow = null;
  26. Color rgbCol = null;
  27.  
  28. double sumH = 0;
  29. double sumV = 0;
  30.  
  31. int iterationsNum = 3;
  32.  
  33. /* DOMAIN TRANSFORM COMPUTATION */
  34.  
  35. //Horizontal and vertical partial derivatives estimate via finite differences
  36. Color rgb2 = new Color(localImage.getRGB(0, 0));
  37.  
  38. for (int col = 0; col < imageWidth; col++) {
  39. for (int row = 0; row < imageHeight; row++) {
  40.  
  41. Color rgb = new Color(localImage.getRGB(col, row));
  42. if(row < imageHeight - 1) {
  43. rgbRow = new Color(localImage.getRGB(col, row+1));
  44. double redDiffRow = rgbRow.getRed() - rgb.getRed();
  45. double greenDiffRow = rgbRow.getGreen() - rgb.getGreen();
  46. double blueDiffRow = rgbRow.getBlue() - rgb.getBlue();
  47. dIcdy[row][col][1] = redDiffRow/255;
  48. dIcdy[row][col][2] = greenDiffRow/255;
  49. dIcdy[row][col][3] = blueDiffRow/255;
  50. }
  51. if(col < imageWidth - 1) {
  52. rgbCol = new Color(localImage.getRGB(col+1, row));
  53. double redDiffCol = rgbCol.getRed() - rgb.getRed();
  54. double greenDiffCol = rgbCol.getGreen() - rgb.getGreen();
  55. double blueDiffCol = rgbCol.getBlue() - rgb.getBlue();
  56. dIcdx[row][col][1] = redDiffCol/255;
  57. dIcdx[row][col][2] = greenDiffCol/255;
  58. dIcdx[row][col][3] = blueDiffCol/255;
  59. }
  60.  
  61. if(row < imageHeight - 1)
  62. diffY[row+1][col] = Math.abs(dIcdy[row][col][1]) + Math.abs(dIcdy[row][col][2]) + Math.abs(dIcdy[row][col][3]);
  63.  
  64. if(col < imageWidth - 1)
  65. diffX[row][col+1] = Math.abs(dIcdx[row][col][1]) + Math.abs(dIcdx[row][col][2]) + Math.abs(dIcdx[row][col][3]);
  66.  
  67. //Derivatives of the horizontal and vertical domain transforms
  68. dHdx[row][col] = (1 + sigma_space/sigma_range * diffX[row][col]);
  69. dVdy[row][col] = (1 + sigma_space/sigma_range * diffY[row][col]);
  70. }
  71. }
  72.  
  73. //Integration of the domain transforms
  74. for(int row = 0; row < imageHeight; row++) {
  75. for(int col = 0; col < imageWidth; col++) {
  76. sumH = sumH + dHdx[row][col];
  77. ct_H[row][col] = sumH;
  78. }
  79. sumH = 0;
  80. }
  81.  
  82. for(int col = 0; col < imageWidth; col++) {
  83. for(int row = 0; row < imageHeight; row++) {
  84. sumV = sumV + dVdy[row][col];
  85. ct_V[row][col] = sumV;
  86. }
  87. sumV = 0;
  88. }
  89.  
  90. double transposeCt_V[][] = new double[imageWidth][imageHeight];
  91.  
  92. for(int row = 0; row < imageWidth; row++) {
  93. for(int col = 0; col < imageHeight; col++) {
  94. transposeCt_V[row][col] = ct_V[col][row];
  95. }
  96. }
  97.  
  98. double F[][][] = new double[imageHeight][imageWidth][channels+1];
  99.  
  100. /* FILTERING */
  101. double sigma_H = sigma_space;
  102. for(int i = 0; i < iterationsNum - 1; i++) {
  103. //Sigma value for iteration i
  104. double sigma_H_i = sigma_H * Math.sqrt(3) * Math.pow(2, iterationsNum - (i + 1)) / Math.sqrt(Math.pow(4, iterationsNum) - 1);
  105.  
  106. //Radius of the box filter with desired variance
  107. double boxRadius = Math.sqrt(3) * sigma_H_i;
  108.  
  109. for (int row = 0; row < imageHeight; row++) {
  110. for (int col = 0; col < imageWidth; col++) {
  111. Color rgb = new Color(localImage.getRGB(col, row));
  112. F[row][col][1] = (double)(rgb.getRed())/255;
  113. F[row][col][2] = (double)(rgb.getGreen())/255;
  114. F[row][col][3] = (double)(rgb.getBlue())/255;
  115. }
  116. }
  117.  
  118. F = TransformedDomainBoxFilterConvolution(F, ct_H, boxRadius);
  119. double transposeF[][][] = new double[imageWidth][imageHeight][channels+1];
  120.  
  121. for(int row = 0; row < imageHeight; row++) {
  122. for(int col = 0; col < imageWidth; col++) {
  123. for(int c = 1; c < channels+1; c++) {
  124. transposeF[col][row][c] = F[row][col][c];
  125. }
  126. }
  127. }
  128.  
  129. double auxF[][][] = new double[imageWidth][imageHeight][channels+1];
  130.  
  131. auxF = TransformedDomainBoxFilterConvolution(transposeF, transposeCt_V, boxRadius);
  132.  
  133. double newTransposeF[][][] = new double[imageHeight][imageWidth][channels+1];
  134.  
  135. for(int row = 0; row < imageHeight; row++) {
  136. for(int col = 0; col < imageWidth; col++) {
  137. for(int c = 1; c < channels+1; c++) {
  138. newTransposeF[row][col][c] = auxF[col][row][c];
  139. }
  140. }
  141. }
  142.  
  143. for(int row = 0; row < imageHeight; row++) {
  144. for(int col = 0; col < imageWidth; col++) {
  145. for(int c = 1; c < channels+1; c++) {
  146. F[row][col][c] = newTransposeF[row][col][c];
  147. }
  148. }
  149. }
  150. }
  151.  
  152. BufferedImage filteredImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
  153.  
  154. for(int row = 0; row < imageHeight; row++) {
  155. for(int col = 0; col < imageWidth; col++) {
  156. Color rgb = new Color((int)(F[row][col][1]*255), (int)(F[row][col][2]*255), (int)(F[row][col][3]*255));
  157. filteredImage.setRGB(col, row, rgb.getRGB());
  158. }
  159. }
  160.  
  161. return filteredImage;
  162. }
  163.  
  164. public double[][][] TransformedDomainBoxFilterConvolution(double F[][][], double domainPosition[][], double boxRadius) {
  165. int imageHeight = F.length;
  166. int imageWidth = F[0].length;
  167. int channels = 3;
  168.  
  169. //Determines the lower and upper limits of the box kernel in the transformed domain
  170. double lowerPosition[][] = new double[domainPosition.length][domainPosition[0].length];
  171. double upperPosition[][] = new double[domainPosition.length][domainPosition[0].length];
  172.  
  173. for(int row = 0; row < imageHeight; row++) {
  174. for(int col = 0; col < imageWidth; col++) {
  175. lowerPosition[row][col] = domainPosition[row][col] - boxRadius;
  176. upperPosition[row][col] = domainPosition[row][col] + boxRadius;
  177. }
  178. }
  179.  
  180. //Finds the indices of the pixels associated with the lower and upper limits of the box kernel
  181. int l_idx[][] = new int[domainPosition.length][domainPosition[0].length];
  182. int u_idx[][] = new int[domainPosition.length][domainPosition[0].length];
  183.  
  184. double domainPositionRow[] = new double[imageWidth+1];
  185. double l_pos_row[] = new double[domainPosition[0].length];
  186. double u_pos_row[] = new double[domainPosition[0].length];
  187. int local_l_idx[] = new int[imageWidth];
  188. int local_u_idx[] = new int[imageWidth];
  189.  
  190. int find = 0;
  191.  
  192. for(int row = 0; row < imageHeight; row++) {
  193.  
  194. for(int c = 0; c < imageWidth; c++) {
  195. domainPositionRow[c] = domainPosition[row][c];
  196. l_pos_row[c] = lowerPosition[row][c];
  197. u_pos_row[c] = upperPosition[row][c];
  198. }
  199.  
  200. domainPositionRow[imageWidth] = Double.POSITIVE_INFINITY;
  201.  
  202. for(int i = 0; i < imageWidth; i++) {
  203. local_l_idx[i] = 0;
  204. local_u_idx[i] = 0;
  205. }
  206.  
  207. for(int i = 0; i < imageWidth; i++) {
  208. if(domainPositionRow[i] > l_pos_row[0]) {
  209. local_l_idx[0] = i+1;
  210. break;
  211. }
  212. }
  213.  
  214. for(int i = 0; i < imageWidth; i++) {
  215. if(domainPositionRow[i] > u_pos_row[0]) {
  216. local_u_idx[0] = i+1;
  217. break;
  218. }
  219. }
  220.  
  221. for(int col = 1; col < imageWidth; col++) {
  222. find = 0;
  223. int counter = 0;
  224.  
  225. for(int i = local_l_idx[col-1]; i < imageWidth; i++) {
  226.  
  227. counter++;
  228. if(domainPositionRow[i-1] > l_pos_row[col]) {
  229. find = counter;
  230. break;
  231. }
  232. }
  233.  
  234. local_l_idx[col] = local_l_idx[col-1] + find - 1;
  235.  
  236. find = 0;
  237. counter = 0;
  238. for(int i = local_u_idx[col-1]; i < imageWidth; i++) {
  239. counter++;
  240. if(domainPositionRow[i-1] > u_pos_row[col]) {
  241. find = counter;
  242. break;
  243. }
  244. }
  245.  
  246. local_u_idx[col] = local_u_idx[col-1] + find - 1;
  247. }
  248.  
  249. for(int i = 0; i < imageWidth; i++) {
  250. l_idx[row][i] = local_l_idx[i];
  251. u_idx[row][i] = local_u_idx[i];
  252. }
  253.  
  254. }
  255.  
  256. //Box filter computation using summed area table
  257. double SAT[][][] = new double[imageHeight][imageWidth+1][channels+1];
  258. double sumR = 0.0;
  259. double sumG = 0.0;
  260. double sumB = 0.0;
  261.  
  262. for(int row = 0; row < imageHeight; row++) {
  263. for(int col = 0; col < imageWidth; col++) {
  264.  
  265. if(col == 0) {
  266. SAT[row][col][1] = 0.0;
  267. SAT[row][col][2] = 0.0;
  268. SAT[row][col][3] = 0.0;
  269. }
  270. else {
  271. sumR += F[row][col][1];
  272. sumG += F[row][col][2];
  273. sumB += F[row][col][3];
  274. SAT[row][col][1] = sumR;
  275. SAT[row][col][2] = sumG;
  276. SAT[row][col][3] = sumB;
  277. }
  278. }
  279. sumR = 0.0;
  280. sumG = 0.0;
  281. sumB = 0.0;
  282. }
  283.  
  284.  
  285. int row_indices[][] = new int[imageHeight][imageWidth];
  286.  
  287. for(int i = 0; i < imageHeight; i++) {
  288. for(int j = 0; j < imageWidth; j++) {
  289. row_indices[i][j] = i + 1;
  290. }
  291. }
  292.  
  293. int a[][] = new int[imageHeight][imageWidth];
  294. int b[][] = new int[imageHeight][imageWidth];
  295. int rowA, colA, channelsA, rowB, colB, channelsB, indexA, indexB;
  296.  
  297. for(int row = 0; row < imageHeight; row++) {
  298. for(int col = 0; col < imageWidth; col++) {
  299. for(int c = 1; c < 4; c++){
  300. a[row][col] = (imageHeight*imageWidth)*(c-1) + (l_idx[row][col]-1)*imageHeight + row_indices[row][col];
  301. b[row][col] = (imageHeight*imageWidth)*(c-1) + (u_idx[row][col]-1)*imageHeight + row_indices[row][col];
  302.  
  303. indexA = a[row][col];
  304. channelsA = indexA / (imageHeight*imageWidth) + 1;
  305. rowA = indexA - (channelsA-1)*(imageHeight*imageWidth);
  306.  
  307. if(rowA > imageHeight)
  308. rowA = rowA % imageHeight;
  309.  
  310. colA = (indexA - (channelsA-1)*(imageHeight*imageWidth)) / imageHeight + 1;
  311.  
  312. indexB = b[row][col];
  313. channelsB = indexB / (imageHeight*imageWidth) + 1;
  314. rowB = indexB - (channelsB-1)*(imageHeight*imageWidth);
  315.  
  316. if(rowB > imageHeight)
  317. rowB = rowB % imageHeight;
  318.  
  319.  
  320. colB = (indexB - (channelsB-1)*(imageHeight*imageWidth)) / imageHeight + 1;
  321.  
  322. if(rowB == 0)
  323. rowB = imageHeight;
  324. if(rowA == 0)
  325. rowA = imageHeight;
  326.  
  327. F[row][col][c] = (SAT[rowA-1][colA-1][channelsA] - SAT[rowB-1][colB-1][channelsB]) / (u_idx[row][col] - l_idx[row][col]);
  328. }
  329. }
  330. }
  331. return F;
  332.  
  333. }
  334.  
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement