📄 functions.java
字号:
package IrisRecog;/** * Functions.java * * Abstract class defines the static functions used throughout the * application. * * @owner Michael Huffman * @author Team 6: A. Bare, I. Chaugule,M. Huffman, M. Pearson, C. Schell * @version 0.0.1 * * CSC 480 Section 2 * Fall 2005 * 10/2/05 */ import java.io.*;import javax.swing.*;import java.net.*;import java.awt.*;import java.awt.image.*;import java.util.*;public abstract class Functions{ /** * Returns the lowercase extension of a file. * * @param f The file * @return the extension */ public static String getExtension(File f) { String str_Extension = null; String str_FileName = f.getName(); int i = str_FileName.lastIndexOf('.'); if (i > 0 && i < str_FileName.length() - 1) { str_Extension = str_FileName.substring(i+1).toLowerCase(); } return str_Extension; } /** * Creates an image icon from the specified path. * * @param path The string path to the file used to create the image icon * @return the image icon created */ public static ImageIcon createImageIcon(String path) { URL url_Image = Functions.class.getResource(path); if (url_Image != null) return new ImageIcon(url_Image); else return null; } /** * Converts the image into an integer double array of grayscale color. * * @param img The image to be converted * @return the integer double array */ public static int [][] convertToGrayscale(Image img) { PixelGrabber pgb_PixelGrabber; Color clr_Tmp; //Ensure the image is loaded completely new ImageIcon(img).getImage(); //Save the width and height of the image int int_Width = img.getWidth(null); int int_Height = img.getHeight(null); //Allocate new integer double array to hold all of the pixels in the image int [][] int_Arr_Pixels = new int [int_Width][int_Height]; //Allocated array to send to pixel grabber - NOTE: this is a one dimensional array int [] int_Dar_AllPix = new int [int_Width * int_Height]; try { pgb_PixelGrabber = new PixelGrabber(img, 0, 0, int_Width, int_Height, int_Dar_AllPix, 0, int_Width); pgb_PixelGrabber.grabPixels(); /* Convert one dimensional array to two dimensional array */ for(int i = 0; i < img.getWidth(null); i++) { for(int j = 0; j < img.getHeight(null); j++) { clr_Tmp = new Color(int_Dar_AllPix[(j * img.getWidth(null)) + i]); //Compute the grayscale of the color with the weights given (these are well documented) int_Arr_Pixels[i][j] = (int)(0.3 * clr_Tmp.getRed() + 0.59 * clr_Tmp.getGreen() + 0.11 * clr_Tmp.getBlue()); } } } catch(Exception e) { e.printStackTrace(); } return int_Arr_Pixels; } /** * Counts the number of pixels directly to the right that have an intensity * below the threshhold. (The similarity is depended on the variance) * * @param img The image to search * @param x The x location of the pixel that starts the block * @param y The y location of the pixel that starts the block * @return the counts of pixels to the right of the starting point that have * similar intensity */ public static int getCountPixelsToRightBelowThreshhold(MedianImage img, int x, int y, int t) { int int_Pixels = 1; //While the pixel is in the image and has an intesnity less than the intensity of //starting pixel (scaled by a variance). while( (x + int_Pixels) < img.getWidth() && img.getIntensity(x + int_Pixels, y) <= t ) { int_Pixels++; //increment pixel count } return int_Pixels; } /** * Determines if the vertical band going through the center of the block only contains * pixels that are less than the intensity of the block starting pixel. * * @param img The image to use in matching the band to the block start intensity * @param x The x location of the pixel that starts the block * @param y The y location of the pixel that starts the block * @return true if all of the pixels in the vertical band have intensities less * than the block starting pixel */ public static boolean verticalBlockBelowThreshhold(MedianImage img, int x, int y, int blockSize) { //Get the size of the vertial band, which is a percentage portion of the block size (x2 in each direction) int int_VerticalBlockSize = (int)((blockSize / 2) * Globals.PUPIL_BLOCK_SIZE_PORTION); //Get the intensity of the block starting pixel int int_Intensity = img.getIntensity(x, y); if(y - int_VerticalBlockSize < 0) return false; //For y values of -verticalBlockSize to +verticalBlockSize for(int i = (-1 * int_VerticalBlockSize); i <= int_VerticalBlockSize; i++) { //If the intensity at the current pixel is greater than the block //starting pixel's intensity, return false if(img.getIntensity(x, y + i) >= (int_Intensity + Globals.PUPIL_VARIANCE) ) { return false; } } return true; } /** * Returns the value of the pixel with the lowest intensity in the image. * * @param img The image to be searched for its lowest intensity * @return the lowest intensity found in the image */ public static int lowestIntensity(MedianImage img) { int int_Min = 255; //set default minimum to the maximum 8-bit intensity int int_Tmp = int_Min; //For each row in the image for(int i = 0; i < img.getWidth(); i++) { //For each column in the image for(int j = 0; j < img.getHeight(); j++) { //Retrieve the intensity of the pixel at the row, column int_Tmp = img.getIntensity(i, j); //If the intensity is less than the current minimum, update if(int_Tmp < int_Min) int_Min = int_Tmp; } } return int_Min; } /** * Converts the given polar coordinate, with a center of reference, to * cartesian point. * * @param center The point to be used as the center or the coordinate system * @param theta The angle portion of the polar coordinate * @param r The radius porition of the polar coordinate * @return Point The cartesian equivalent of the polar coordinate */ public static Point polarToCartesian(Point center, double theta, double r) { int int_X = (int)(Math.cos(theta) * r) + (int)(center.getX()); int int_Y = (int)(Math.sin(theta) * r) + (int)(center.getY()); return new Point(int_X, int_Y); } /** * Determines the amount of pixels, rather percentage of total amount of pixels, * laying on the edge of a circle defined the the center point and radius * that have an intensity in the given image that is equal to zero (black). * The function is meant to be applied to an image that has undergone canny * edge detection and therefore only consists of pixels that are either black or * white. * * @param img The image from which to test * @param center The center point of the circle to test * @param radius The radius of the circle to test * @return double The percentage of the total pixels in the defined circle that are black */ public static double circleOfBlackPixels(GrayscaleImage img, Point center, double radius) { int int_Pixels = 0; //For each degree in the circle for(int theta = 0; theta < 360; theta++) { //If any of the intensities in nearby circles pixels are black if( img.getIntensity(center, theta, radius - 3) == 0 || img.getIntensity(center, theta, radius - 2) == 0 || img.getIntensity(center, theta, radius - 1) == 0 || img.getIntensity(center, theta, radius) == 0 || img.getIntensity(center, theta, radius + 1) == 0 || img.getIntensity(center, theta, radius + 2) == 0 || img.getIntensity(center, theta, radius + 3) == 0) { int_Pixels++; //increment pixel count } } //return percentage of total pixels that are black return (double)int_Pixels / 360; } /** * Subtracts the second unwrapped image from the first using absolute value of * differences and returns the result. * * @param img1 The first unwrapped image * @param img2 The second unwrapped image * @return UnwrappedImage The absolute subtraction of the two images */ public static UnwrappedImage subtractAbsolute(GrayscaleImage img1, GrayscaleImage img2) { //Create subtraction image to use to return UnwrappedImage uwr_Subtraction = new UnwrappedImage(Constants.INT_UNWRAPPED_WIDTH, Constants.INT_UNWRAPPED_HEIGHT); //For each x for(int i = 0; i < Constants.INT_UNWRAPPED_WIDTH; i++) { //For each y for(int j = 0; j < Constants.INT_UNWRAPPED_HEIGHT; j++) { //Set value at x, y location to the absolute value of the result of the subtraction uwr_Subtraction.setIntensity(i, j, Math.abs(img1.getIntensity(i, j) - img2.getIntensity(i, j) )); } } //Return the subtractiong image return uwr_Subtraction; } /** * Serializes the agent object by serializing the memory of the agent to the * specified file. * * @param f The file to serialize the agent's memory to * @param a The agent whose memory is to be serialized to the file */ public static void serializeAgent(File f, Agent a) { try { FileOutputStream fos_FileOutputStream = new FileOutputStream(f); ObjectOutputStream oos_ObjectOutputStream = new ObjectOutputStream(fos_FileOutputStream); oos_ObjectOutputStream.writeObject(a.getMemory()); //Serialize the memory object of the agent oos_ObjectOutputStream.close(); } catch(Exception e) { e.printStackTrace(); } } /** * Deserializes the agent object by deserializing the memory of the agent * from the specified file. This process returns the memory as a result. * * @param f The file to deserialize the agent's memory from * @return Memory The memory contents in the specified file */ public static Memory deserializeAgent(File f) { Memory mem_Ret = null; try { FileInputStream fis_FileInputStream = new FileInputStream(f); ObjectInputStream ois_FileInputStream = new ObjectInputStream(fis_FileInputStream); mem_Ret = (Memory)ois_FileInputStream.readObject(); //deserialize the memory object from the file ois_FileInputStream.close(); } catch(Exception e) { e.printStackTrace(); } return mem_Ret; } /** * Applies a median filter to the subject, whereby creatinga median image * that is stored in the subject object. The median filter works by using * a window size that dictates the grid of pixels used in the median computation. * A window size of 3, for example, indicates that a window of 7 x 7 is to * be used (3 pixels to left, 3 pixels above, 3 pixels to right and 3 pixels
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -