Edge Detection with Lockbits C# - c#

I made a program that implements an edge detection algorithm,
but it takes a long time to process.
I've read about using lockbits, and unsafe state instead of getpixel and setpixel, but I still don't understand how to use it.
This is my example code:
private Bitmap SobelEdgeDetect(Bitmap original)
{
Bitmap b = original;
Bitmap bb = original;
int width = b.Width;
int height = b.Height;
int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };
int[,] allPixR = new int[width, height];
int[,] allPixG = new int[width, height];
int[,] allPixB = new int[width, height];
int limit = 128 * 128;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
allPixR[i, j] = b.GetPixel(i, j).R;
allPixG[i, j] = b.GetPixel(i, j).G;
allPixB[i, j] = b.GetPixel(i, j).B;
}
}
int new_rx = 0, new_ry = 0;
int new_gx = 0, new_gy = 0;
int new_bx = 0, new_by = 0;
int rc, gc, bc;
for (int i = 1; i < b.Width - 1; i++)
{
for (int j = 1; j < b.Height - 1; j++)
{
new_rx = 0;
new_ry = 0;
new_gx = 0;
new_gy = 0;
new_bx = 0;
new_by = 0;
rc = 0;
gc = 0;
bc = 0;
for (int wi = -1; wi < 2; wi++)
{
for (int hw = -1; hw < 2; hw++)
{
rc = allPixR[i + hw, j + wi];
new_rx += gx[wi + 1, hw + 1] * rc;
new_ry += gy[wi + 1, hw + 1] * rc;
gc = allPixG[i + hw, j + wi];
new_gx += gx[wi + 1, hw + 1] * gc;
new_gy += gy[wi + 1, hw + 1] * gc;
bc = allPixB[i + hw, j + wi];
new_bx += gx[wi + 1, hw + 1] * bc;
new_by += gy[wi + 1, hw + 1] * bc;
}
}
if (new_rx * new_rx + new_ry * new_ry > limit || new_gx * new_gx + new_gy * new_gy > limit || new_bx * new_bx + new_by * new_by > limit)
bb.SetPixel(i, j, Color.Black);
else
bb.SetPixel(i, j, Color.Transparent);
}
}
return bb;
}
I am using the fastbitmap class, which I implement like this:
private Bitmap SobelEdgeDetectTwo(Bitmap original)
{
int width = original.Width;
int height = original.Height;
Bitmap result = new Bitmap(width,height);
FastBitmap b = new FastBitmap(original);
FastBitmap bb = new FastBitmap(result);
b.LockBitmap();
bb.LockBitmap();
int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };
int[,] allPixR = new int[width, height];
int[,] allPixG = new int[width, height];
int[,] allPixB = new int[width, height];
int limit = 128 * 128;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
var pixel = b.GetPixel(i,j);
allPixR[i, j] = pixel.Red;
allPixG[i, j] = pixel.Green;
allPixB[i, j] = pixel.Blue;
}
}
int new_rx = 0, new_ry = 0;
int new_gx = 0, new_gy = 0;
int new_bx = 0, new_by = 0;
int rc, gc, bc;
for (int i = 1; i < width - 1; i++)
{
for (int j = 1; j < height - 1; j++)
{
new_rx = 0;
new_ry = 0;
new_gx = 0;
new_gy = 0;
new_bx = 0;
new_by = 0;
rc = 0;
gc = 0;
bc = 0;
for (int wi = -1; wi < 2; wi++)
{
for (int hw = -1; hw < 2; hw++)
{
rc = allPixR[i + hw, j + wi];
new_rx += gx[wi + 1, hw + 1] * rc;
new_ry += gy[wi + 1, hw + 1] * rc;
gc = allPixG[i + hw, j + wi];
new_gx += gx[wi + 1, hw + 1] * gc;
new_gy += gy[wi + 1, hw + 1] * gc;
bc = allPixB[i + hw, j + wi];
new_bx += gx[wi + 1, hw + 1] * bc;
new_by += gy[wi + 1, hw + 1] * bc;
}
}
if (new_rx * new_rx + new_ry * new_ry > limit || new_gx * new_gx + new_gy * new_gy > limit || new_bx * new_bx + new_by * new_by > limit)
{
PixelData p = new PixelData(Color.Black);
bb.SetPixel(i, j, p);
}
else
{
PixelData p = new PixelData(Color.Transparent);
bb.SetPixel(i, j, p);
}
}
}
b.UnlockBitmap();
bb.UnlockBitmap();
return result;
}
However, the image doesn't change at all.
Could you give me advice about which part of my code is wrong?

It's easiest to use a class like FastBitmap. Just add a FastBitmap and use GetPixel() on that class instead of on your Bitmap, the rest can be the same.
Something like this:
Bitmap dstBmp = new Bitmap(width, height, original.PixelFormat);
FastBitmap fastBitmap = new FastBitmap(dstBmp);
fastBitmap.LockBitmap();
//...
var pixel = fastBitmap.GetPixel(x,y);
//...
fastBitmap.UnlockBitmap();

Ok, let's see what we can do - a quick Google found this, which can be simply adapted to your function something like this
private Bitmap SobelEdgeDetect(Bitmap original)
{
int width = original.Width;
int height = original.Height;
int BitsPerPixel = Image.GetPixelFormatSize(original.PixelFormat);
int OneColorBits = BitsPerPixel / 8;
BitmapData bmpData = original.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, original.PixelFormat);
int position;
int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };
byte Threshold = 128;
Bitmap dstBmp = new Bitmap(width, height, original.PixelFormat);
BitmapData dstData = dstBmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, dstBmp.PixelFormat);
unsafe
{
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
byte* dst = (byte*)dstData.Scan0.ToPointer();
for (int i = 1; i < height - 1; i++)
{
for (int j = 1; j < width - 1; j++)
{
int NewX = 0, NewY = 0;
for (int ii = 0; ii < 3; ii++)
{
for (int jj = 0; jj < 3; jj++)
{
int I = i + ii - 1;
int J = j + jj - 1;
byte Current = *(ptr + (I * width + J) * OneColorBits);
NewX += gx[ii, jj] * Current;
NewY += gy[ii, jj] * Current;
}
}
position = ((i * width + j) * OneColorBits);
if (NewX * NewX + NewY * NewY > Threshold * Threshold)
dst[position] = dst[position + 1] = dst[position + 2] = 255;
else
dst[position] = dst[position + 1] = dst[position + 2] = 0;
}
}
}
original.UnlockBits(bmpData);
dstBmp.UnlockBits(dstData);
return dstBmp;
}
It's not complete copy/paste solution but you should be able to see how the original author is accessing the pixel data by using LockBits in exactly the way you need. The rest is up to you ;-)
You will need to set the unsafe option in your project properties as I explained in my answer to your previous question.

Related

How do I find a image on the screen and get the mouse coordinates?

I want to find the image on the screen and get the x,y coordinates if it matched on the screen. I already know how to move the mouse and click using this x,y coordinates.
EG:
I want to give icon image and the code will get a screenshot of desktop and find the image, move mouse.
The following code works, but if I change the resolution of the screen I have to get the image (bmpMatch) again.
private static Rectangle FindImageOnScreen(Bitmap bmpMatch, bool ExactMatch)
{
Rectangle rct = Rectangle.Empty;
try
{
Bitmap ScreenBmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(ScreenBmp);
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
ScreenBmp.Size,
CopyPixelOperation.SourceCopy);
BitmapData ImgBmd = bmpMatch.LockBits(new Rectangle(0, 0, bmpMatch.Width, bmpMatch.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData ScreenBmd = ScreenBmp.LockBits(new Rectangle(0, 0, ScreenBmp.Width, ScreenBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
byte[] ImgByts = new byte[(Math.Abs(ImgBmd.Stride) * bmpMatch.Height) - 1 + 1];
byte[] ScreenByts = new byte[(Math.Abs(ScreenBmd.Stride) * ScreenBmp.Height) - 1 + 1];
Marshal.Copy(ImgBmd.Scan0, ImgByts, 0, ImgByts.Length);
Marshal.Copy(ScreenBmd.Scan0, ScreenByts, 0, ScreenByts.Length);
bool FoundMatch = false;
int sindx, iindx;
int spc, ipc;
int skpx = System.Convert.ToInt32((bmpMatch.Width - 1) / (double)10);
if (skpx < 1 | ExactMatch)
skpx = 1;
int skpy = System.Convert.ToInt32((bmpMatch.Height - 1) / (double)10);
if (skpy < 1 | ExactMatch)
skpy = 1;
for (int si = 0; si <= ScreenByts.Length - 1; si += 3)
{
FoundMatch = true;
for (int iy = 0; iy <= ImgBmd.Height - 1; iy += skpy)
{
for (int ix = 0; ix <= ImgBmd.Width - 1; ix += skpx)
{
sindx = (iy * ScreenBmd.Stride) + (ix * 3) + si;
iindx = (iy * ImgBmd.Stride) + (ix * 3);
spc = Color.FromArgb(ScreenByts[sindx + 2], ScreenByts[sindx + 1], ScreenByts[sindx]).ToArgb();
ipc = Color.FromArgb(ImgByts[iindx + 2], ImgByts[iindx + 1], ImgByts[iindx]).ToArgb();
if (spc != ipc)
{
FoundMatch = false;
iy = ImgBmd.Height - 1;
ix = ImgBmd.Width - 1;
}
}
}
if (FoundMatch)
{
double r = si / (double)(ScreenBmp.Width * 3);
double c = ScreenBmp.Width * (r % 1);
if (r % 1 >= 0.5)
r -= 1;
rct.X = System.Convert.ToInt32(c);
rct.Y = System.Convert.ToInt32(r);
rct.Width = bmpMatch.Width;
rct.Height = bmpMatch.Height;
break;
}
}
bmpMatch.UnlockBits(ImgBmd);
ScreenBmp.UnlockBits(ScreenBmd);
//ScreenBmp.Dispose();
return rct;
} catch(Exception ex)
{
Console.Write(ex.Message);
}
return rct;
}
is there anyway I can give any size of image, and get it search from the desktop screenshot.
I was looking for exactly this, thank you very much! Regarding your question, although I don't understand the code well, it worked for me to change the increment in the for, instead of incrementing from 3 to 3, I put it from 1 to 1, it worked for me to find the coordinates in different resolution with the same image
private static Rectangle FindImageOnScreen(Bitmap bmpMatch, bool ExactMatch)
{
Rectangle rct = Rectangle.Empty;
try
{
Bitmap ScreenBmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(ScreenBmp);
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
ScreenBmp.Size,
CopyPixelOperation.SourceCopy);
BitmapData ImgBmd = bmpMatch.LockBits(new Rectangle(0, 0, bmpMatch.Width, bmpMatch.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData ScreenBmd = ScreenBmp.LockBits(new Rectangle(0, 0, ScreenBmp.Width, ScreenBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
byte[] ImgByts = new byte[(Math.Abs(ImgBmd.Stride) * bmpMatch.Height) - 1 + 1];
byte[] ScreenByts = new byte[(Math.Abs(ScreenBmd.Stride) * ScreenBmp.Height) - 1 + 1];
Marshal.Copy(ImgBmd.Scan0, ImgByts, 0, ImgByts.Length);
Marshal.Copy(ScreenBmd.Scan0, ScreenByts, 0, ScreenByts.Length);
bool FoundMatch = false;
int sindx, iindx;
int spc, ipc;
int skpx = System.Convert.ToInt32((bmpMatch.Width - 1) / (double)10);
if (skpx < 1 | ExactMatch)
skpx = 1;
int skpy = System.Convert.ToInt32((bmpMatch.Height - 1) / (double)10);
if (skpy < 1 | ExactMatch)
skpy = 1;
for (int si = 0; si <= ScreenByts.Length - 1; si ++) //here modify
{
FoundMatch = true;
for (int iy = 0; iy <= ImgBmd.Height - 1; iy += skpy)
{
for (int ix = 0; ix <= ImgBmd.Width - 1; ix += skpx)
{
sindx = (iy * ScreenBmd.Stride) + (ix * 3) + si;
iindx = (iy * ImgBmd.Stride) + (ix * 3);
spc = Color.FromArgb(ScreenByts[sindx + 2], ScreenByts[sindx + 1], ScreenByts[sindx]).ToArgb();
ipc = Color.FromArgb(ImgByts[iindx + 2], ImgByts[iindx + 1], ImgByts[iindx]).ToArgb();
if (spc != ipc)
{
FoundMatch = false;
iy = ImgBmd.Height - 1;
ix = ImgBmd.Width - 1;
}
}
}
if (FoundMatch)
{
double r = si / (double)(ScreenBmp.Width * 3);
double c = ScreenBmp.Width * (r % 1);
if (r % 1 >= 0.5)
r -= 1;
rct.X = System.Convert.ToInt32(c);
rct.Y = System.Convert.ToInt32(r);
rct.Width = bmpMatch.Width;
rct.Height = bmpMatch.Height;
break;
}
}
bmpMatch.UnlockBits(ImgBmd);
ScreenBmp.UnlockBits(ScreenBmd);
//ScreenBmp.Dispose();
return rct;
} catch(Exception ex)
{
Console.Write(ex.Message);
}
return rct;
}

Why does this edge detector not work correctly when bitmap width not divisible by 4?

Using the Sobel edge detector code below I find that the output bitmap has a diagonal line of zero values superimposed over detected edges if the input bitmap has a width not divisible by 4. The red square marked in the output bitmap at co-ords (80,80) is broken up and incorrectly placed in this case. Why is this and how can I make the code work with any bitmap width?
private Bitmap SobelEdgeDetect2(Bitmap original, byte Threshold = 128)
{
// https://stackoverflow.com/questions/16747257/edge-detection-with-lockbits-c-sharp
int width = original.Width;
int height = original.Height;
int BitsPerPixel = Image.GetPixelFormatSize(original.PixelFormat);
int OneColorBits = BitsPerPixel / 8;
BitmapData bmpData = original.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, original.PixelFormat);
int position;
int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };
Bitmap dstBmp = new Bitmap(width, height, original.PixelFormat);
BitmapData dstData = dstBmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, dstBmp.PixelFormat);
int byteCount = dstData.Stride * dstBmp.Height;
byte[] input = new byte[byteCount];
byte[] processed = new byte[byteCount];
IntPtr ptr = bmpData.Scan0;
IntPtr dst = dstData.Scan0;
Marshal.Copy(ptr, input, 0, input.Length);
Marshal.Copy(dst,processed, 0, input.Length);
int BlackPoints = 0;
int WhitePoints = 0;
for (int i = 1; i < height - 1; i++) // y
{
for (int j = 1; j < width - 1; j++) // x
{
int NewX = 0, NewY = 0;
for (int ii = 0; ii < 3; ii++)
{
for (int jj = 0; jj < 3; jj++)
{
int I = i + ii - 1;
int J = j + jj - 1;
byte Current = input[(I * (width) + J) * OneColorBits];
NewX += gx[ii, jj] * Current;
NewY += gy[ii, jj] * Current;
}
}
position = (i * (width) + j) * OneColorBits;
if (NewX * NewX + NewY * NewY > Threshold * Threshold)
{
processed[position] = 255;
processed[position + 1] = 255;
processed[position + 2] = 255;
WhitePoints++;
}
else
{
processed[position] = 0;
processed[position + 1] = 0;
processed[position + 2] = 0;
BlackPoints++;
}
if (j >= 78 && j <= 82 && i >= 78 && i <= 82)
{
processed[position] = 0;
processed[position + 1] = 0;
processed[position + 2] = 255;
}
}
}
Marshal.Copy(processed, 0, dst, input.Length);
dstBmp.UnlockBits(dstData);
return dstBmp;
}
For a 201 pixel wide bitmap, dstData.Stride was 604. For a 200 pixel wide bitmap dstData.Stride was 612, which explains why width had to be divisible by 4 for my code.
Replacing
position = (i * (width) + j) * OneColorBits;
by
position = i * dstData.Stride + j * OneColorBits;
and
byte Current = input[(I * (width) + J) * OneColorBits];
by
byte Current = input[I * dstData.Stride + J * OneColorBits];
fixed the problem.

Image filter too slow

I need a specific rewrite of this code snippet to run it faster..
I can barely get 5fps out off this code and I need 30fps to run it smoothly..
Need help on how to rewrite it to run faster..
I'm using it as a filter on my media player to enhance poor frames in the media, so it look a little better on higher resolutions..
public static Bitmap MedianFilter(this Bitmap sourceBitmap, int matrixSize, int bias = 0, bool grayscale = false)
{
BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height];
Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);
sourceBitmap.UnlockBits(sourceData);
if (grayscale == true)
{
float rgb = 0;
for (int k = 0; k < pixelBuffer.Length; k += 4)
{
rgb = pixelBuffer[k] * 0.11f;
rgb += pixelBuffer[k + 1] * 0.59f;
rgb += pixelBuffer[k + 2] * 0.3f;
pixelBuffer[k] = (byte)rgb;
pixelBuffer[k + 1] = pixelBuffer[k];
pixelBuffer[k + 2] = pixelBuffer[k];
pixelBuffer[k + 3] = 255;
}
}
int filterOffset = (matrixSize - 1) / 2;
int calcOffset = 0;
int byteOffset = 0;
List<int> neighbourPixels = new List<int>();
byte[] middlePixel;
for (int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++)
{
for (int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++)
{
byteOffset = offsetY * sourceData.Stride + offsetX * 4;
neighbourPixels.Clear();
for (int filterY = -filterOffset; filterY <= filterOffset; filterY++)
{
for (int filterX = -filterOffset; filterX <= filterOffset; filterX++)
{
calcOffset = byteOffset + (filterX * 4) + (filterY * sourceData.Stride);
neighbourPixels.Add(BitConverter.ToInt32(pixelBuffer, calcOffset));
}
}
neighbourPixels.Sort();
middlePixel = BitConverter.GetBytes(neighbourPixels[filterOffset]);
resultBuffer[byteOffset] = middlePixel[0];
resultBuffer[byteOffset + 1] = middlePixel[1];
resultBuffer[byteOffset + 2] = middlePixel[2];
resultBuffer[byteOffset + 3] = middlePixel[3];
}
}
Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);
BitmapData resultData = resultBitmap.LockBits(new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(resultBuffer, 0, resultData.Scan0, resultBuffer.Length);
resultBitmap.UnlockBits(resultData);
return resultBitmap;
}

C# Zoom In and Zoom Out On A Image

I want to make a program have zoom in and zoom out function.(2x,4x,8x)But I can't use a available zoom function.I have to write a new one.I have done importing bitmap image.And I can get rgb color for each pixel.I created matrisses colorR,colorG and colorB for r,g and b colors.After that I thought I can create the 2x zoomed image with SolidBrush on a panel.I will draw 2x image like this:
Original Image (For example 3x3 pixels) (p=pixel color and "_" for space)
p1_p2_p3
p4_p5_p6
p7_p8_p9
2x Zoomed Image (6x6 pixels because of orginal image size) (p=pixel color of orginal image and "_" for space)
p1_p1_p2_p2_p3_p3
p1_p1_p2_p2_p3_p3
p4_p4_p5_p5_p6_p6
p4_p4_p5_p5_p6_p6
p7_p7_p8_p8_p9_p9
p7_p7_p8_p8_p9_p9
I wrote one loop but it didn't work because it is complety wrong.So how can I write for loops ?
private void button4_Click(object sender, EventArgs e) {
listBox1.Items.Clear();//insignificant
listBox2.Items.Clear();//insignificant
listBox3.Items.Clear();//insignificant
using (OpenFileDialog dlg = new OpenFileDialog()) {
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK) {
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
}
private void button1_Click(object sender, EventArgs e) {
Graphics my2xImage = panel1.CreateGraphics();
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
int[,] colorR = new int[bmpHeight, bmpWidth];
int[,] colorG = new int[bmpHeight, bmpWidth];
int[,] colorB = new int[bmpHeight, bmpWidth];
for (int y = 0; y < bmpHeight; y++) {
for (int x = 0; x < bmpWidth; x++) {
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
listBox1.Items.Add("(" + (x + 1) + "," + (y + 1) + ")" + " " + colorR[x, y]);//insignificant
listBox2.Items.Add("(" + (x + 1) + "," + (y + 1) + ")" + " " + colorG[x, y]);//insignificant
listBox3.Items.Add("(" + (x + 1) + "," + (y + 1) + ")" + " " + colorB[x, y]);//insignificant
}
}
//for (int y = 0; y < (bmpHeight * 2); y++)
//{
// for (int x = 0; x < (bmpWidth * 2); x++)
// {
// Color mySpecialColor = Color.FromArgb(colorR[x,y], colorG[x,y], colorB[x,y]);
// SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
// my2xImage.FillRectangle(pixelBrush, x, y, 1, 1);
// }
//}
}
private void button5_Click(object sender, EventArgs e) {
}
private void button2_Click(object sender, EventArgs e) {
}
This is insane, but if you really must do it this way, then try something like this:
int dx = x*2;
int dy = y*2;
colorR[dx ,dy ] = pixelColor.R;
colorR[dx+1,dy ] = pixelColor.R;
colorR[dx ,dy+1] = pixelColor.R;
colorR[dx+1,dy+1] = pixelColor.R;
colorG[dx ,dy ] = pixelColor.G;
colorG[dx+1,dy ] = pixelColor.G;
colorG[dx ,dy+1] = pixelColor.G;
colorG[dx+1,dy+1] = pixelColor.G;
colorB[dx ,dy ] = pixelColor.B;
colorB[dx+1,dy ] = pixelColor.B;
colorB[dx ,dy+1] = pixelColor.B;
colorB[dx+1,dy+1] = pixelColor.B;
You should use the DrawImage method of the Graphics class.
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = Graphics.FromImage(bmpFirst);
// Draw image to screen.
g.DrawImage(newImage, destRect, x, y, width, height, units);
Look here: https://msdn.microsoft.com/en-us/library/ms142045(v=vs.110).aspx
Look here also: https://msdn.microsoft.com/en-us/library/k0fsyd4e(v=vs.110).aspx
You can even set the interpolation mode: https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode(v=vs.110).aspx
You are looking for the NearestNeighbor interpolation mode.
He is the solution.
private void button4_Click(object sender, EventArgs e )
{
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel1.CreateGraphics();
int[,] colorR = new int[bmpHeight*2 , bmpWidth*2];
int[,] colorG = new int[bmpHeight*2 , bmpWidth*2];
int[,] colorB = new int[bmpHeight*2 , bmpWidth*2];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 2;
int dy = y * 2;
colorR[dx, dy] = pixelColor.R;
colorR[dx + 1, dy] = pixelColor.R;
colorR[dx, dy + 1] = pixelColor.R;
colorR[dx + 1, dy + 1] = pixelColor.R;
colorG[dx, dy] = pixelColor.G;
colorG[dx + 1, dy] = pixelColor.G;
colorG[dx, dy + 1] = pixelColor.G;
colorG[dx + 1, dy + 1] = pixelColor.G;
colorB[dx, dy] = pixelColor.B;
colorB[dx + 1, dy] = pixelColor.B;
colorB[dx, dy + 1] = pixelColor.B;
colorB[dx + 1, dy + 1] = pixelColor.B;
}
}
for (int y = 0; y < (bmpHeight*2); y++)
{
for (int x = 0; x < (bmpWidth*2); x++)
{
Color mySpecialColor = Color.FromArgb(colorR[x, y], colorG[x, y], colorB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
You can use for loops for color[dx,dy] parts.Here it is for 8x zoom.
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel3.CreateGraphics();
int[,] colorR = new int[bmpHeight * 8, bmpWidth * 8];
int[,] colorG = new int[bmpHeight * 8, bmpWidth * 8];
int[,] colorB = new int[bmpHeight * 8, bmpWidth * 8];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 8;
int dy = y * 8;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
colorR[dx + j, dy + i] = pixelColor.R;
colorG[dx + j, dy + i] = pixelColor.G;
colorB[dx + j, dy + i] = pixelColor.B;
}
}
}
Full code (Also you can download project: Link
private void button4_Click(object sender, EventArgs e )
{
tabControl1.SelectedTab = tabPage1;
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.jpg|*.jpg|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
if (pictureBox1.Image == null)
{
MessageBox.Show("Please choose a image file.");
}
else
{
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
if (bmpHeight > 100 || bmpWidth > 100)
{
MessageBox.Show("Image size can't be bigger than 100x100 pixels");
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = false;
}
else
{
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = false;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage2;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel1.CreateGraphics();
int[,] colorR = new int[bmpWidth * 2 , bmpHeight *2];
int[,] colorG = new int[bmpWidth * 2 , bmpHeight *2];
int[,] colorB = new int[bmpWidth * 2 , bmpHeight *2];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 2;
int dy = y * 2;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
colorR[dx + i, dy + j] = pixelColor.R;
colorG[dx + i, dy + j] = pixelColor.G;
colorB[dx + i, dy + j] = pixelColor.B;
}
}
}
}
for (int y = 0; y < (bmpHeight*2); y++)
{
for (int x = 0; x < (bmpWidth*2); x++)
{
Color mySpecialColor = Color.FromArgb(colorR[x, y], colorG[x, y], colorB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage3;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel2.CreateGraphics();
int[,] colorR = new int[bmpWidth * 4, bmpHeight * 4];
int[,] colorG = new int[bmpWidth * 4, bmpHeight * 4];
int[,] colorB = new int[bmpWidth * 4, bmpHeight * 4];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 4;
int dy = y * 4;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
colorR[dx + i, dy + j] = pixelColor.R;
colorG[dx + i, dy + j] = pixelColor.G;
colorB[dx + i, dy + j] = pixelColor.B;
}
}
}
}
for (int y = 0; y < (bmpHeight * 4); y++)
{
for (int x = 0; x < (bmpWidth * 4); x++)
{
Color mySpecialColor = Color.FromArgb(colorR[x, y], colorG[x, y], colorB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void button5_Click_1(object sender, EventArgs e)
{
}
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage4;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel3.CreateGraphics();
int[,] colorR = new int[bmpWidth * 8, bmpHeight * 8];
int[,] colorG = new int[bmpWidth * 8, bmpHeight * 8];
int[,] colorB = new int[bmpWidth * 8, bmpHeight * 8];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
int dx = x * 8;
int dy = y * 8;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
colorR[dx + i, dy + j] = pixelColor.R;
colorG[dx + i, dy + j] = pixelColor.G;
colorB[dx + i, dy + j] = pixelColor.B;
}
}
}
}
for (int y = 0; y < (bmpHeight * 8); y++)
{
for (int x = 0; x < (bmpWidth * 8); x++)
{
Color mySpecialColor = Color.FromArgb(colorR[x, y], colorG[x, y], colorB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button5_Click_2(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage5;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel4.CreateGraphics();
int[,] colorR = new int[bmpWidth, bmpHeight];
int[,] colorG = new int[bmpWidth, bmpHeight];
int[,] colorB = new int[bmpWidth, bmpHeight];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
}
}
int[,] colorSR = new int[bmpWidth /2, bmpHeight /2];
int[,] colorSG = new int[bmpWidth /2, bmpHeight /2];
int[,] colorSB = new int[bmpWidth /2, bmpHeight /2];
for (int i = 0; i < bmpWidth / 2; i++)
{
for (int j = 0; j < bmpHeight /2; j++)
{
colorSR[i, j] = (colorR[2 * i, 2 * j] + colorR[2 * i, 2 * j + 1] + colorR[2 * i + 1, 2 * j] + colorR[2 * i + 1, 2 * j + 1]) / 4;
colorSG[i, j] = (colorG[2 * i, 2 * j] + colorG[2 * i, 2 * j + 1] + colorG[2 * i + 1, 2 * j] + colorG[2 * i + 1, 2 * j + 1]) / 4;
colorSB[i, j] = (colorB[2 * i, 2 * j] + colorB[2 * i, 2 * j + 1] + colorB[2 * i + 1, 2 * j] + colorB[2 * i + 1, 2 * j + 1]) / 4;
}
}
for (int y = 0; y < (bmpHeight / 2); y++)
{
for (int x = 0; x < (bmpWidth / 2); x++)
{
Color mySpecialColor = Color.FromArgb(colorSR[x, y], colorSG[x, y], colorSB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage6;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel5.CreateGraphics();
int[,] colorR = new int[bmpWidth, bmpHeight];
int[,] colorG = new int[bmpWidth, bmpHeight];
int[,] colorB = new int[bmpWidth, bmpHeight];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
}
}
int[,] colorSR = new int[bmpWidth / 4, bmpHeight / 4];
int[,] colorSG = new int[bmpWidth / 4, bmpHeight / 4];
int[,] colorSB = new int[bmpWidth / 4, bmpHeight / 4];
for (int i = 0; i < bmpWidth / 4; i++)
{
for (int j = 0; j < bmpHeight / 4; j++)
{
colorSR[i, j] = (colorR[4 * i, 4 * j] + colorR[4 * i, 4 * j + 1] + colorR[4 * i + 1, 4 * j] + colorR[4 * i + 1, 4 * j + 1]) / 4;
colorSG[i, j] = (colorG[4 * i, 4 * j] + colorG[4 * i, 4 * j + 1] + colorG[4 * i + 1, 4 * j] + colorG[4 * i + 1, 4 * j + 1]) / 4;
colorSB[i, j] = (colorB[4 * i, 4 * j] + colorB[4 * i, 4 * j + 1] + colorB[4 * i + 1, 4 * j] + colorB[4 * i + 1, 4 * j + 1]) / 4;
}
}
for (int y = 0; y < (bmpHeight / 4); y++)
{
for (int x = 0; x < (bmpWidth / 4); x++)
{
Color mySpecialColor = Color.FromArgb(colorSR[x, y], colorSG[x, y], colorSB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button7_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage7;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel6.CreateGraphics();
int[,] colorR = new int[bmpWidth, bmpHeight];
int[,] colorG = new int[bmpWidth, bmpHeight];
int[,] colorB = new int[bmpWidth, bmpHeight];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
}
}
int[,] colorSR = new int[bmpWidth / 8, bmpHeight / 8];
int[,] colorSG = new int[bmpWidth / 8, bmpHeight / 8];
int[,] colorSB = new int[bmpWidth / 8, bmpHeight / 8];
for (int i = 0; i < bmpWidth / 8; i++)
{
for (int j = 0; j < bmpHeight / 8; j++)
{
colorSR[i, j] = (colorR[8 * i, 8 * j] + colorR[8 * i, 8 * j + 1] + colorR[8 * i + 1, 8 * j] + colorR[8 * i + 1, 8 * j + 1]) / 4;
colorSG[i, j] = (colorG[8 * i, 8 * j] + colorG[8 * i, 8 * j + 1] + colorG[8 * i + 1, 8 * j] + colorG[8 * i + 1, 8 * j + 1]) / 4;
colorSB[i, j] = (colorB[8 * i, 8 * j] + colorB[8 * i, 8 * j + 1] + colorB[8 * i + 1, 8 * j] + colorB[8 * i + 1, 8 * j + 1]) / 4;
}
}
for (int y = 0; y < (bmpHeight / 8); y++)
{
for (int x = 0; x < (bmpWidth / 8); x++)
{
Color mySpecialColor = Color.FromArgb(colorSR[x, y], colorSG[x, y], colorSB[x, y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button8_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.jpg|*.jpg|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
if (pictureBox1.Image == null)
{
MessageBox.Show("Please choose a image file.");
}
else
{
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
if (bmpHeight > 800 || bmpWidth > 800)
{
MessageBox.Show("Image size can't be bigger than 800x800 pixels.");
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = false;
}
else
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button10.Enabled = false;
}
}
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
tabControl1.SelectedTab = tabPage8;
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
Graphics g = panel7.CreateGraphics();
int[,] colorR = new int[bmpWidth, bmpHeight];
int[,] colorG = new int[bmpWidth, bmpHeight];
int[,] colorB = new int[bmpWidth, bmpHeight];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
Color pixelColor = bmpFirst.GetPixel(x, y);
colorR[x, y] = pixelColor.R;
colorG[x, y] = pixelColor.G;
colorB[x, y] = pixelColor.B;
}
}
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
int dx = bmpWidth - 1;
int dy = bmpHeight - 1;
Color mySpecialColor = Color.FromArgb(colorR[dx - x, dy - y], colorG[dx - x, dy - y], colorB[dx - x, dy - y]);
SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
g.FillRectangle(pixelBrush, x, y, 1, 1);
}
}
}
private void button9_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "*.bmp|*.bmp|*.jpg|*.jpg|*.*|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName);
}
}
if (pictureBox1.Image == null)
{
MessageBox.Show("Please choose a image file.");
}
else
{
int bmpHeight = pictureBox1.Image.Height;
int bmpWidth = pictureBox1.Image.Width;
if (bmpHeight > 800 || bmpWidth > 800)
{
MessageBox.Show("Image size can't be bigger than 800x800 pixels");
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = false;
}
else
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button10.Enabled = true;
}
}
}
}

Sharpen on a Bitmap using C#

I want to put a sharpen filter on an image. I have found a web with short tutorial.
I tried to do it in C# so here is my code. Anyway, I tried to find out why it is not working. I do not know if I am doing something wrong, if yes, please advise me what to do to make it work as it should be. Thanks
public static Bitmap sharpen(Bitmap image)
{
Bitmap sharpenImage = new Bitmap(image.Width, image.Height);
int filterWidth = 3;
int filterHeight = 3;
int w = image.Width;
int h = image.Height;
double[,] filter = new double[filterWidth, filterHeight];
filter[0, 0] = filter[0, 1] = filter[0, 2] = filter[1, 0] = filter[1, 2] = filter[2, 0] = filter[2, 1] = filter[2, 2] = -1;
filter[1, 1] = 9;
double factor = 1.0;
double bias = 0.0;
Color[,] result = new Color[image.Width, image.Height];
for (int x = 0; x < w; ++x)
{
for (int y = 0; y < h; ++y)
{
double red = 0.0, green = 0.0, blue = 0.0;
Color imageColor = image.GetPixel(x, y);
for (int filterX = 0; filterX < filterWidth; filterX++)
{
for (int filterY = 0; filterY < filterHeight; filterY++)
{
int imageX = (x - filterWidth / 2 + filterX + w) % w;
int imageY = (y - filterHeight / 2 + filterY + h) % h;
red += imageColor.R * filter[filterX, filterY];
green += imageColor.G * filter[filterX, filterY];
blue += imageColor.B * filter[filterX, filterY];
}
int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);
result[x, y] = Color.FromArgb(r, g, b);
}
}
}
for (int i = 0; i < w; ++i)
{
for (int j = 0; j < h; ++j)
{
sharpenImage.SetPixel(i, j, result[i, j]);
}
}
return sharpenImage;
}
public static Bitmap sharpen(Bitmap image)
{
Bitmap sharpenImage = new Bitmap(image.Width, image.Height);
int filterWidth = 3;
int filterHeight = 3;
int w = image.Width;
int h = image.Height;
double[,] filter = new double[filterWidth, filterHeight];
filter[0, 0] = filter[0, 1] = filter[0, 2] = filter[1, 0] = filter[1, 2] = filter[2, 0] = filter[2, 1] = filter[2, 2] = -1;
filter[1, 1] = 9;
double factor = 1.0;
double bias = 0.0;
Color[,] result = new Color[image.Width, image.Height];
for (int x = 0; x < w; ++x)
{
for (int y = 0; y < h; ++y)
{
double red = 0.0, green = 0.0, blue = 0.0;
//=====[REMOVE LINES]========================================================
// Color must be read per filter entry, not per image pixel.
Color imageColor = image.GetPixel(x, y);
//===========================================================================
for (int filterX = 0; filterX < filterWidth; filterX++)
{
for (int filterY = 0; filterY < filterHeight; filterY++)
{
int imageX = (x - filterWidth / 2 + filterX + w) % w;
int imageY = (y - filterHeight / 2 + filterY + h) % h;
//=====[INSERT LINES]========================================================
// Get the color here - once per fiter entry and image pixel.
Color imageColor = image.GetPixel(imageX, imageY);
//===========================================================================
red += imageColor.R * filter[filterX, filterY];
green += imageColor.G * filter[filterX, filterY];
blue += imageColor.B * filter[filterX, filterY];
}
int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);
result[x, y] = Color.FromArgb(r, g, b);
}
}
}
for (int i = 0; i < w; ++i)
{
for (int j = 0; j < h; ++j)
{
sharpenImage.SetPixel(i, j, result[i, j]);
}
}
return sharpenImage;
}
I took Daniel's answer and modified it for performance, by using BitmapData class, since using GetPixel/SetPixel is very expensive and inappropriate for performance-hungry systems. It works exactly the same as the previous solution and can be used instead.
public static Bitmap Sharpen(Bitmap image)
{
Bitmap sharpenImage = (Bitmap)image.Clone();
int filterWidth = 3;
int filterHeight = 3;
int width = image.Width;
int height = image.Height;
// Create sharpening filter.
double[,] filter = new double[filterWidth, filterHeight];
filter[0, 0] = filter[0, 1] = filter[0, 2] = filter[1, 0] = filter[1, 2] = filter[2, 0] = filter[2, 1] = filter[2, 2] = -1;
filter[1, 1] = 9;
double factor = 1.0;
double bias = 0.0;
Color[,] result = new Color[image.Width, image.Height];
// Lock image bits for read/write.
BitmapData pbits = sharpenImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// Declare an array to hold the bytes of the bitmap.
int bytes = pbits.Stride * height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(pbits.Scan0, rgbValues, 0, bytes);
int rgb;
// Fill the color array with the new sharpened color values.
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
double red = 0.0, green = 0.0, blue = 0.0;
for (int filterX = 0; filterX < filterWidth; filterX++)
{
for (int filterY = 0; filterY < filterHeight; filterY++)
{
int imageX = (x - filterWidth / 2 + filterX + width) % width;
int imageY = (y - filterHeight / 2 + filterY + height) % height;
rgb = imageY * pbits.Stride + 3 * imageX;
red += rgbValues[rgb + 2] * filter[filterX, filterY];
green += rgbValues[rgb + 1] * filter[filterX, filterY];
blue += rgbValues[rgb + 0] * filter[filterX, filterY];
}
int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);
result[x, y] = Color.FromArgb(r, g, b);
}
}
}
// Update the image with the sharpened pixels.
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
rgb = y * pbits.Stride + 3 * x;
rgbValues[rgb + 2] = result[x, y].R;
rgbValues[rgb + 1] = result[x, y].G;
rgbValues[rgb + 0] = result[x, y].B;
}
}
// Copy the RGB values back to the bitmap.
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, pbits.Scan0, bytes);
// Release image bits.
sharpenImage.UnlockBits(pbits);
return sharpenImage;
}
This will create a softer sharpening effect. You can expand the filter array if you need to, or change the 16 to something larger, but I found this isn't as harsh as the one you have.
const int filterWidth = 5;
const int filterHeight = 5;
double[,] filter = new double[filterWidth,filterHeight] {
{ -1, -1, -1, -1, -1 },
{ -1, 2, 2, 2, -1 },
{ -1, 2, 16, 2, -1 },
{ -1, 2, 2, 2, -1 },
{ -1, -1, -1, -1, -1 }
};
double factor = 1.0 / 16.0;
I combined niaher's and David's answer and fixed the "bias" property. Now you can pass a "strength" between 0.0 and 1.0 to the Sharpen() function.
/// <summary>
/// Sharpens the specified image.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="strength">The strength between 0.0 and 1.0.</param>
/// <returns></returns>
public static Bitmap Sharpen(Image image, double strength)
{
using (var bitmap = image as Bitmap)
{
if (bitmap != null)
{
var sharpenImage = bitmap.Clone() as Bitmap;
int width = image.Width;
int height = image.Height;
// Create sharpening filter.
const int filterWidth = 5;
const int filterHeight = 5;
var filter = new double[,]
{
{-1, -1, -1, -1, -1},
{-1, 2, 2, 2, -1},
{-1, 2, 16, 2, -1},
{-1, 2, 2, 2, -1},
{-1, -1, -1, -1, -1}
};
double bias = 1.0 - strength;
double factor = strength/16.0;
var result = new Color[image.Width,image.Height];
// Lock image bits for read/write.
if (sharpenImage != null)
{
BitmapData pbits = sharpenImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
// Declare an array to hold the bytes of the bitmap.
int bytes = pbits.Stride*height;
var rgbValues = new byte[bytes];
// Copy the RGB values into the array.
Marshal.Copy(pbits.Scan0, rgbValues, 0, bytes);
int rgb;
// Fill the color array with the new sharpened color values.
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
double red = 0.0, green = 0.0, blue = 0.0;
for (int filterX = 0; filterX < filterWidth; filterX++)
{
for (int filterY = 0; filterY < filterHeight; filterY++)
{
int imageX = (x - filterWidth/2 + filterX + width)%width;
int imageY = (y - filterHeight/2 + filterY + height)%height;
rgb = imageY*pbits.Stride + 3*imageX;
red += rgbValues[rgb + 2]*filter[filterX, filterY];
green += rgbValues[rgb + 1]*filter[filterX, filterY];
blue += rgbValues[rgb + 0]*filter[filterX, filterY];
}
rgb = y*pbits.Stride + 3*x;
int r = Math.Min(Math.Max((int) (factor*red + (bias*rgbValues[rgb + 2])), 0), 255);
int g = Math.Min(Math.Max((int) (factor*green + (bias*rgbValues[rgb + 1])), 0), 255);
int b = Math.Min(Math.Max((int) (factor*blue + (bias*rgbValues[rgb + 0])), 0), 255);
result[x, y] = Color.FromArgb(r, g, b);
}
}
}
// Update the image with the sharpened pixels.
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
rgb = y*pbits.Stride + 3*x;
rgbValues[rgb + 2] = result[x, y].R;
rgbValues[rgb + 1] = result[x, y].G;
rgbValues[rgb + 0] = result[x, y].B;
}
}
// Copy the RGB values back to the bitmap.
Marshal.Copy(rgbValues, 0, pbits.Scan0, bytes);
// Release image bits.
sharpenImage.UnlockBits(pbits);
}
return sharpenImage;
}
}
return null;
}
Ok, fixed the problem with distored edges. Here´s the updated one:
/// <summary>
/// Sharpens the specified image.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="strength">The strength.</param>
/// <returns></returns>
public static Bitmap Sharpen(Image image, double strength)
{
using (var bitmap = image as Bitmap)
{
if (bitmap != null)
{
var sharpenImage = bitmap.Clone() as Bitmap;
int width = image.Width;
int height = image.Height;
// Create sharpening filter.
const int filterSize = 5;
var filter = new double[,]
{
{-1, -1, -1, -1, -1},
{-1, 2, 2, 2, -1},
{-1, 2, 16, 2, -1},
{-1, 2, 2, 2, -1},
{-1, -1, -1, -1, -1}
};
double bias = 1.0 - strength;
double factor = strength/16.0;
const int s = filterSize/2;
var result = new Color[image.Width,image.Height];
// Lock image bits for read/write.
if (sharpenImage != null)
{
BitmapData pbits = sharpenImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
// Declare an array to hold the bytes of the bitmap.
int bytes = pbits.Stride*height;
var rgbValues = new byte[bytes];
// Copy the RGB values into the array.
Marshal.Copy(pbits.Scan0, rgbValues, 0, bytes);
int rgb;
// Fill the color array with the new sharpened color values.
for (int x = s; x < width - s; x++)
{
for (int y = s; y < height - s; y++)
{
double red = 0.0, green = 0.0, blue = 0.0;
for (int filterX = 0; filterX < filterSize; filterX++)
{
for (int filterY = 0; filterY < filterSize; filterY++)
{
int imageX = (x - s + filterX + width)%width;
int imageY = (y - s + filterY + height)%height;
rgb = imageY*pbits.Stride + 3*imageX;
red += rgbValues[rgb + 2]*filter[filterX, filterY];
green += rgbValues[rgb + 1]*filter[filterX, filterY];
blue += rgbValues[rgb + 0]*filter[filterX, filterY];
}
rgb = y * pbits.Stride + 3 * x;
int r = Math.Min(Math.Max((int)(factor * red + (bias * rgbValues[rgb + 2])), 0), 255);
int g = Math.Min(Math.Max((int)(factor * green + (bias * rgbValues[rgb + 1])), 0), 255);
int b = Math.Min(Math.Max((int)(factor * blue + (bias * rgbValues[rgb + 0])), 0), 255);
result[x, y] = Color.FromArgb(r, g, b);
}
}
}
// Update the image with the sharpened pixels.
for (int x = s; x < width - s; x++)
{
for (int y = s; y < height - s; y++)
{
rgb = y*pbits.Stride + 3*x;
rgbValues[rgb + 2] = result[x, y].R;
rgbValues[rgb + 1] = result[x, y].G;
rgbValues[rgb + 0] = result[x, y].B;
}
}
// Copy the RGB values back to the bitmap.
Marshal.Copy(rgbValues, 0, pbits.Scan0, bytes);
// Release image bits.
sharpenImage.UnlockBits(pbits);
}
return sharpenImage;
}
}
return null;
}
hi i edit the code a litle bit and add two other matrices
for me this works now perfect
/// <summary>
/// Sharpens the specified image.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="strength">strength erwartet werte zwische 0 - 99</param>
/// <returns></returns>
public Bitmap Sharpen(Image image, whichMatrix welcheMatrix , double strength)
{
double FaktorKorrekturWert = 0;
//strenght muß für den jeweiligen filter angepasst werden
switch (welcheMatrix)
{
case whichMatrix.Gaussian3x3:
//diese Matrix benötigt einen strenght Wert von 0 bis -9.9 default ist -2.5
//und einen korekturwert von 16
strength = (strength * -1) / 10;
FaktorKorrekturWert = 16;
break;
case whichMatrix.Mean3x3:
//diese Matrix benötigt einen strenght Wert von 0 bis -9 default ist -2.25
//und einen Korrekturwert von 10
strength = strength * -9 / 100;
FaktorKorrekturWert = 10;
break;
case whichMatrix.Gaussian5x5Type1:
//diese Matrix benötigt einen strenght Wert von 0 bis 2.5 default ist 1.25
//und einen Korrekturwert von 12
strength = strength * 2.5 / 100;
FaktorKorrekturWert = 12;
break;
default:
break;
}
using (var bitmap = image as Bitmap)
{
if (bitmap != null)
{
var sharpenImage = bitmap.Clone() as Bitmap;
int width = image.Width;
int height = image.Height;
// Create sharpening filter.
var filter = Matrix(welcheMatrix);
//const int filterSize = 3; // wenn die Matrix 3 Zeilen und 3 Spalten besitzt dann 3 bei 4 = 4 usw.
int filterSize = filter.GetLength(0);
double bias = 1.0 - strength;
double factor = strength / FaktorKorrekturWert;
//const int s = filterSize / 2;
int s = filterSize / 2; // Filtersize ist keine Constante mehr darum wurde der befehl const entfernt
var result = new Color[image.Width, image.Height];
// Lock image bits for read/write.
if (sharpenImage != null)
{
BitmapData pbits = sharpenImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// Declare an array to hold the bytes of the bitmap.
int bytes = pbits.Stride * height;
var rgbValues = new byte[bytes];
// Copy the RGB values into the array.
Marshal.Copy(pbits.Scan0, rgbValues, 0, bytes);
int rgb;
// Fill the color array with the new sharpened color values.
for (int x = s; x < width - s; x++)
{
for (int y = s; y < height - s; y++)
{
double red = 0.0, green = 0.0, blue = 0.0;
for (int filterX = 0; filterX < filterSize; filterX++)
{
for (int filterY = 0; filterY < filterSize; filterY++)
{
int imageX = (x - s + filterX + width) % width;
int imageY = (y - s + filterY + height) % height;
rgb = imageY * pbits.Stride + 3 * imageX;
red += rgbValues[rgb + 2] * filter[filterX, filterY];
green += rgbValues[rgb + 1] * filter[filterX, filterY];
blue += rgbValues[rgb + 0] * filter[filterX, filterY];
}
rgb = y * pbits.Stride + 3 * x;
int r = Math.Min(Math.Max((int)(factor * red + (bias * rgbValues[rgb + 2])), 0), 255);
int g = Math.Min(Math.Max((int)(factor * green + (bias * rgbValues[rgb + 1])), 0), 255);
int b = Math.Min(Math.Max((int)(factor * blue + (bias * rgbValues[rgb + 0])), 0), 255);
result[x, y] = System.Drawing.Color.FromArgb(r, g, b);
}
}
}
// Update the image with the sharpened pixels.
for (int x = s; x < width - s; x++)
{
for (int y = s; y < height - s; y++)
{
rgb = y * pbits.Stride + 3 * x;
rgbValues[rgb + 2] = result[x, y].R;
rgbValues[rgb + 1] = result[x, y].G;
rgbValues[rgb + 0] = result[x, y].B;
}
}
// Copy the RGB values back to the bitmap.
Marshal.Copy(rgbValues, 0, pbits.Scan0, bytes);
// Release image bits.
sharpenImage.UnlockBits(pbits);
}
return sharpenImage;
}
}
return null;
}
public enum whichMatrix
{
Gaussian3x3,
Mean3x3,
Gaussian5x5Type1
}
private double[,] Matrix(whichMatrix welcheMatrix)
{
double[,] selectedMatrix = null;
switch (welcheMatrix)
{
case whichMatrix.Gaussian3x3:
selectedMatrix = new double[,]
{
{ 1, 2, 1, },
{ 2, 4, 2, },
{ 1, 2, 1, },
};
break;
case whichMatrix.Gaussian5x5Type1:
selectedMatrix = new double[,]
{
{-1, -1, -1, -1, -1},
{-1, 2, 2, 2, -1},
{-1, 2, 16, 2, -1},
{-1, 2, -1, 2, -1},
{-1, -1, -1, -1, -1}
};
break;
case whichMatrix.Mean3x3:
selectedMatrix =new double[,]
{
{ 1, 1, 1, },
{ 1, 1, 1, },
{ 1, 1, 1, },
};
break;
}
return selectedMatrix;
}

Categories

Resources