Moved some methods. and fixed the image blur filter

This commit is contained in:
Ziver Koc 2008-11-19 15:58:48 +00:00
parent ed68e21b05
commit 40b5173abe
8 changed files with 140 additions and 281 deletions

View file

@ -147,59 +147,6 @@ public abstract class ImageFilterProcessor {
return img;
}
/**
* Copies the given array to a new one that it returns
* @param data The data to duplicate
* @param cols The amount of columns
* @param rows The amount of rows
* @return The array copy
*/
public static int[][][] copyArray(int[][][] data,int cols,int rows){
int[][][] copy = new int[rows][cols][4];
for(int y=0; y<rows ;y++){
for(int x=0; x<cols ;x++){
copy[y][x][0] = data[y][x][0];
copy[y][x][1] = data[y][x][1];
copy[y][x][2] = data[y][x][2];
copy[y][x][3] = data[y][x][3];
}
}
return copy;
}
/**
* This method clips the values of the pixel so that they
* are in the range 0-255
* @param data The image data
* @param cols The amount of columns
* @param rows The amount of rows
*/
public static void clip(int[][][] data, int cols, int rows){
for(int y=0; y<rows ;y++){
for(int x=0; x<cols ;x++){
data[y][x][1] = clip(data[y][x][1]);
data[y][x][1] = clip(data[y][x][2]);
data[y][x][1] = clip(data[y][x][3]);
}
}
}
/**
* This method clips the values of a color so that it
* is in the range 0-255
* @param color
* @return
*/
public static int clip(int color){
if(color < 0)
return 0;
else if(color > 255)
return 255;
else
return color;
}
/**
* The underlying effect is run here
* @param data The raw image to apply the effect to

View file

@ -43,6 +43,25 @@ public class ImageUtil {
}
}
}
/**
* Normalizes the image data by the given scale
*
* @param output The output data array
* @param data The image data
* @param cols The number of columns
* @param rows The number of rows
* @param scale The scale to normalize the image by
*/
public static void normalize(int[][][] output, int[][][] data, int cols, int rows, double scale) {
for(int y=0; y<rows ;y++){
for(int x=0; x<cols ;x++){
output[y][x][1] = (int)(data[y][x][1] * scale);
output[y][x][2] = (int)(data[y][x][2] * scale);
output[y][x][3] = (int)(data[y][x][3] * scale);
}
}
}
/**
* Returns the rms value of the image
@ -167,4 +186,60 @@ public class ImageUtil {
}
return crop;
}
/**
* Copies the given array to a new one that it returns
*
* @param data The data to duplicate
* @param cols The amount of columns
* @param rows The amount of rows
* @return The array copy
*/
public static int[][][] copyArray(int[][][] data,int cols,int rows){
int[][][] copy = new int[rows][cols][4];
for(int y=0; y<rows ;y++){
for(int x=0; x<cols ;x++){
copy[y][x][0] = data[y][x][0];
copy[y][x][1] = data[y][x][1];
copy[y][x][2] = data[y][x][2];
copy[y][x][3] = data[y][x][3];
}
}
return copy;
}
/**
* This method clips the values of the pixel so that they
* are in the range 0-255
*
* @param data The image data
* @param cols The amount of columns
* @param rows The amount of rows
*/
public static void clip(int[][][] data, int cols, int rows){
for(int y=0; y<rows ;y++){
for(int x=0; x<cols ;x++){
data[y][x][1] = clip(data[y][x][1]);
data[y][x][1] = clip(data[y][x][2]);
data[y][x][1] = clip(data[y][x][3]);
}
}
}
/**
* This method clips the values of a color so that it
* is in the range 0-255
*
* @param color
* @return
*/
public static int clip(int color){
if(color < 0)
return 0;
else if(color > 255)
return 255;
else
return color;
}
}

View file

@ -26,52 +26,58 @@ public class BlurFilter extends ImageFilterProcessor{
super(img);
blurValue = blur;
}
@Override
public int[][][] process(final int[][][] data, int cols, int rows) {
int inputPeak = ImageUtil.peakValue(data, cols, rows);
int[][][] output = new int[rows][cols][4];
//Perform the convolution one or more times
// in succession
int[][][] tmpData = ImageUtil.copyArray(data, cols, rows);
//Perform the convolution one or more times in succession
int redSum, greenSum, blueSum, outputPeak;
for(int i=0; i<blurValue ;i++){
//Iterate on each pixel as a registration
// point.
for(int y=1; y<rows-2 ;y++){
setProgress(ZMath.percent(0, (blurValue-1)*(rows-3), i*(rows-3)+y));
for(int x=0+1; x<cols-2 ;x++){
int redSum =
data[y - 1][x - 1][1] +
data[y - 1][x - 0][1] +
data[y - 1][x + 1][1] +
data[y - 0][x - 1][1] +
data[y - 0][x - 0][1] +
data[y - 0][x + 1][1] +
data[y + 1][x - 1][1] +
data[y + 1][x - 0][1] +
data[y + 1][x + 1][1];
int greenSum =
data[y - 1][x - 1][2] +
data[y - 1][x - 0][2] +
data[y - 1][x + 1][2] +
data[y - 0][x - 1][2] +
data[y - 0][x - 0][2] +
data[y - 0][x + 1][2] +
data[y + 1][x - 1][2] +
data[y + 1][x - 0][2] +
data[y + 1][x + 1][2];
int blueSum =
data[y - 1][x - 1][3] +
data[y - 1][x - 0][3] +
data[y - 1][x + 1][3] +
data[y - 0][x - 1][3] +
data[y - 0][x - 0][3] +
data[y - 0][x + 1][3] +
data[y + 1][x - 1][3] +
data[y + 1][x - 0][3] +
data[y + 1][x + 1][3];
output[y][x][0] = data[y][x][0];
//Iterate on each pixel as a registration point.
for(int y=0; y<rows ;y++){
setProgress(ZMath.percent(0, (blurValue-1)*(rows-2), i*(rows-2)+y));
for(int x=0; x<cols ;x++){
if(x == 0 || x == cols-1 || y == 0 || y == rows-1){
redSum = tmpData[y][x][1] * 9;
greenSum = tmpData[y][x][2] * 9;
blueSum = tmpData[y][x][3] * 9;
}
else{
redSum =
tmpData[y - 1][x - 1][1] +
tmpData[y - 1][x - 0][1] +
tmpData[y - 1][x + 1][1] +
tmpData[y - 0][x - 1][1] +
tmpData[y - 0][x - 0][1] +
tmpData[y - 0][x + 1][1] +
tmpData[y + 1][x - 1][1] +
tmpData[y + 1][x - 0][1] +
tmpData[y + 1][x + 1][1];
greenSum =
tmpData[y - 1][x - 1][2] +
tmpData[y - 1][x - 0][2] +
tmpData[y - 1][x + 1][2] +
tmpData[y - 0][x - 1][2] +
tmpData[y - 0][x - 0][2] +
tmpData[y - 0][x + 1][2] +
tmpData[y + 1][x - 1][2] +
tmpData[y + 1][x - 0][2] +
tmpData[y + 1][x + 1][2];
blueSum =
tmpData[y - 1][x - 1][3] +
tmpData[y - 1][x - 0][3] +
tmpData[y - 1][x + 1][3] +
tmpData[y - 0][x - 1][3] +
tmpData[y - 0][x - 0][3] +
tmpData[y - 0][x + 1][3] +
tmpData[y + 1][x - 1][3] +
tmpData[y + 1][x - 0][3] +
tmpData[y + 1][x + 1][3];
}
output[y][x][0] = tmpData[y][x][0];
output[y][x][1] = redSum;
output[y][x][2] = greenSum;
output[y][x][3] = blueSum;
@ -79,10 +85,10 @@ public class BlurFilter extends ImageFilterProcessor{
}
// getting the new peak value and normalizing the image
int outputPeak = ImageUtil.peakValue(output, cols, rows);
ImageUtil.normalize(output, cols, rows, ((double)inputPeak)/outputPeak );
outputPeak = ImageUtil.peakValue(output, cols, rows);
ImageUtil.normalize(tmpData, output, cols, rows, ((double)inputPeak)/outputPeak );
}
return output;
return tmpData;
}
}

View file

@ -33,13 +33,13 @@ public class ContrastBrightnessFilter extends ImageFilterProcessor{
public int[][][] process(final int[][][] data, int cols, int rows) {
int mean = ImageUtil.meanValue(data, cols, rows);
int[][][] output = copyArray(data, cols, rows);
int[][][] output = ImageUtil.copyArray(data, cols, rows);
ImageUtil.addMeanValue(output, cols, rows, mean*(-1));
ImageUtil.scale(output, cols, rows, contrast);
ImageUtil.addMeanValue(output, cols, rows, (int)(brightness*mean));
clip(output , cols, rows);
ImageUtil.clip(output ,cols, rows);
return output;
}

View file

@ -3,6 +3,7 @@ package zutil.image.filters;
import java.awt.image.BufferedImage;
import zutil.image.ImageFilterProcessor;
import zutil.image.ImageUtil;
import zutil.math.ZMath;
@ -41,7 +42,7 @@ public class DitheringFilter extends ImageFilterProcessor{
int error, index;
int[] currentPixel;
int[][][] output = copyArray(data, cols, rows);
int[][][] output = ImageUtil.copyArray(data, cols, rows);
for(int y=0; y<rows ;y++){
setProgress(ZMath.percent(0, rows-1, y));
@ -53,14 +54,14 @@ public class DitheringFilter extends ImageFilterProcessor{
for (int i = 1; i < 4; i++) {
error = currentPixel[i] - palette[index][i];
if (x + 1 < cols) {
output[y+0][x+1][i] = clip( output[y+0][x+1][i] + (error*7)/16 );
output[y+0][x+1][i] = ImageUtil.clip( output[y+0][x+1][i] + (error*7)/16 );
}
if (y + 1 < rows) {
if (x - 1 > 0)
output[y+1][x-1][i] = clip( output[y+1][x-1][i] + (error*3)/16 );
output[y+1][x+0][i] = clip( output[y+1][x+0][i] + (error*5)/16 );
output[y+1][x-1][i] = ImageUtil.clip( output[y+1][x-1][i] + (error*3)/16 );
output[y+1][x+0][i] = ImageUtil.clip( output[y+1][x+0][i] + (error*5)/16 );
if (x + 1 < cols)
output[y+1][x+1][i] = clip( output[y+1][x+1][i] + (error*1)/16 );
output[y+1][x+1][i] = ImageUtil.clip( output[y+1][x+1][i] + (error*1)/16 );
}
}
}

View file

@ -3,6 +3,7 @@ package zutil.image.filters;
import java.awt.image.BufferedImage;
import zutil.image.ImageFilterProcessor;
import zutil.image.ImageUtil;
import zutil.math.ZMath;
public class SpotLightFilter extends ImageFilterProcessor{
@ -62,9 +63,9 @@ public class SpotLightFilter extends ImageFilterProcessor{
}
output[y][x][0] = data[y][x][0];
output[y][x][1] = clip((int)(scale * data[y][x][1]));
output[y][x][2] = clip((int)(scale * data[y][x][2]));
output[y][x][3] = clip((int)(scale * data[y][x][3]));
output[y][x][1] = ImageUtil.clip((int)(scale * data[y][x][1]));
output[y][x][2] = ImageUtil.clip((int)(scale * data[y][x][2]));
output[y][x][3] = ImageUtil.clip((int)(scale * data[y][x][3]));
}
}
return output;

View file

@ -31,7 +31,7 @@ import zutil.image.filters.SpotLightFilter;
@SuppressWarnings("unused")
public class ImageProcessorTest implements ProgressListener{
private static String imgPath = "Image6.gif";
private static String imgPath = "exemple.gif";
private JLabel processedLabel;
private JLabel orginalLabel;
@ -64,11 +64,11 @@ public class ImageProcessorTest implements ProgressListener{
//ImageFilterProcessor processor = new SpotLightFilter(img,100,100,100);
//ImageFilterProcessor processor = new ContrastBrightnessFilter(img);
//ImageFilterProcessor processor = new ColorIntensityFilter(img, true);
//ImageFilterProcessor processor = new BlurFilter(img);
ImageFilterProcessor processor = new BlurFilter(img, 100);
//ImageFilterProcessor processor = new DitheringFilter(img);
//ImageFilterProcessor processor = new ResizeImage(img,100,100);
//ImageFilterProcessor processor = new MedianFilter(img);
ImageFilterProcessor processor = new FaceDetectionFilter(img);
//ImageFilterProcessor processor = new FaceDetectionFilter(img);
processor.setProgressListener(this);
procImg = processor.process();