Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (796)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: BurntPizza (posted 2015-05-10 15:53:18, viewed 314 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182  
import static java.util.stream.Collectors.*;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

/*

Over-zealous (ab)use of streams

*/

public class Hough {
    
    @SuppressWarnings("serial")
    public static void main(String[] args) throws IOException {
        
        BufferedImage img = ImageIO.read(new File("lines.png"));
        BufferedImage inv = filter(img);
        List<P3D> analysis = transform(points(inv));
        
        DoubleSummaryStatistics xstats = analysis.stream().mapToDouble(e -> e.x).summaryStatistics();
        DoubleSummaryStatistics ystats = analysis.stream().mapToDouble(e -> e.y).summaryStatistics();
        
        // used purely for the visuals
        List<P3D> norm = normalizeValues(analysis.stream().map(e -> {
            return new P3D(
                    remap(e.x, (float) xstats.getMin(), (float) xstats.getMax(), 0, img.getWidth() - 1),
                    remap(e.y, (float) ystats.getMin(), (float) ystats.getMax(), 0, img.getHeight() - 1),
                    (int) Math.pow(Math.min(e.v, 4) * 10, .5));
        }).collect(toList()));
        
        BufferedImage mapped = toImage(img.getWidth(), img.getHeight(), norm);
        
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new JPanel() {
            {
                setSize(img.getWidth() * 5 + 5, img.getHeight());
                setPreferredSize(getSize());
            }
            
            @Override
            public void paint(Graphics g) {
                g.setColor(Color.gray);
                g.fillRect(0, 0, getWidth(), getHeight());
                
                g.drawImage(img, 0, 0, null);
                g.drawImage(inv, img.getWidth() + 1, 0, null);
                g.drawImage(mapped, img.getWidth() * 2 + 2, 0, null);
                
                g.setColor(Color.red);
                g.translate(img.getWidth() * 3 + 3, 0);
                g.drawImage(img, 0, 0, null);
                
                IntSummaryStatistics vstats = analysis.stream().mapToInt(e -> e.v).summaryStatistics();
                
                analysis.stream().forEach(e -> {
                    float b = e.x;
                    float m = e.y;
                    g.setColor(new Color(255, 0, 0, (int) remap(e.v, vstats.getMin(), vstats.getMax(), 0, 255)));
                    g.drawLine(0, Math.round(b), img.getWidth(), Math.round(img.getWidth() * m + b));
                });
                
                g.translate(img.getWidth() + 1, 0);
                g.drawImage(img, 0, 0, null);
                g.setColor(Color.red);
                
                // cheating a bit with limit(4)
                analysis.stream().sorted((e1, e2) -> Integer.compare(e2.v, e1.v)).limit(4).forEach(e -> {
                    float b = e.x;
                    float m = e.y;
                    g.drawLine(0, Math.round(b), img.getWidth(), Math.round(img.getWidth() * m + b));
                });
            }
        });
        
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    private static List<P3D> normalizeValues(List<P3D> in) {
        Map<Point2D.Float, Integer> map = in.stream().collect(toMap(e -> new Point2D.Float(e.x, e.y), e -> e.v, (i, j) -> i + j));
        IntSummaryStatistics vstats = map.values().stream().mapToInt(i -> i).summaryStatistics();
        
        return map.entrySet().stream().map(e -> {
            return new P3D(e.getKey().x, e.getKey().y, (int) remap(e.getValue(), vstats.getMin(), vstats.getMax(), 0, 255));
        }).collect(toList());
    }
    
    private static BufferedImage toImage(int w, int h, List<P3D> in) {
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
        byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
        
        in.stream().forEach(e -> {
            data[Math.round(e.x + e.y * w)] = (byte) e.v;
        });
        
        return img;
    }
    
    private static List<P3D> transform(List<Point> in) {
        Map<Point2D.Float, Integer> map = new HashMap<>();
        
        for (int i = 0; i < in.size(); i++) {
            Point p = in.get(i);
            for (int j = 0; j < in.size(); j++) {
                if (j != i) {
                    Point q = in.get(j);
                    int dx = q.x - p.x;
                    int dy = q.y - p.y;
                    if (dx != 0) {
                        float m = dy / (float) dx;
                        float b = p.y - m * p.x;
                        map.merge(new Point2D.Float(b, m), 1, (k, h) -> k + h);
                    }
                }
            }
        }
        
        return map.entrySet().stream().map(e -> new P3D(e.getKey().x, e.getKey().y, e.getValue())).collect(toList());
    }
    
    private static BufferedImage filter(BufferedImage img) {
        BufferedImage b = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        byte[] dst = ((DataBufferByte) b.getRaster().getDataBuffer()).getData();
        
        int threshold = 8;
        
        for (int y = 0; y < b.getHeight(); y++) {
            for (int x = 0; x < b.getWidth(); x++) {
                dst[x + y * b.getWidth()] = (byte) ((luma(img.getRGB(x, y)) >= threshold ? 255 : 0));
            }
        }
        
        return b;
    }
    
    private static List<Point> points(BufferedImage img) {
        List<Point> list = new ArrayList<>();
        byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
        
        for (int y = 0; y < img.getHeight(); y++) {
            for (int x = 0; x < img.getWidth(); x++) {
                if (data[x + y * img.getWidth()] != 0) {
                    list.add(new Point(x, y));
                }
            }
        }
        
        return list;
    }
    
    private static byte luma(int rgb) {
        return (byte) ((int) (0.3 * ((rgb >> 16) & 0xFF) + 0.59 * ((rgb >> 8) & 0xFF) + 0.11 * (rgb & 0xFF)) & 0xFF);
    }
    
    private static float remap(float value, float low1, float high1, float low2, float high2) {
        return low2 + (value - low1) * (high2 - low2) / (high1 - low1);
    }
    
    private static class P3D {
        float x, y;
        int v;
        
        P3D(float j, float k, int i) {
            x = j;
            y = k;
            v = i;
        }
    }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
hadezbladez (3627 views)
2018-11-16 13:46:03

hadezbladez (1292 views)
2018-11-16 13:41:33

hadezbladez (3633 views)
2018-11-16 13:35:35

hadezbladez (695 views)
2018-11-16 13:32:03

EgonOlsen (3998 views)
2018-06-10 19:43:48

EgonOlsen (4525 views)
2018-06-10 19:43:44

EgonOlsen (2686 views)
2018-06-10 19:43:20

DesertCoockie (3546 views)
2018-05-13 18:23:11

nelsongames (3699 views)
2018-04-24 18:15:36

nelsongames (4723 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!