Skip to content

Instantly share code, notes, and snippets.

@piyushchauhan2011
Created October 8, 2016 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piyushchauhan2011/6fd88bd86d52e35121ae646d50ca6719 to your computer and use it in GitHub Desktop.
Save piyushchauhan2011/6fd88bd86d52e35121ae646d50ca6719 to your computer and use it in GitHub Desktop.
Image Processing algorithms with CSharp and Xamarin IDE on osx
using System;
using System.Drawing;
using System.IO;
namespace ImageProcessing
{
class MainClass
{
static void RunImage()
{
var f = File.OpenRead("piyush.jpg");
var img = Image.FromStream(f);
var bmp = new Bitmap(img);
//GrayFilter(bmp);
//NegativeFilter(bmp);
//LogFilter(bmp);
GammaFilter(bmp);
bmp.Save("grayscale.jpg");
f.Close();
}
static void GrayFilter(Bitmap bmp)
{
var width = bmp.Width;
var height = bmp.Height;
for (var i = 0; i < height; i++)
{
for (var j = 0; j < width; j++)
{
var c = bmp.GetPixel(j, i);
var red = Convert.ToByte(c.R * 0.299);
var green = Convert.ToByte(c.G * 0.587);
var blue = Convert.ToByte(c.B * 0.114);
var color = Color
.FromArgb(red + green + blue, red + green + blue, red + green + blue);
bmp.SetPixel(j, i, color);
}
}
}
static void NegativeFilter(Bitmap bmp)
{
var width = bmp.Width;
var height = bmp.Height;
for (var i = 0; i < height; i++)
{
for (var j = 0; j < width; j++)
{
var c = bmp.GetPixel(j, i);
var red = 255 - c.R;
var green = 255 - c.G;
var blue = 255 - c.B;
var color = Color
.FromArgb(red, green, blue);
bmp.SetPixel(j, i, color);
}
}
}
static void LogFilter(Bitmap bmp)
{
var width = bmp.Width;
var height = bmp.Height;
for (var i = 0; i < height; i++)
{
for (var j = 0; j < width; j++)
{
var c = bmp.GetPixel(j, i);
var con = 100;
var red = Convert.ToByte(con * Math.Log10(c.R + 1));
var green = Convert.ToByte(con * Math.Log10(c.G + 1));
var blue = Convert.ToByte(con * Math.Log10(c.B + 1));
var color = Color
.FromArgb(red, green, blue);
bmp.SetPixel(j, i, color);
}
}
}
static void GammaFilter(Bitmap bmp)
{
var width = bmp.Width;
var height = bmp.Height;
for (var i = 0; i < height; i++)
{
for (var j = 0; j < width; j++)
{
var c = bmp.GetPixel(j, i);
var con = 100;
var Gamma = 1 / 10.0;
var red = Convert.ToByte(con * Math.Pow(c.R, Gamma));
var green = Convert.ToByte(con * Math.Pow(c.G, Gamma));
var blue = Convert.ToByte(con * Math.Pow(c.B, Gamma));
var color = Color
.FromArgb(red, green, blue);
bmp.SetPixel(j, i, color);
}
}
}
public static void Main(string[] args)
{
Console.WriteLine("Image Processing Algorithms!");
RunImage();
}
}
}
@piyushchauhan2011
Copy link
Author

Example outputs:

Negative Filter

grayscale

@piyushchauhan2011
Copy link
Author

Make sure to add the piyush.jpg in Xamarin IDE by selecting add files. Also, select the copy to output by right-clicking. The file is given below:

piyush

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment