Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package de.htw.cbir.feature;
  2.  
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferedImage;
  5.  
  6. import de.htw.cbir.model.Pic;
  7. import de.htw.cbir.model.Settings;
  8.  
  9. public class ColorMeanSaturation extends FeatureFactory
  10. {
  11.  
  12. public ColorMeanSaturation(Settings settings) {
  13. super(settings);
  14. }
  15.  
  16. ///////////////////////////////////////////
  17. // visualize the feature data as image
  18. //
  19. @Override
  20. public BufferedImage getFeatureImage(Pic image) {
  21.  
  22. int w = 1;
  23. int h = 1;
  24.  
  25. BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  26. Graphics2D big = bi.createGraphics();
  27.  
  28. int[] pixels = new int[h * w];
  29.  
  30. float[] featureVector = image.getFeatureVector();
  31. int lum = (int) featureVector[0];
  32. int a = (int) featureVector[1];
  33. int b = (int) featureVector[2];
  34. int re = (int) (lum - b);
  35. int bl = (int) (lum - a);
  36. if(re < 0)
  37. re = 0;
  38. if (re > 255)
  39. re = 255;
  40. if(bl <0)
  41. bl = 0;
  42. if(bl > 255)
  43. bl = 255;
  44. int gr = (int) (3* lum - re - bl);
  45.  
  46. System.out.println("Red: "+re);
  47. System.out.println("Blue: "+bl);
  48. System.out.println("Green: "+gr);
  49.  
  50.  
  51. if(gr < 0)
  52. gr += 256;
  53.  
  54. if(re < 0)
  55. re += 256;
  56. if(bl < 0)
  57. bl += 256;
  58.  
  59. pixels[0] = (0xFF << 24) | (re << 16) | (gr << 8) | bl;
  60.  
  61. //Rücktransformation
  62.  
  63. BufferedImage bThumb = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  64. bThumb.setRGB(0, 0, w, h, pixels, 0, w);
  65.  
  66. big.drawImage(bThumb, 0, 0, w, h, null);
  67. big.dispose();
  68. return bi;
  69. }
  70.  
  71. @Override
  72. public float[] getFeatureVector(Pic image)
  73. {
  74. BufferedImage bi = image.getDisplayImage();
  75.  
  76. int width = bi.getWidth();
  77. int height = bi.getHeight();
  78.  
  79. int [] rgbValues = new int[width * height];
  80.  
  81. bi.getRGB(0, 0, width, height, rgbValues, 0, width);
  82.  
  83.  
  84. // loop over the block
  85. int r = 0; int g = 0; int b = 0; int sum = 0;
  86.  
  87. for(int y=0; y < height; y++) {
  88. for (int x=0 ; x<width ; x++) {
  89. int pos = y*width + x;
  90. r += (rgbValues[pos] >> 16) & 255;
  91. g += (rgbValues[pos] >> 8) & 255;
  92. b += (rgbValues[pos] ) & 255;
  93. sum++;
  94. }
  95. }
  96.  
  97. r /= sum;
  98. g /= sum;
  99. b /= sum;
  100.  
  101. float[] featureVector = new float[3];
  102. float lum = (r+g+b)/3;
  103. featureVector[0] = lum;
  104. featureVector[1] = settings.getSaturation()*(lum-b);
  105. featureVector[2] = settings.getSaturation()*(lum-r);
  106.  
  107. return featureVector;
  108. }
  109.  
  110. @Override
  111. public float getDistance(float[] fv1, float[] fv2) {
  112. return getL1Distance(fv1, fv2);
  113. }
  114.  
  115. @Override
  116. public String getName() {
  117. return "ColorMeanSaturation (Saturation: " + settings.getSaturation() + ")";
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement