Edit: I included an example of k value. Also to be clear I produce three separate arrays from an RGB image . I also include code for loading the image
public static final int[][] SHARPEN = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
Load image
BufferedImage inputImage = ImageIO.read(new File("bridge-rgb.png")); // load the image from this current folder
When I convolute an image in java using 3*3 kernel, the resultant image produced has some of the properties that you would expect from the given kernel but is extremely dark, black being the dominant colour. If I process the image with an identity kernel then identity is returned so I guess that means that Ive selected the correct setting for creating a bufferedImage and hence the problem must be with my convolution algorithm, however I did test the convolution algorithm with a test array and it does seem to be producing accurate output. I wonder could any one make any comment on what I have or point me in the right direction?
for (int j = 0; j < kernelWidth; ++j) {
try {
output+=(input[y-1][x-1+j] * k[0][j]);
counter++;
}catch(Exception e) {
continue;
}
}
for (int j = 0; j < kernelWidth;++j) {
try {
output+=(input[y][x-1+j] * k[1][j]);
counter1++;
}catch(Exception e) {
continue;
}
}
for (int j = 0; j < kernelWidth;++j) {
try {
output+=(input[y+1][x-1+j] * k[2][j]);
counter2++;
}catch(Exception e) {
continue;
}
}
if((output>>bitshiftValue)>255) {
return ((255& 0xff)<<bitshiftValue);
}
else if ((output>>bitshiftValue)<0) {
return 0;
}else {
return output;
} }
I got the arrays to be convoluted with the following method
private static int[][] convertTo2DWithoutUsingGetRGBgreen(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
int[][] result = new int[height][width];
final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel + 3 < pixels.length; pixel += pixelLength) {
result[row][col] = ((int) ((pixels[pixel + 2] & 0xff) )<<8);
col++;
if (col == width) {
col = 0;
row++;
}
}
return result;
}
and post convolution I pimply added them together like so
int[][] finalConv = new int[convRedArray.length][convRedArray[0].length];
for(int c =0; c<convRedArray.length;c++) {
for(int p =0;p<convRedArray[0].length;p++) {
finalConv[c][p]=(convBlueArray[c][p])+(convGreenArray[c[p])+(convRedArray[c][p]);