EdgeDetectionGUI.java

package ac.essex.ooechs.imaging.commons.edge; 
 
import ac.essex.ooechs.imaging.commons.util.panels.ImagePanel; 
import ac.essex.ooechs.imaging.commons.util.panels.FileTree; 
import ac.essex.ooechs.imaging.commons.util.ImageFilenameFilter; 
import ac.essex.ooechs.imaging.commons.PixelLoader; 
import ac.essex.ooechs.imaging.commons.ConvolutionMatrix; 
 
import javax.swing.*; 
import java.io.File; 
import java.awt.image.BufferedImage; 
import java.awt.*; 
 
public class EdgeDetectionGUI extends JFrame { 
 
    protected ImagePanel p; 
    protected JButton load, segment; 
 
    public EdgeDetectionGUI(File file) { 
        super("Edge Detection Browser"); 
 
        p = new ImagePanel(); 
        //p.setDisplayCentered(true); 
 
        FileTree f = new FileTree(file, new ImageFilenameFilter()) { 
 
            /** 
             * Called when a file is selected in the tree 
             */ 
            public void onSelectFile(File f) { 
                loadImage(f); 
            } 
 
        }; 
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, new JScrollPane(f), new JScrollPane(p)); 
        getContentPane().add(splitPane); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
        setSize(640, 480); 
        setVisible(true); 
    } 
 
    protected void loadImage(File f) { 
        if (f != null) { 
            try { 
                p.setImage(new PixelLoader(f)); 
                segment(); 
            } catch (Exception err) { 
                err.printStackTrace(); 
            } 
        } 
    } 
 
    protected void segment() { 
        new Thread() { 
            public void run() { 
                BufferedImage image = p.getImage(); 
 
                Graphics g = p.getGraphics(); 
                g.setColor(Color.RED); 
 
                PixelLoader pl = new PixelLoader(image); 
 
                ConvolutionMatrix HSOBEL = new ConvolutionMatrix(ConvolutionMatrix.HORIZONTAL_SOBEL); 
                ConvolutionMatrix VSOBEL = new ConvolutionMatrix(ConvolutionMatrix.VERTICAL_SOBEL); 
 
                for (int y = 0; y < image.getHeight(); y+=2) 
                    for (int x = 0; x < image.getWidth(); x+=2) { 
 
                        double variance = pl.get3x3Variance(x, y); 
 
                        if (variance < 2000) continue; 
 
                        //System.out.println(variance); 
 
                        double gx = pl.getConvolved(x, y, HSOBEL); 
                        double gy = pl.getConvolved(x, y, VSOBEL); 
 
                        int G = Math.abs((int) gx) + Math.abs((int) gy); 
                         
                        if (G > 200) { 
 
                            double angle = Math.atan(gy/gx); 
 
                            double lineLength = 4; 
                            int x1 = (int) (Math.cos(angle) * lineLength); 
                            int y1 = (int) (Math.sin(angle) * lineLength); 
 
                            g.drawLine(x - x1, y - y1, x + x1, y + y1); 
                        } 
                    } 
            } 
        }.start(); 
    } 
 
    public static void main(String args[]) { 
        File directory = new File("/home/ooechs/Desktop/Documents/Papers/Pipelines/data"); 
        new EdgeDetectionGUI(directory); 
    } 
 
}