This is a continuation of the previous post at Handling Images in C#
A Grayscale Filter
To develop a Grayscale filter simply add up the red green and blue pixels and divide by 3. This averages out the values.
public static Bitmap GrayScaleFilter(Bitmap inputbitmap)
{
Bitmap outputbitmap = new Bitmap(inputbitmap.Width, inputbitmap.Height);
BitmapData inputdata = inputbitmap.LockBits(new Rectangle(0, 0, inputbitmap.Width, inputbitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData outputdata = outputbitmap.LockBits(new Rectangle(0, 0, outputbitmap.Width, outputbitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int inputstride = inputdata.Stride;
int outputstride = outputdata.Stride;
System.IntPtr inputScan = inputdata.Scan0;
System.IntPtr outputScan = outputdata.Scan0;
unsafe
{
byte* newp1 = (byte*)(void*)inputScan;
byte* newp2 = (byte*)(void*)outputScan;
int newOffset1 = inputstride - inputbitmap.Width * 3;
int newOffset2 = outputstride - outputdata.Width * 3;
byte redvalue, greenvalue, bluevalue;
byte newred, newgreen, newblue;
for (int y = 0; y < inputbitmap.Height; ++y)
{
for (int x = 0; x < inputbitmap.Width; ++x)
{
bluevalue = newp1[0];
greenvalue = newp1[1];
redvalue = newp1[2];
byte grayscalevalue =(byte )( (redvalue + greenvalue + bluevalue) / 3.0);
newp2[0] = grayscalevalue ;
newp2[1] = grayscalevalue ;
newp2[2] = grayscalevalue ;
newp1 += 3;
newp2 += 3;
}
newp1 += newOffset1;
newp2 += newOffset2;
}
}
inputbitmap.UnlockBits(inputdata );
outputbitmap.UnlockBits(outputdata );
return outputbitmap;
}
A BlackAndWhite Filter
Simply take the averaged value from the previous filter. If the average is greater than 128 make it white 255 else make it black 255.
public static Bitmap BlackAndWhiteFilter(Bitmap inputbitmap)
{
Bitmap outputbitmap = new Bitmap(inputbitmap.Width, inputbitmap.Height);
BitmapData inputdata = inputbitmap.LockBits(new Rectangle(0, 0, inputbitmap.Width, inputbitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData outputdata = outputbitmap.LockBits(new Rectangle(0, 0, outputbitmap.Width, outputbitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int inputstride = inputdata.Stride;
int outputstride = outputdata.Stride;
System.IntPtr inputScan = inputdata.Scan0;
System.IntPtr outputScan = outputdata.Scan0;
unsafe
{
byte* newp1 = (byte*)(void*)inputScan;
byte* newp2 = (byte*)(void*)outputScan;
int newOffset1 = inputstride - inputbitmap.Width * 3;
int newOffset2 = outputstride - outputdata.Width * 3;
byte redvalue, greenvalue, bluevalue;
for (int y = 0; y < inputbitmap.Height; ++y)
{
for (int x = 0; x < inputbitmap.Width; ++x)
{
bluevalue = newp1[0];
greenvalue = newp1[1];
redvalue = newp1[2];
byte avgvalue =(byte )( (redvalue + greenvalue + bluevalue) / 3.0);
if (avgvalue > 128)
avgvalue = 255;
else
avgvalue = 0;
newp2[0] = avgvalue ;
newp2[1] = avgvalue ;
newp2[2] = avgvalue ;
newp1 += 3;
newp2 += 3;
}
newp1 += newOffset1;
newp2 += newOffset2;
}
}
inputbitmap.UnlockBits(inputdata );
outputbitmap.UnlockBits(outputdata );
return outputbitmap;
}
Next we shall develop edge detection filters.
A Grayscale Filter
To develop a Grayscale filter simply add up the red green and blue pixels and divide by 3. This averages out the values.
public static Bitmap GrayScaleFilter(Bitmap inputbitmap)
{
Bitmap outputbitmap = new Bitmap(inputbitmap.Width, inputbitmap.Height);
BitmapData inputdata = inputbitmap.LockBits(new Rectangle(0, 0, inputbitmap.Width, inputbitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData outputdata = outputbitmap.LockBits(new Rectangle(0, 0, outputbitmap.Width, outputbitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int inputstride = inputdata.Stride;
int outputstride = outputdata.Stride;
System.IntPtr inputScan = inputdata.Scan0;
System.IntPtr outputScan = outputdata.Scan0;
unsafe
{
byte* newp1 = (byte*)(void*)inputScan;
byte* newp2 = (byte*)(void*)outputScan;
int newOffset1 = inputstride - inputbitmap.Width * 3;
int newOffset2 = outputstride - outputdata.Width * 3;
byte redvalue, greenvalue, bluevalue;
byte newred, newgreen, newblue;
for (int y = 0; y < inputbitmap.Height; ++y)
{
for (int x = 0; x < inputbitmap.Width; ++x)
{
bluevalue = newp1[0];
greenvalue = newp1[1];
redvalue = newp1[2];
byte grayscalevalue =(byte )( (redvalue + greenvalue + bluevalue) / 3.0);
newp2[0] = grayscalevalue ;
newp2[1] = grayscalevalue ;
newp2[2] = grayscalevalue ;
newp1 += 3;
newp2 += 3;
}
newp1 += newOffset1;
newp2 += newOffset2;
}
}
inputbitmap.UnlockBits(inputdata );
outputbitmap.UnlockBits(outputdata );
return outputbitmap;
}
A BlackAndWhite Filter
Simply take the averaged value from the previous filter. If the average is greater than 128 make it white 255 else make it black 255.
public static Bitmap BlackAndWhiteFilter(Bitmap inputbitmap)
{
Bitmap outputbitmap = new Bitmap(inputbitmap.Width, inputbitmap.Height);
BitmapData inputdata = inputbitmap.LockBits(new Rectangle(0, 0, inputbitmap.Width, inputbitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData outputdata = outputbitmap.LockBits(new Rectangle(0, 0, outputbitmap.Width, outputbitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int inputstride = inputdata.Stride;
int outputstride = outputdata.Stride;
System.IntPtr inputScan = inputdata.Scan0;
System.IntPtr outputScan = outputdata.Scan0;
unsafe
{
byte* newp1 = (byte*)(void*)inputScan;
byte* newp2 = (byte*)(void*)outputScan;
int newOffset1 = inputstride - inputbitmap.Width * 3;
int newOffset2 = outputstride - outputdata.Width * 3;
byte redvalue, greenvalue, bluevalue;
for (int y = 0; y < inputbitmap.Height; ++y)
{
for (int x = 0; x < inputbitmap.Width; ++x)
{
bluevalue = newp1[0];
greenvalue = newp1[1];
redvalue = newp1[2];
byte avgvalue =(byte )( (redvalue + greenvalue + bluevalue) / 3.0);
if (avgvalue > 128)
avgvalue = 255;
else
avgvalue = 0;
newp2[0] = avgvalue ;
newp2[1] = avgvalue ;
newp2[2] = avgvalue ;
newp1 += 3;
newp2 += 3;
}
newp1 += newOffset1;
newp2 += newOffset2;
}
}
inputbitmap.UnlockBits(inputdata );
outputbitmap.UnlockBits(outputdata );
return outputbitmap;
}
Next we shall develop edge detection filters.
No comments:
Post a Comment