Advertisement
Guest User

Scattered Data Interpolation

a guest
Feb 6th, 2013
1,612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Color;
  2. import java.awt.Point;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Random;
  9.  
  10. import javax.imageio.ImageIO;
  11.  
  12. public class Interpolation {
  13.  
  14.     private List<WeightedPoint> weightedPoints = new ArrayList<WeightedPoint>();
  15.     private List<ColorRange> colorRanges = new ArrayList<ColorRange>();
  16.     private Point origin = new Point(0, 0);
  17.  
  18.     int power = 4;
  19.     Random random = new Random();
  20.  
  21.     public static void main(String[] args) {
  22.         // TODO Auto-generated method stub
  23.  
  24.         try {
  25.             File imageFile = new File("newImage.png");
  26.             ImageIO.write(new Interpolation().getImage(1366, 768), "png",
  27.                     imageFile);
  28.         } catch (IOException ex) {
  29.  
  30.         }
  31.     }
  32.  
  33.     public BufferedImage getImage(int width, int height) {
  34.  
  35.         BufferedImage bufferedImage = new BufferedImage(width, height,
  36.                 BufferedImage.TYPE_INT_RGB);
  37.  
  38.         initialState();
  39.         interpolateImage(bufferedImage);
  40.  
  41.         return bufferedImage;
  42.     }
  43.  
  44.     public void addWeightedPoint(WeightedPoint p) {
  45.         weightedPoints.add(p);
  46.     }
  47.  
  48.     public void addColorRange(ColorRange c) {
  49.         colorRanges.add(c);
  50.     }
  51.  
  52.     public Point getOrigin() {
  53.         return origin;
  54.     }
  55.  
  56.     public void setOrigin(Point origin) {
  57.         this.origin = origin;
  58.     }
  59.  
  60.     private void initialState() {
  61.  
  62.         float r = 0.34f;
  63.         int n = 10;
  64.         int max = 101;
  65.  
  66.         for (int i = 0; i < n; i++) {
  67.             r -= 0.3333f / n;
  68.  
  69.             colorRanges.add(new ColorRange(i * (max / n), i * (max / n)
  70.                     + (max / n), Color.getHSBColor(r, 1f, 1f)));
  71.         }
  72.  
  73.         /* Agregar puntos */
  74.         weightedPoints.add(new WeightedPoint(100, 100, 100));
  75.         weightedPoints.add(new WeightedPoint(300, 200, 0));
  76.         weightedPoints.add(new WeightedPoint(800, 550, 10));
  77.         weightedPoints.add(new WeightedPoint(100, 500, 50));
  78.         weightedPoints.add(new WeightedPoint(1200, 400, 80));
  79.         weightedPoints.add(new WeightedPoint(1100, 700, 70));
  80.         weightedPoints.add(new WeightedPoint(800, 550, 10));
  81.  
  82.     }
  83.  
  84.     private void interpolateImage(BufferedImage bufferedImage) {
  85.  
  86.         for (int i = 0; i < bufferedImage.getWidth(); i++) {
  87.             for (int j = 0; j < bufferedImage.getHeight(); j++) {
  88.                 bufferedImage.setRGB(i, j, getValueShepard(i, j));
  89.             }
  90.         }
  91.     }
  92.  
  93.     private int getValueShepard(int i, int j) {
  94.  
  95.         double dTotal = 0.0;
  96.         double result = 0.0;
  97.  
  98.         for (WeightedPoint p : weightedPoints) {
  99.  
  100.             double d = distance(p.getX() - origin.getX(),
  101.                     p.getY() - origin.getY(), i, j);
  102.             if (power != 1) {
  103.                 d = Math.pow(d, power);
  104.             }
  105.             d = Math.sqrt(d);
  106.             if (d > 0.0) {
  107.                 d = 1.0 / d;
  108.             } else { // if d is real small set the inverse to a large number
  109.                      // to avoid INF
  110.                 d = 1.e20;
  111.             }
  112.             result += p.getValue() * d;
  113.             dTotal += d;
  114.         }
  115.  
  116.         if (dTotal > 0) {
  117.             return getColor(result / dTotal);
  118.         } else {
  119.             return getColor(0);
  120.         }
  121.  
  122.     }
  123.  
  124.     private int getColor(double val) {
  125.         for (ColorRange r : colorRanges) {
  126.             if (val >= r.min && val < r.max) {
  127.                 return r.color.getRGB();
  128.             }
  129.         }
  130.         return 0;
  131.     }
  132.  
  133.     /**
  134.      * Calculates the distance between two points.
  135.      *
  136.      * @param xDataPt
  137.      *            the x coordinate.
  138.      * @param yDataPt
  139.      *            the y coordinate.
  140.      * @param xGrdPt
  141.      *            the x grid coordinate.
  142.      * @param yGrdPt
  143.      *            the y grid coordinate.
  144.      *
  145.      * @return The distance between two points.
  146.      */
  147.     private double distance(double xDataPt, double yDataPt, double xGrdPt,
  148.             double yGrdPt) {
  149.         double dx = xDataPt - xGrdPt;
  150.         double dy = yDataPt - yGrdPt;
  151.         return Math.sqrt(dx * dx + dy * dy);
  152.     }
  153.  
  154.     // bufferedImage.setRGB(i, j, new Random(100).nextInt());
  155.  
  156.     public class WeightedPoint {
  157.         int x;
  158.         int y;
  159.         int value;
  160.  
  161.         public WeightedPoint(int x, int y, int value) {
  162.  
  163.             this.x = x;
  164.             this.y = y;
  165.             this.value = value;
  166.         }
  167.  
  168.         public int getX() {
  169.             return x;
  170.         }
  171.  
  172.         public void setX(int x) {
  173.             this.x = x;
  174.         }
  175.  
  176.         public int getY() {
  177.             return y;
  178.         }
  179.  
  180.         public void setY(int y) {
  181.             this.y = y;
  182.         }
  183.  
  184.         public int getValue() {
  185.             return value;
  186.         }
  187.  
  188.         public void setValue(int value) {
  189.             this.value = value;
  190.         }
  191.     }
  192.  
  193.     public class ColorRange {
  194.         int min;
  195.         int max;
  196.         Color color;
  197.  
  198.         public ColorRange(int min, int max, Color color) {
  199.  
  200.             this.min = min;
  201.             this.max = max;
  202.             this.color = color;
  203.         }
  204.  
  205.         public int getMin() {
  206.             return min;
  207.         }
  208.  
  209.         public void setMin(int min) {
  210.             this.min = min;
  211.         }
  212.  
  213.         public int getMax() {
  214.             return max;
  215.         }
  216.  
  217.         public void setMax(int max) {
  218.             this.max = max;
  219.         }
  220.  
  221.         public Color getColor() {
  222.             return color;
  223.         }
  224.  
  225.         public void setColor(Color color) {
  226.             this.color = color;
  227.         }
  228.  
  229.     }
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement