Friday, January 25, 2008

Convolution C#.NET

Convolution matrix

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;
}

Ruler in C#.NET

Here i have posted Ruler user control

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RulerTest
{
public partial class Ruler : UserControl
{
private int _width;
private int _height;

public int RulerWidth
{
get { return _width; }
set { _width = value; }
}
public int RulerHeight
{
get { return _height; }
set { _height = value; }
}

public Ruler()
{
InitializeComponent();
}


private void DrawRuler(Graphics g, int formWidth, int formHeight)
{
// Border
//g.DrawRectangle(Pens.Black, 0, 0, formWidth - 1, formHeight - 1);


// Width
//g.DrawString(formWidth + " pixels", Font, Brushes.Black, 10, (formHeight / 2) - (Font.Height / 2));

// Ticks
for (int i = 0; i < formWidth; i++)
{
if (i % 2 == 0)
{
int tickHeight;
if (i % 100 == 0)
{
tickHeight = 15;
DrawTickLabel(g, i.ToString(), i, formHeight, tickHeight);
DrawTick(g, i, formHeight, tickHeight);
}
else if (i % 10 == 0)
{
tickHeight = 10;
DrawTick(g, i, formHeight, tickHeight);
}
else
{
tickHeight = 5;
}


}
}
}

private static void DrawTick(Graphics g, int xPos, int formHeight, int tickHeight)
{

// Top
g.DrawLine(Pens.White, xPos + 50, 40, xPos + 50, -tickHeight + 40);

// Left
g.DrawLine(Pens.White, 40, xPos + 50, -tickHeight + 40, xPos + 50);

}


private void DrawTickLabel(Graphics g, string text, int xPos, int formHeight, int height)
{
// Top
g.DrawString(text, Font, Brushes.White, xPos + 45, -height + 20);

// Left
g.DrawString(text, Font, Brushes.White, -height+20, xPos+45);
}

private void Ruler_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
int height = Width;
int width = Height;

DrawRuler(graphics, width, height);



}


}
}

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;
}