Line.java

package ac.essex.ooechs.imaging.commons.texture; 
 
import ac.essex.ooechs.imaging.commons.PixelLoader; 
import ac.essex.ooechs.imaging.commons.StatisticsSolver; 
import ac.essex.ooechs.imaging.commons.fast.FastStatistics; 
 
/** 
 * Gets statistics along a line 
 * 
 * @author Olly Oechsle, University of Essex, Date: 05-Jul-2007 
 * @version 1.0 
 */ 
public class Line implements AbstractShape { 
 
    public static final int HORIZONTAL = 1; 
    public static final int VERTICAL = 2; 
 
    protected int length, orientation; 
 
    protected int edgeCount = -1; 
 
    public Line(int height, int orientation) { 
 
        this.length = height; 
        this.orientation = orientation; 
 
    } 
 
    public static void main(String[] args) throws Exception { 
        PixelLoader image = new PixelLoader("/home/ooechs/Desktop/Lenna.png"); 
        StatisticsSolver s = new StatisticsSolver(11); 
        for (int t = 0; t <= 10; t++) { 
            long start = System.currentTimeMillis(); 
            for (int i = 0; i <= 307200; i++) { 
                //image.getPerimeter2().getStatistics(image, 50, 50).getMean(); 
                //image.getHLine1().getStatistics(image, 50, 50).getMean(); 
                image.getHLine1().getMean(image, 50, 50); 
            } 
            long end = System.currentTimeMillis(); 
            long time = end - start; 
            if (t > 0) s.addData(time); 
            System.out.println(time); 
        } 
 
        System.out.println(s.getMean() + " | " + s.getStandardDeviation()); 
    } 
 
    public FastStatistics getStatistics(PixelLoader image, int x, int y) { 
 
        double halfImageStandardDeviation = image.getStdDeviation() / 2; 
 
        FastStatistics solver = new FastStatistics(); 
 
        int edges = 0; 
        int previous = -1; 
 
        if (orientation == HORIZONTAL) { 
            for (int i = -length; i < length; i++) { 
                try { 
                    int value = image.getGreyValue(x + i, y); 
                    solver.addData(value); 
                    if (previous != -1) { 
                        if (Math.abs(value - previous) > halfImageStandardDeviation) edges++; 
                        previous = value; 
                    } 
                } catch (Exception e) { 
 
                } 
            } 
        } 
 
        if (orientation == VERTICAL) { 
            for (int i = -length; i < length; i++) { 
                try { 
                    int value = image.getGreyValue(x, y + i); 
                    solver.addData(value); 
                    if (previous != -1) { 
                        if (Math.abs(value - previous) > halfImageStandardDeviation) edges++; 
                        previous = value; 
                    } 
                } catch (Exception e) { 
 
                } 
            } 
        } 
 
        edgeCount = edges; 
 
        return solver; 
 
    } 
 
    public float getMean(PixelLoader image, int x, int y) { 
 
        float total = 0; 
        int c = 0; 
 
        if (orientation == HORIZONTAL) { 
            for (int i = -length; i < length; i++) { 
                try { 
                    total += image.getGreyValue(x + i, y); 
                    c++; 
                } catch (Exception e) { 
 
                } 
            } 
        } 
 
        if (orientation == VERTICAL) { 
            for (int i = -length; i < length; i++) { 
                try { 
                    total += image.getGreyValue(x + i, y); 
                    c++; 
                } catch (Exception e) { 
 
                } 
            } 
        } 
 
        return total / c; 
 
    } 
 
    public int getEdgeCount(PixelLoader image, int x, int y) { 
        if (edgeCount != -1) { 
            getStatistics(image, x, y); 
        } 
        return edgeCount; 
    } 
 
}