Reputation: 1280
I've been making a convolution filter in Java, and I mean it's seemed pretty simple. I understand the theory behind it, except for one thing.
Here's my sharpening filter:
0 -1 0
-1 5 -1
0 -1 0
It seems simple enough. However I've been having issues with it. It's terribly easy for the result to go beyond bounds (above 255 and below 0).
For instance, if I have an area of pixels with these values (for simplicity let's say it's a single-channel image):
255 255 255
255 255 100
255 255 255
This will result in (5*255) - 255 - 255 - 255 - 100 = 410. That's too high.
Then, consider this:
255 255 255
255 100 255
255 255 255
This is (5*100) - 255 - 255 - 255 - 255 = -520. That's too low.
If I try and restrict the value to an acceptable range like this:
result = Math.min(Math.max(result, 0), 255);
It turns out horrid and not at all what I want.
I'm at a loss with what to do about it, and I'm not finding any help online. Pretty much every resource about convolution that I'm finding either has unhelpfully loose and vague explanations of normalisation, or doesn't mention it at all.
If you want my code you can ask for it, but I doubt that it has anything to do with it. My code functions fine, it just seems that I'm misunderstanding the theory or something?
Upvotes: 0
Views: 473
Reputation: 1280
Welp, turns out it was a design mistake on my part. I wasn't writing the resulting pixels to a new image, but to the source image. In that case then restricting using Math.min()
and Math.max()
is an acceptable solution.
Upvotes: 0