0

I am trying to understand how to process images manually. This is a school project, so I won't be able to use ConvolveOp. I am trying to manually do what it does.

From what I have read, the sharpen kernel is this:

-1 -1 -1  
-1  9 -1  
-1 -1 -1  

This would mean that the value of the new pixel would be (9*pixel - sum_of_neighbors)/9. Please correct me if i'm wrong. However I am having problems with this approach. I think it is because of the way in which I get the value of the pixel.

I have tried changing the "kernel" used. But I either obtain a mess of pixels or a red pixels over my original image.

I am clearly doing something terribly wrong. It could be that I am not supposed to use .getRGB or that I do not fully understand how this type of image processing works.

Update :

Thank you for the pointers Steffen. Following your recommendations I created a 3 dimensional array. I used the 3'rd dimension to keep track of rgb values

Thank you for pointing out that mistake.

robertpas
  • 643
  • 5
  • 12
  • 25
  • 1
    You must not do this in-place! You must not read the values from b and write them into b. Think about it. This can not work. Also the division by 18 should be only for the edge kernel. -> `)/18);` And the negative values are missing: should be `8*b[...] - b[..] -b[...]`. – Steffen Jun 09 '15 at 13:34
  • @Steffen Ok, so the value of b[i][j][x] should be (b[i][j][x] + (8*b[i][j][x] - neighbors)/some_value) – robertpas Jun 09 '15 at 14:12
  • @Steffen With this modification some pixels are off (will post update). As for b, I don't really understand what you mean. I am using "b" to keep track of .getRGB and the values of red, green, blue. Once I make the modifications to the collors, I write them to a. "a" is the main pixel array. – robertpas Jun 09 '15 at 14:21
  • 1
    You modify b[1][1][2] then you move on to modify b[1][2][2]. To modify this value, you need to access b[1][2-1][2] which is basically the value you have modified earlier. This should not happen. You need the original color value. – Steffen Jun 09 '15 at 16:55
  • Understood. Basically I need some intermediary variables in order to not modify my array, such as : int red = b[1][1][2] + ( 8 * b[1][1][2] - neighbors)/value – robertpas Jun 09 '15 at 17:15

1 Answers1

3

You should not multiply the RGB values directly. Check this answer: Understanding BufferedImage.getRGB output values

You can decompose the image into 3 arrays (r,g,b), do the convoloution and then build a 3 channel image from the single channels.

Your sharpening kernel is a addition of an edge filter and the identity filter:

Identity is:

[ 0,0,0;
  0,1,0;
  0,0,0];

The edge filter is:

[ -1,-1,-1;
  -1, 8,-1;
  -1,-1,-1];

If you want to scale the strenth of the sharpening, then you can scale the edge filter by dividing by some value. But only the edge filter. Not the identity. So, your assumption "(9*pixel - sum_of_neighbors)/9" is not correct. It would be "(1*pixel +(8*pixel+sum_of_neigbors)/some_value)". some_value can be 9, but also any other value.

Community
  • 1
  • 1
Steffen
  • 2,381
  • 4
  • 20
  • 33
  • Thank you for the reply. I will look over the answer that you provided. I hope that it will clarify my questions/confusions. – robertpas Jun 09 '15 at 08:54