Friday, January 25, 2008

Unsharp Mask in C#.NET

UnSharp Mask filter (called USM)

Resolution might add detail, but it can't add sharpness, they're different things. Resolution adds the detail that lets us recognize features. Sharpness makes edges clear and distinct. The standard tool of choice for sharpening is the UnSharp Mask filter (called USM). It's magic! Most good programs have a USM. Scanners often include a USM tool, but normally the USM sharpening is done after the scan, as the final operation in an image program that saves the image file.

Example:





I feel this one will do unsharp mask but its not doing accurate...(

private unsafe Bitmap UnsharpMask(Bitmap img, double amount, int radius, int threshold)
{

if (amount > 500) amount = 500;
amount = amount * 2;
if (radius > 50) radius = 50;
radius = radius * 2;
if (threshold > 255) threshold = 255;

if (radius == 0)
{
img.Dispose();
return img;
}
int w = img.Width; int h = img.Height;
Bitmap imgCanvas = new Bitmap(w, h);
Bitmap imgBlur = new Bitmap(w, h);

for (int i = 0; i < con =" new" imgblur =" (Bitmap)img.Clone();" imgdata1 =" img.LockBits(new" imgdata2 =" imgBlur.LockBits(new" p1 =" (byte*)(void*)imgdata1.Scan0;" p2 =" (byte*)(void*)imgdata2.Scan0;"> 0)
{


for (int x = 0; x < y =" 0;" imgrvalue =" p1[2];" imggvalue =" p1[1];" imgbvalue =" p1[0];" imgblurrvalue =" p2[2];" imgblurgvalue =" p2[1];" imgblurbvalue =" p2[0];" rnew =" (Math.Abs(imgRValue">= (int)threshold)
? Math.Max(0, Math.Min(255, ((int)amount * (imgRValue - imgBlurRValue)) + imgRValue)) : imgRValue;
int gNew = (Math.Abs(imgGValue - imgBlurGValue) >= (int)threshold)
? Math.Max(0, Math.Min(255, ((int)amount * (imgGValue - imgBlurGValue)) + imgGValue)) : imgGValue;
int bNew = (Math.Abs(imgBValue - imgBlurBValue) >= (int)threshold)
? Math.Max(0, Math.Min(255, ((int)amount * (imgBValue - imgBlurBValue)) + imgBValue)) : imgBValue;

if ((imgRValue != rNew) || (imgGValue != gNew) || (imgBValue != bNew))
{
p1[0] = (byte)bNew;
p1[1] = (byte)gNew;
p1[2] = (byte)rNew;

}
p1 = p1 + 3;
p2 = p2 + 3;


}

}

}
else
{
for (int x = 0; x < y =" 0;" rgb =" img.GetPixel(x," imgrvalue =" Convert.ToInt32(rgb.R);" imggvalue =" Convert.ToInt32(rgb.G);" imgbvalue =" Convert.ToInt32(rgb.B);" imgblurrgb =" imgBlur.GetPixel(x," imgblurrvalue =" Convert.ToInt32(imgBlurRGB.R);" imgblurgvalue =" Convert.ToInt32(imgBlurRGB.G);" imgblurbvalue =" Convert.ToInt32(imgBlurRGB.B);" rnew =" ((int)amount"> 255)
rNew = 255;
else if (rNew < rnew =" 0;" gnew =" ((int)amount"> 255)
gNew = 255;
else if (gNew < gnew =" 0;" bnew =" ((int)amount"> 255)
bNew = 255;
else if (bNew < 0)
bNew = 0;

img.SetPixel(x, y, System.Drawing.Color.FromArgb(rNew, gNew, bNew));

}
}

}

img.UnlockBits(imgdata1);
imgBlur.UnlockBits(imgdata2);

return img;
}

3 comments:

Fredrik said...

I've made a version that only uses safe code (a bit slower, but will work on web hosts with medium trust).

Here's a tutorial on
how to implement an unsharp filter in C#

Unknown said...
This comment has been removed by the author.
Unknown said...

Unsharp mask in WHAT FUCKING LANGUAGE? Right now I'm trying to figure out if you just copied this shit from this site or if you wrote it yourself... there's a few differences, but many similarities as well... you've got FOUR FUCKING EXTRA CLOSING BRACES in your code though, so there NO POSSIBLE WAY you tested this... Additionally, you've got a stack of else if's without an opening if, and a HOST of other errors in there... According to the timestamps on the posts, YOU'RE the original retard that posted code that doesn't work, hasn't been tested, and has more errors than I would make writing a program just by banging my forehead on the keyboard.