IT Community - Software Programming, Web Development and Technical Support

Unsharp Mask C#.NET

This is a discussion on Unsharp Mask C#.NET within the C# Programming forums, part of the Software Development category; private unsafe Bitmap UnsharpMask(Bitmap img, double amount, int radius, int threshold) { if (amount > 500) amount = 500; amount = amount * ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 01-25-2008, 03:28 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Unsharp Mask C#.NET

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 < radius; i++)
{

ConvMatrix con = new ConvMatrix();
imgBlur = (Bitmap)img.Clone();
BitmapFilter.Conv3x3(imgBlur, con);
}

BitmapData imgdata1 = img.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData imgdata2 = imgBlur.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

byte* p1 = (byte*)(void*)imgdata1.Scan0;

byte* p2 = (byte*)(void*)imgdata2.Scan0;

if (threshold > 0)
{


for (int x = 0; x < w - 1; x++)
{
for (int y = 0; y < h; y++)
{

int imgRValue = p1[2];
int imgGValue = p1[1];
int imgBValue = p1[0];

int imgBlurRValue = p2[2];
int imgBlurGValue = p2[1];
int imgBlurBValue = p2[0];


int rNew = (Math.Abs(imgRValue - imgBlurRValue) >= (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 < w; x++)
{
for (int y = 0; y < h; y++)
{
System.Drawing.Color rgb = img.GetPixel(x, y);
int imgRValue = Convert.ToInt32(rgb.R);
int imgGValue = Convert.ToInt32(rgb.G);
int imgBValue = Convert.ToInt32(rgb.B);

System.Drawing.Color imgBlurRGB = imgBlur.GetPixel(x, y);
int imgBlurRValue = Convert.ToInt32(imgBlurRGB.R);
int imgBlurGValue = Convert.ToInt32(imgBlurRGB.G);
int imgBlurBValue = Convert.ToInt32(imgBlurRGB.B);

int rNew = ((int)amount * (imgRValue - imgBlurRValue)) + imgRValue;

if (rNew > 255)
rNew = 255;
else if (rNew < 0)
rNew = 0;

int gNew = ((int)amount * (imgGValue - imgBlurGValue)) + imgGValue;

if (gNew > 255)
gNew = 255;
else if (gNew < 0)
gNew = 0;

int bNew = ((int)amount * (imgBValue - imgBlurBValue)) + imgBValue;
if (bNew > 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;
}



this one is not perfect...radius..but its working fine
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-25-2008, 03:54 AM
$enthil $enthil is offline
D-Web Sr.Programmer
 
Join Date: Apr 2007
Posts: 162
$enthil is on a distinguished road
Smile Re: Unsharp Mask C#.NET

Can you give me the code for ConvMatrix class and Conv3x3() function?
__________________
$enthil
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-25-2008, 04:15 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: Unsharp Mask C#.NET

I forgot this method...

Quote:
public class ConvMatrix
{
public int TopLeft = 1, TopMid = 2, TopRight = 1;
public int MidLeft = 2, Pixel = 4, MidRight = 2;
public int BottomLeft = 1, BottomMid = 2, BottomRight = 1;
public int Factor = 1;
public int Offset = 0;
public void SetAll(int nVal)
{
TopLeft = TopMid = TopRight = MidLeft = Pixel = MidRight = BottomLeft = BottomMid = BottomRight = nVal;

}

}


Quote:
Quote:
public static bool Conv3x3(Bitmap b, ConvMatrix m)
{
// Avoid divide by zero errors
if (0 == m.Factor) return false;

Bitmap bSrc = (Bitmap)b.Clone();

// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;
int stride2 = stride * 2;
System.IntPtr Scan0 = bmData.Scan0;
System.IntPtr SrcScan0 = bmSrc.Scan0;

unsafe
{
byte* p = (byte*)(void*)Scan0;
byte* pSrc = (byte*)(void*)SrcScan0;

int nOffset = stride - b.Width * 3;
int nWidth = b.Width - 2;
int nHeight = b.Height - 2;

int nPixel;

for (int y = 0; y < nHeight; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
nPixel = ((((pSrc[2] * m.TopLeft) + (pSrc[5] * m.TopMid) + (pSrc[8] * m.TopRight) +
(pSrc[2 + stride] * m.MidLeft) + (pSrc[5 + stride] * m.Pixel) + (pSrc[8 + stride] * m.MidRight) +
(pSrc[2 + stride2] * m.BottomLeft) + (pSrc[5 + stride2] * m.BottomMid) + (pSrc[8 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;

p[5 + stride] = (byte)nPixel;

nPixel = ((((pSrc[1] * m.TopLeft) + (pSrc[4] * m.TopMid) + (pSrc[7] * m.TopRight) +
(pSrc[1 + stride] * m.MidLeft) + (pSrc[4 + stride] * m.Pixel) + (pSrc[7 + stride] * m.MidRight) +
(pSrc[1 + stride2] * m.BottomLeft) + (pSrc[4 + stride2] * m.BottomMid) + (pSrc[7 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;

p[4 + stride] = (byte)nPixel;

nPixel = ((((pSrc[0] * m.TopLeft) + (pSrc[3] * m.TopMid) + (pSrc[6] * m.TopRight) +
(pSrc[0 + stride] * m.MidLeft) + (pSrc[3 + stride] * m.Pixel) + (pSrc[6 + stride] * m.MidRight) +
(pSrc[0 + stride2] * m.BottomLeft) + (pSrc[3 + stride2] * m.BottomMid) + (pSrc[6 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;

p[3 + stride] = (byte)nPixel;

p += 3;
pSrc += 3;
}
p += nOffset;
pSrc += nOffset;
}
}

b.UnlockBits(bmData);
bSrc.UnlockBits(bmSrc);

return true;
}
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)

Last edited by kingmaker : 01-25-2008 at 05:05 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-25-2008, 04:23 AM
it.wily it.wily is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
it.wily is on a distinguished road
Smile Re: Unsharp Mask C#.NET

Hi kingmaker...

what does the Conv3x3 function do?

and also the class ConvMatrix?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-25-2008, 04:31 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: Unsharp Mask C#.NET

Convolution is a mathematical way of combining two signals to form a third signal

Check the attached images...
Attached Files
File Type: zip Desktop.zip (52.3 KB, 1 views)
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mask For TextField seesamjagan Flash Actionscript Programming 2 08-09-2007 10:49 PM
text box under mask nssukumar Flash Actionscript Programming 0 03-06-2007 05:01 AM


All times are GMT -7. The time now is 08:38 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0