All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ojil.algorithm.Gray8UnsignedBackgroundSubtract Maven / Gradle / Ivy

There is a newer version: 0.0.11
Show newest version
package com.github.ojil.algorithm;

import com.github.ojil.core.Error;
import com.github.ojil.core.Gray32Image;
import com.github.ojil.core.Gray8Image;
import com.github.ojil.core.Image;
import com.github.ojil.core.PipelineStage;

/**
 * Compensates for uneven background illumination in an input image,
 * at the same time changing an unsigned byte image to a signed
 * byte image, which is the type used through JJIL. Unsigned byte
 * images are supplied by, e.g., the Google G1 phone when operating
 * in preview mode. 
 * @author webb
 *
 */
public class Gray8UnsignedBackgroundSubtract extends PipelineStage {
	Gray32Image mg32 = null;
	int mnHeight;
	int mnWidth;
	
	/**
	 * Set the width and height of the window used for averaging when computing
	 * the background illumination.
	 * @param nWidth width to average over
	 */
	public Gray8UnsignedBackgroundSubtract(int nWidth, int nHeight) {
		this.mnWidth = nWidth;
		this.mnHeight = nHeight;
	}

	/**
	 * Compute an output Gray8Image which is the difference of the input
	 * unsigned Gray8Image and an average of a window of width x height
	 * size of the input. This is done using a cumulative sum operation
	 * so the operation is done efficiently.
	 */
	public void push(Image imageInput) throws Error {
        if (!(imageInput instanceof Gray8Image)) {
            throw new Error(
            				Error.PACKAGE.ALGORITHM,
            				ErrorCodes.IMAGE_NOT_GRAY8IMAGE,
            				imageInput.toString(),
            				null,
            				null);
        }
        if (this.mnWidth > imageInput.getWidth() || 
        		this.mnHeight > imageInput.getHeight()) {
            throw new Error(
    				Error.PACKAGE.ALGORITHM,
    				ErrorCodes.PARAMETER_OUT_OF_RANGE,
    				imageInput.toString(),
    				Integer.toString(this.mnWidth),
    				Integer.toString(this.mnHeight));
        }
        // if the image size is changed or this is the first time
        // allocate the intermediate Gray32Image.
        if (this.mg32 == null || 
        		!this.mg32.getSize().equals(imageInput.getSize())) {
        	this.mg32 = new Gray32Image(
        			imageInput.getWidth(), imageInput.getHeight());
        }
        Gray8Image gray = (Gray8Image) imageInput;
        Byte[] grayData = gray.getData();
        Integer[] gray32Data = this.mg32.getData();
        // First row
        int nSum = 0;
        for (int j=0; j=gray.getHeight()-this.mnHeight/2) {
        		nBottom = gray.getHeight()-1;
        		nTop = nBottom - this.mnHeight;
        	} else {
	        	nTop = i-this.mnHeight/2;
	        	nBottom = i+this.mnHeight/2;        		
        	}
	        for (int j=0; j




© 2015 - 2024 Weber Informatics LLC | Privacy Policy