Pacific-Design.com

    
Home Index

1. Computer Vision

2. Region Orientation

+ images

Computer Vision / Region Orientation /

Computing Orientation for Multi Regions withing an Image

package orientation;

/*------------------------------------------------------------------------------------------------
 Computing Central Moments for multiple Regions within a image
 Written by: Kevin Duraj
--------------------------------------------------------------------------------------------------*/
public class Moments {

    private int REGION = 0;
    
    /*--------------------------------------------------------------------------------------------*/
    public double moment(int[][] image, int p, int q) {
        
        double Mpq = 0.0;
        int height = image.length;
        int width  = image[0].length;
        
        for(int j=0; j < height; j++) {
            for(int i=0; i<width; i++) {
                if(image[j][i] == REGION) {
                    Mpq += Math.pow(i,p) * Math.pow(j,q);
                }
            }
        }
        
        return Mpq;
    }
    /*--------------------------------------------------------------------------------------------*/
    public double centralMoment(int region, int[][]image, int p, int q) {
        
        this.REGION = region;        
        double cMpq = 0.0;        
        int height = image.length;
        int width  = image[0].length;
        
        double m00 = moment(image, 0, 0); //m00 = region area
        double xCtr = moment(image, 1, 0) / m00;
        double yCtr = moment(image, 0, 1) / m00;
        
        for(int j=0; j<height; j++) {
            for(int i=0; i<width; i++) {
                if(image[j][i] == REGION) {
                    cMpq += Math.pow(i - xCtr, p) * Math.pow(j - yCtr, q);                    
                }
            }
        }
        return cMpq;
    }
    /*--------------------------------------------------------------------------------------------*/
    
}
/*------------------------------------------------------------------------------------------------*/
package orientation;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/*-------------------------------------------------------------------------------------------------
 Orientation describes the direction of the major axis, that is the axis 
 that runs through the centroid and along the widest part of the region.
 -------------------------------------------------------------------------------------------------*/
public class Main {

    /*--------------------------------------------------------------------------------------------*/
    public static void main(String[] args) throws IOException {

        int[][] image = ImageRead("src/images/assignment05.png");
        Moments moments = new Moments();

        for (int region = 1; region < 5; region++) {
            System.out.println("-------------- Region = " + region + " --------------");
            System.out.println("u00 = " + moments.centralMoment(region, image, 0, 0));

            System.out.println("u01 = 0 = " + moments.centralMoment(region, image, 0, 1));
            System.out.println("u10 = 0 = " + moments.centralMoment(region, image, 1, 0));

            System.out.println("u11 = " + moments.centralMoment(region, image, 1, 1));
            System.out.println("u20 = " + moments.centralMoment(region, image, 2, 0));
            System.out.println("u02 = " + moments.centralMoment(region, image, 0, 2));

            double u11 = moments.centralMoment(region, image, 1, 1);
            double u20 = moments.centralMoment(region, image, 2, 0);
            double u02 = moments.centralMoment(region, image, 0, 2);

            double theta = 0.5 * Math.atan2(2.0 * u11, u20 - u02);

            System.out.format("\nTheta              = %.3f\n", theta);
            System.out.format("Orientation Degree = %.3f\n", theta * 180.0 / Math.PI);
            
        }

    }
    /*--------------------------------------------------------------------------------------------*/

    private static int[][] ImageRead(String filename) throws IOException {

        File infile = new File(filename);
        BufferedImage bi = ImageIO.read(infile);

        int red[][] = new int[bi.getHeight()][bi.getWidth()];
        int grn[][] = new int[bi.getHeight()][bi.getWidth()];
        int blu[][] = new int[bi.getHeight()][bi.getWidth()];

        for (int i = 0; i < red.length; ++i) {
            for (int j = 0; j < red[i].length; ++j) {
                red[i][j] = bi.getRGB(j, i) >> 16 & 0xFF;
                grn[i][j] = bi.getRGB(j, i) >> 8 & 0xFF;
                blu[i][j] = bi.getRGB(j, i) & 0xFF;
            }
        }
        return grn;
    }
    /*--------------------------------------------------------------------------------------------*/

}
/*------------------------------------------------------------------------------------------------*/