fixed point math sharpening filter
Does anyone have a nice fixed point math 1 channel sharpening filter? Should be done in C using fixed point math.
possible declaration:
void sharpen( uint8_t *src, uint8_t *dest, int srcdstpitch, int height );
Edited: "Best algorithm realization gets 300 rep bounty". Seems like it's not possible to have a bounty on your own question oO. Nevertheless - I will 开发者_StackOverflow社区carefully go through any of winners answers and up 30 answers :).
OK, here is my try. Very simple linear filter that only looks at the nearest neighbors, but it works:
Edit: Changed the code to handle the edges of the image separately, for performance purposes. Made the strength of the sharpening a parameter to the function.
/* Simple Laplacian sharpening. */
void sharpen(uint8_t *src, uint8_t *dest, int width, int height, int strength)
{
int i, j;
int here, north, south, west, east;
int sharpening;
static const int scale = 1024;
/* Handle interior pixels. */
for (i = 1; i < height-1; i++) for (j = 1; j < width-1; j++) {
/* This pixel and it's neighbors. */
here = src[width*i+j];
north = src[width*(i-1)+j];
south = src[width*(i+1)+j];
west = src[width*i+(j-1)];
east = src[width*i+(j+1)];
/* Filter. */
sharpening = 4 * here - (north + south + west + east);
here += strength * sharpening / scale;
/* Store clipped result. */
dest[width*i+j] = here<0 ? 0 : here>255 ? 255 : here;
}
/* Optimization: handle edges separately. */
for (i = 0; i < height; i++) {
int j_step = (i==0 || i==height-1) ? 1 : width-1;
for (j = 0; j < width; j += j_step) {
/* Expand the image by symmetry. */
north = i==0 ? src[width*(1)+j] : src[width*(i-1)+j];
south = i==height-1 ? src[width*(height-2)+j] : src[width*(i+1)+j];
west = j==0 ? src[width*i+(1)] : src[width*i+(j-1)];
east = j==width-1 ? src[width*i+(width-2)] : src[width*i+(j+1)];
/* Same as the code for the interior. */
here = src[width*i+j];
sharpening = 4 * here - (north + south + west + east);
here += strength * sharpening / scale;
dest[width*i+j] = here<0 ? 0 : here>255 ? 255 : here;
}
}
}
I tried it with a PGM image. You can tune the strength of the sharpening with the last parameter. A strength of 100 is a good starting point.
You have to extract the code from this gimp plug-in; doesn't seem that hard.
精彩评论