sorry to disturb again but i like learning here. i am using JHLabs library on filters for buffered images.on running my code i am getting this exception:L
java.lang.ArrayIndexOutOfBoundsException: 4
at com.jhlabs.image.ConvolveFilter.convolveHV(ConvolveFilter.java:175)
at com.jhlabs.image.ConvolveFilter.convolve(ConvolveFilter.java:144)
at com.jhlabs.image.ConvolveFilter.filter(ConvolveFilter.java:107)
at Main$BlurMapper.map(Main.java:73)
at Main$BlurMapper.map(Main.java:13)
at org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:50)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:342)
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:138)
a) i am unable to understand the error message b)here is the code which i am running:
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.jhlabs.image.*;
import java.io.ByteArrayOutputStream;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
public class Main {
public static class BlurMapper extends MapReduceBase implements Mapper<Text, BytesWritable, LongWritable, BytesWritable>
{
OutputCollector<LongWritable, BytesWritable> goutput;
int IMAGE_HEIGHT = 240;
int IMAGE_WIDTH = 320;
int g=0;
byte[] prev_frame_arr= new byte[IMAGE_HEIGHT*IMAGE_WIDTH];
ByteArrayInputStream in=new ByteArrayInputStream(prev_frame_arr);
int alpha=1,ite=100;
// byte []uinitial=new float[IMAGE_HEIGHT*IMAGE_WIDTH];
//float []vinitial=new float[IMAGE_HEIGHT*IMAGE_WIDTH];
public void map(Text key, BytesWritable file,OutputCollector<LongWritable, BytesWritable> output, Reporter reporter) throws IOException
{
if(g==0){
in=new ByteArrayInputStream(file.getBytes());
g++;
}
else
{
byte []u=new byte[IMAGE_HEIGHT*IMAGE_WIDTH];
// u=uinitial;
byte []v=new byte[IMAGE_HEIGHT*IMAGE_WIDTH];
// v=vinitial;
BufferedImage curr_frame = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
BufferedImage prev_frame = ImageIO.read(in);
GaussianFilter p_f= new GaussianFilter(3);
BufferedImage curr_frame1=null;
BufferedImage prev_frame1=null;
//smoothening the images with gaussian filter using jhlabs library
prev_frame1= p_f.filter(prev_frame,prev_frame1);
curr_frame1= p_f.filter(curr_frame,curr_frame1);
BufferedImage curr_frame2=null;
BufferedImage prev_frame2=null;
prev_frame2= p_f.filter(prev_frame1,prev_frame2);
curr_frame2= p_f.filter(curr_frame1,curr_frame2);
BufferedImage fx1=null;
BufferedImage fy1=null;
BufferedImage ft1=null;
BufferedImage fx2=null;
BufferedImage fy2=null;
BufferedImage ft2=null;
byte[] f_x=new byte[IMAGE_HEIGHT*IMAGE_WIDTH];
byte[] f_y=new byte[IMAGE_HEIGHT*IMAGE_WIDTH];
byte[] f_t=new byte[IMAGE_HEIGHT*IMAGE_WIDTH];
float [] matrix1={-0.25f,0.25f,-0.25f,0.25f};
float [] matrix2={-0.25f,-0.25f,0.25f,0.25f};
float [] matrix3={0.25f,0.25f,0.25f,0.25f};
float [] matrix4={-0.25f,-0.25f,-0.25f,-0.25f};
ConvolveFilter ob1=new ConvolveFilter(2,2,matrix1);
ConvolveFilter ob2=new ConvolveFilter(2,2,matrix2);
ConvolveFilter ob3=new ConvolveFilter(2,2,matrix3);
ConvolveFilter ob4=new ConvolveFilter(2,2,matrix4);
***fx1= ob1.filter(prev_frame2,fx1);// this line is giving the error and may be following lines may also give***
fx2=ob1.filter(curr_frame2,fx2);
fy1=ob2.filter(prev_frame2,fy1);
fy2=ob2.filter(curr_frame2,fy2);
ft1=ob3.filter(prev_frame2,ft1);
ft2=ob4.filter(curr_frame2,ft2);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(fx1, "jpeg", baos);
byte[] fx_1 = baos.toByteArray();
ImageIO.write(fx2, "jpeg", baos);
byte[] fx_2 = baos.toByteArray();
ImageIO.write(fy1, "jpeg", baos);
byte[] fy_1 = baos.toByteArray();
ImageIO.write(fy2, "jpeg", baos);
byte[] fy_2 = baos.toByteArray();
ImageIO.write(ft1, "jpeg", baos);
byte[] ft_1 = baos.toByteArray();
ImageIO.write(ft2, "jpeg", baos);
byte[] ft_2 = baos.toByteArray();
for(int i=0;i<IMAGE_HEIGHT*IMAGE_WIDTH;i++)
{
f_x[i]=(byte)(fx_1[i]+fx_2[i]);
f_y[i]=(byte)(fy_1[i]+fy_2[i]);
f_t[i]=(byte)(ft_1[i]+ft_2[i]);
}
// BufferedImage fx=ImageIO.read(new ByteArrayInputStream(f_x));
// BufferedImage fy=ImageIO.read(new ByteArrayInputStream(f_y));
// BufferedImage ft=ImageIO.read(new ByteArrayInputStream(f_t));
//fx = conv2(im1,0.25* [-1 1; -1 1],'same') + conv2(im2, 0.25*[-1 1; -1 1],'same');
//fy = conv2(im1, 0.25*[-1 -1; 1 1], 'same') + conv2(im2, 0.25*[-1 -1; 1 1], 'same');
//ft = conv2(im1, 0.25*ones(2),'same') + conv2(im2, -0.25*ones(2),'same');
/*public BufferedImage produceRenderedImage(byte[] dataBuffer, int width, int height)
{
DataBuffer dBuffer = new DataBufferByte(dataBuffer, width * height);
WritableRaster wr = Raster.createInterleavedRaster(dBuffer,width,height,width,1,new int[]{0},null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel cm = new ComponentColorModel(cs,false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
BufferedImage bi = new BufferedImage(cm, wr, false, null);
return bi;
} */
//Averaging kernel
float []avg_matrix={0.083f,0.167f,0.083f,0.167f,0.0f,0.167f,0.083f,0.167f,0.083f};
BufferedImage uAvg=null;
BufferedImage vAvg=null;
ConvolveFilter ob=new ConvolveFilter(3,3,avg_matrix);
BufferedImage uu=ImageIO.read(new ByteArrayInputStream(u));//not the right way
BufferedImage vv=ImageIO.read(new ByteArrayInputStream(v));//not he right way
//Iterations
for (int i=1;i<=ite;i++)
{
uAvg=ob.filter(uu,uAvg);
vAvg=ob.filter(vv,vAvg);
//Compute local averages of the flow vectors
// uAvg=conv2(u,kernel_1,'same');
//vAvg=conv2(v,kernel_1,'same');
ImageIO.write(uu, "jpeg", baos);
byte[] u_u = baos.toByteArray();
ImageIO.write(vv, "jpeg", baos);
byte[] v_v = baos.toByteArray();
ImageIO.write(uAvg, "jpeg", baos);
byte[] u_avg = baos.toByteArray();
ImageIO.write(vAvg, "jpeg", baos);
byte[] v_avg = baos.toByteArray();byte x,y;
for(i=0;i<IMAGE_HEIGHT*IMAGE_WIDTH;i++)
{
x=(byte)((f_x[i]*u_avg[i])+(f_y[i]*v_avg[i])+f_t[i]);
y=(byte) (Math.pow(alpha, 2.0) + Math.pow(f_x[i], 2.0) + Math.pow(f_y[i], 2.0));
u_u[i]=(byte)(u_avg[i]-(x/y));
v_v[i]=(byte)(v_avg[i]-(x/y));
}
// % Compute flow vectors constrained by its local average and the optical flow constraints
//u= uAvg - ( fx .* ( ( fx .* uAvg ) + ( fy .* vAvg ) + ft ) ) ./ ( alpha^2 + fx.^2 + fy.^2);
//v= vAvg - ( fy .* ( ( fx .* uAvg ) + ( fy .* vAvg ) + ft ) ) ./ ( alpha^2 + fx.^2 + fy.^2);
//end
for(i=0;i<50;i++)
{
System.out.println(u_u[i]);
}
}
prev_frame_arr=file.getBytes();}
}
}
public static void main(String[] args) {
if(args.length!=2) {
System.err.println("Usage: blurvideo input output");
System.exit(-1);
}
JobClient client = new JobClient();
JobConf conf = new JobConf(Main.class);
conf.setOutputValueClass(BytesWritable.class);
conf.setInputFormat(SequenceFileInputFormat.class);
//conf.setNumMapTasks(n)
SequenceFileInputFormat.addInputPath(conf, new Path(args[0]));
SequenceFileOutputFormat.setOutputPath(conf, new Path(args[1]));
conf.setMapperClass(BlurMapper.class);
conf.setNumReduceTasks(0);
//conf.setReducerClass(org.apache.hadoop.mapred.lib.IdentityReducer.class);
client.setConf(conf);
try {
JobClient.runJob(conf);
} catch (Exception e) {
e.printStackTrace();
}
}
}
c) the code of ConvolveFilter is here, i am unable to find which array is going out of bound.
package com.jhlabs.image;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
public class ConvolveFilter extends AbstractBufferedImageOp {
/**
* Treat pixels off the edge as zero.
*/
public static int ZERO_EDGES = 0;
/**
* Clamp pixels off the edge to the nearest edge.
*/
public static int CLAMP_EDGES = 1;
/**
* Wrap pixels off the edge to the opposite edge.
*/
public static int WRAP_EDGES = 2;
/**
* The convolution kernel.
*/
protected Kernel kernel = null;
/**
* Whether to convolve alpha.
*/
protected boolean alpha = true;
/**
* Whether to promultiply the alpha before convolving.
*/
protected boolean premultiplyAlpha = true;
/**
* What do do at the image edges.
*/
private int edgeAction = CLAMP_EDGES;
/**
* Construct a filter with a null kernel. This is only useful if you're going to change the kernel later on.
*/
public ConvolveFilter() {
this(new float[9]);
}
/**
* Construct a filter with the given 3x3 kernel.
* @param matrix an array of 9 floats containing the kernel
*/
public ConvolveFilter(float[] matrix) {
this(new Kernel(3, 3, matrix));
}
/**
* Construct a filter with the given kernel.
* @param rows the number of rows in the kernel
* @param cols the number of columns in the kernel
* @param matrix an array of rows*cols floats containing the kernel
*/
public ConvolveFilter(int rows, int cols, float[] matrix) {
this(new Kernel(cols, rows, matrix));
}
/**
* Construct a filter with the given 3x3 kernel.
* @param kernel the convolution kernel
*/
public ConvolveFilter(Kernel kernel) {
this.kernel = kernel;
}
/**
* Set the convolution kernel.
* @param kernel the kernel
* @see #getKernel
*/
public void setKernel(Kernel kernel) {
this.kernel = kernel;
}
/**
* Get the convolution kernel.
* @return the kernel
* @see #setKernel
*/
public Kernel getKernel() {
return kernel;
}
/**
* Set the action to perfomr for pixels off the image edges.
* @param edgeAction the action
* @see #getEdgeAction
*/
public void setEdgeAction(int edgeAction) {
this.edgeAction = edgeAction;
}
/**
* Get the action to perfomr for pixels off the image edges.
* @return the action
* @see #setEdgeAction
*/
public int getEdgeAction() {
return edgeAction;
}
/**
* Set whether to convolve the alpha channel.
* @param useAlpha true to convolve the alpha
* @see #getUseAlpha
*/
public void setUseAlpha( boolean useAlpha ) {
this.alpha = useAlpha;
}
/**
* Get whether to convolve the alpha channel.
* @return true to convolve the alpha
* @see #setUseAlpha
*/
public boolean getUseAlpha() {
return alpha;
}
/**
* Set whether to premultiply the alpha channel.
* @param premultiplyAlpha true to premultiply the alpha
* @see #getPremultiplyAlpha
*/
public void setPremultiplyAlpha( boolean premultiplyAlpha ) {
this.premultiplyAlpha = premultiplyAlpha;
}
/**
* Get whether to premultiply the alpha channel.
* @return true to premultiply the alpha
* @see #setPremultiplyAlpha
*/
public boolean getPremultiplyAlpha() {
return premultiplyAlpha;
}
public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
int width = src.getWidth();
int height = src.getHeight();
if ( dst == null )
dst = createCompatibleDestImage( src, null );
int[] inPixels = new int[width*height];
int[] outPixels = new int[width*height];
getRGB( src, 0, 0, width, height, inPixels );
if ( premultiplyAlpha )
ImageMath.premultiply( inPixels, 0, inPixels.length );
convolve(kernel, inPixels, outPixels, width, height, alpha, edgeAction);
if ( premultiplyAlpha )
ImageMath.unpremultiply( outPixels, 0, outPixels.length );
setRGB( dst, 0, 0, width, height, outPixels );
return dst;
}
public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
if ( dstCM == null )
dstCM = src.getColorModel();
return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
}
public Rectangle2D getBounds2D( BufferedImage src ) {
return new Rectangle(0, 0, src.getWidth(), src.getHeight());
}
public Point2D getPoint2D( Point2D srcPt, Point2D dstPt ) {
if ( dstPt == null )
dstPt = new Point2D.Double();
dstPt.setLocation( srcPt.getX(), srcPt.getY() );
return dstPt;
}
public RenderingHints getRenderingHints() {
return null;
}
/**
* Convolve a block of pixels.
* @param kernel the kernel
* @param inPixels the input pixels
* @param outPixels the output pixels
* @param width the width
* @param height the height
* @param edgeAction what to do at the edges
*/
public static void convolve(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, int edgeAction) {
convolve(kernel, inPixels, outPixels, width, height, true, edgeAction);
}
/**
* Convolve a block of pixels.
* @param kernel the kernel
* @param inPixels the input pixels
* @param outPixels the output pixels
* @param width the width
* @param height the height
* @param alpha include alpha channel
* @param edgeAction what to do at the edges
*/
public static void convolve(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
if (kernel.getHeight() == 1)
convolveH(kernel, inPixels, outPixels, width, height, alpha, edgeAction);
else if (kernel.getWidth() == 1)
convolveV(kernel, inPixels, outPixels, width, height, alpha, edgeAction);
else
convolveHV(kernel, inPixels, outPixels, width, height, alpha, edgeAction);
}
/**
* Convolve with a 2D kernel.
* @param kernel the kernel
* @param inPixels the input pixels
* @param outPixels the output pixels
* @param width the width
* @param height the height
* @param alpha include alpha channel
* @param edgeAction what to do at the edges
*/
public static void convolveHV(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
int index = 0;
float[] matrix = kernel.getKernelData( null );
int rows = kernel.getHeight();
int cols = kernel.getWidth();
int rows2 = rows/2;
int cols2 = cols/2;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float r = 0, g = 0, b = 0, a = 0;
for (int row = -rows2; row <= rows2; row++) {
int iy = y+row;
int ioffset;
if (0 <= iy && iy < height)
ioffset = iy*width;
else if ( edgeAction == CLAMP_EDGES )
ioffset = y*width;
else if ( edgeAction == WRAP_EDGES )
ioffset = ((iy+height) % height) * width;
else
continue;
int moffset = cols*(row+rows2)+cols2;
for (int col = -cols2; col <= cols2; col++) {
float f = matrix[moffset+col];
if (f != 0) {
int ix = x+col;
if (!(0 <= ix && ix < width)) {
if ( edgeAction == CLAMP_EDGES )
ix = x;
else if ( edgeAction == WRAP_EDGES )
ix = (x+width) % width;
else
continue;
}
int rgb = inPixels[ioffset+ix];
a += f * ((rgb >> 24) & 0xff);
r += f * ((rgb >> 16) & 0xff);
g += f * ((rgb >> 8) & 0xff);
b += f * (rgb & 0xff);
}
}
}
int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
int ir = PixelUtils.clamp((int)(r+0.5));
int ig = PixelUtils.clamp((int)(g+0.5));
int ib = PixelUtils.clamp((int)(b+0.5));
outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
}
}
}
/**
* Convolve with a kernel consisting of one row.
* @param kernel the kernel
* @param inPixels the input pixels
* @param outPixels the output pixels
* @param width the width
* @param height the height
* @param alpha include alpha channel
* @param edgeAction what to do at the edges
*/
public static void convolveH(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
int index = 0;
float[] matrix = kernel.getKernelData( null );
int cols = kernel.getWidth();
int cols2 = cols/2;
for (int y = 0; y < height; y++) {
int ioffset = y*width;
for (int x = 0; x < width; x++) {
float r = 0, g = 0, b = 0, a = 0;
int moffset = cols2;
for (int col = -cols2; col <= cols2; col++) {
float f = matrix[moffset+col];
if (f != 0) {
int ix = x+col;
if ( ix < 0 ) {
if ( edgeAction == CLAMP_EDGES )
ix = 0;
else if ( edgeAction == WRAP_EDGES )
ix = (x+width) % width;
} else if ( ix >= width) {
if ( edgeAction == CLAMP_EDGES )
ix = width-1;
else if ( edgeAction == WRAP_EDGES )
ix = (x+width) % width;
}
int rgb = inPixels[ioffset+ix];
a += f * ((rgb >> 24) & 0xff);
r += f * ((rgb >> 16) & 0xff);
g += f * ((rgb >> 8) & 0xff);
b += f * (rgb & 0xff);
}
}
int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
int ir = PixelUtils.clamp((int)(r+0.5));
int ig = PixelUtils.clamp((int)(g+0.5));
int ib = PixelUtils.clamp((int)(b+0.5));
outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
}
}
}
/**
* Convolve with a kernel consisting of one column.
* @param kernel the kernel
* @param inPixels the input pixels
* @param outPixels the output pixels
* @param width the width
* @param height the height
* @param alpha include alpha channel
* @param edgeAction what to do at the edges
*/
public static void convolveV(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
int index = 0;
float[] matrix = kernel.getKernelData( null );
int rows = kernel.getHeight();
int rows2 = rows/2;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float r = 0, g = 0, b = 0, a = 0;
for (int row = -rows2; row <= rows2; row++) {
int iy = y+row;
int ioffset;
if ( iy < 0 ) {
if ( edgeAction == CLAMP_EDGES )
ioffset = 0;
else if ( edgeAction == WRAP_EDGES )
ioffset = ((y+height) % height)*width;
else
ioffset = iy*width;
} else if ( iy >= height) {
if ( edgeAction == CLAMP_EDGES )
ioffset = (height-1)*width;
else if ( edgeAction == WRAP_EDGES )
ioffset = ((y+height) % height)*width;
else
ioffset = iy*width;
} else
ioffset = iy*width;
float f = matrix[row+rows2];
if (f != 0) {
int rgb = inPixels[ioffset+x];
a += f * ((rgb >> 24) & 0xff);
r += f * ((rgb >> 16) & 0xff);
g += f * ((rgb >> 8) & 0xff);
b += f * (rgb & 0xff);
}
}
int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
int ir = PixelUtils.clamp((int)(r+0.5));
int ig = PixelUtils.clamp((int)(g+0.5));
int ib = PixelUtils.clamp((int)(b+0.5));
outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
}
}
}
public String toString() {
return "Blur/Convolve...";
}
}
d) very very confident of getting some sort of solution