Skip to content

Instantly share code, notes, and snippets.

@Cellane
Created November 13, 2012 09:40
Show Gist options
  • Select an option

  • Save Cellane/4064912 to your computer and use it in GitHub Desktop.

Select an option

Save Cellane/4064912 to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
namespace Convolution
{
public class Convolution
{
public Image DoMagic(Bitmap input, ConvolutionKernel kernel)
{
var output = new Bitmap(input.Width, input.Height);
var s = kernel.Size / 2;
for (var x = s; x < input.Width - s; x++)
{
for (var y = s; y < input.Height - s; y++)
{
int r = 0, b = 0, g = 0;
for (var i = 0; i < kernel.Size; i++)
{
for (var j = 0; j < kernel.Size; j++)
{
var temp = input.GetPixel(x + i - s, y + j - s);
r += kernel.Matrix[i, j] * temp.R;
g += kernel.Matrix[i, j] * temp.G;
b += kernel.Matrix[i, j] * temp.B;
}
}
r = Math.Min(Math.Max((r / kernel.Factor) + kernel.Offset, 0), 255);
g = Math.Min(Math.Max((g / kernel.Factor) + kernel.Offset, 0), 255);
b = Math.Min(Math.Max((b / kernel.Factor) + kernel.Offset, 0), 255);
output.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
return output;
}
}
}
namespace Convolution
{
public class ConvolutionKernel
{
public int Factor { get; set; }
public int Offset { get; set; }
private int[,] _matrix =
{
{-2, -2, -2, -2, -2},
{-1, -1, -1, -1, -1},
{0, 0, 0, 0, 0},
{1, 1, 1, 1, 1},
{2, 2, 2, 2, 2}
};
public int[,] Matrix
{
get
{
return _matrix;
}
set
{
_matrix = value;
Factor = 0;
for (var i = 0; i < Size; i++)
{
for (var j = 0; j < Size; j++)
{
Factor += _matrix[i, j];
}
}
if (Factor == 0)
{
Factor = 1;
}
}
}
private int _size = 5;
public int Size
{
get
{
return _size;
}
set
{
if (value != 1 && value != 3 && value != 5 && value != 7)
{
_size = 5;
}
else
{
_size = value;
}
}
}
public ConvolutionKernel()
{
Offset = 0;
Factor = 1;
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Convolution
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void BtnSmallClick(object sender, EventArgs e)
{
var convolution = new Convolution();
var source = new Bitmap(pbSource.Image);
var kernel = new ConvolutionKernel();
int[,] matrix = {{-1, -1, -1}, {0, 0, 0}, {1, 1, 1}};
kernel.Size = 3;
kernel.Matrix = matrix;
var image = convolution.DoMagic(source, kernel);
pbSmall.Image = image;
Refresh();
}
private void BtnBigClick(object sender, EventArgs e)
{
var convolution = new Convolution();
var source = new Bitmap(pbSource.Image);
var kernel = new ConvolutionKernel();
var image = convolution.DoMagic(source, kernel);
pbBig.Image = image;
Refresh();
}
}
}
namespace Convolution
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pbSource = new System.Windows.Forms.PictureBox();
this.pbSmall = new System.Windows.Forms.PictureBox();
this.btnSmall = new System.Windows.Forms.Button();
this.pbBig = new System.Windows.Forms.PictureBox();
this.btnBig = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pbSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pbSmall)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pbBig)).BeginInit();
this.SuspendLayout();
//
// pbSource
//
this.pbSource.Image = global::Convolution.Properties.Resources.Fluttershy;
this.pbSource.Location = new System.Drawing.Point(12, 12);
this.pbSource.Name = "pbSource";
this.pbSource.Size = new System.Drawing.Size(300, 340);
this.pbSource.TabIndex = 0;
this.pbSource.TabStop = false;
//
// pbSmall
//
this.pbSmall.Location = new System.Drawing.Point(318, 12);
this.pbSmall.Name = "pbSmall";
this.pbSmall.Size = new System.Drawing.Size(300, 340);
this.pbSmall.TabIndex = 1;
this.pbSmall.TabStop = false;
//
// btnSmall
//
this.btnSmall.Location = new System.Drawing.Point(318, 12);
this.btnSmall.Name = "btnSmall";
this.btnSmall.Size = new System.Drawing.Size(75, 23);
this.btnSmall.TabIndex = 2;
this.btnSmall.Text = "3*3";
this.btnSmall.UseVisualStyleBackColor = true;
this.btnSmall.Click += new System.EventHandler(this.BtnSmallClick);
//
// pbBig
//
this.pbBig.Location = new System.Drawing.Point(624, 12);
this.pbBig.Name = "pbBig";
this.pbBig.Size = new System.Drawing.Size(300, 340);
this.pbBig.TabIndex = 3;
this.pbBig.TabStop = false;
//
// btnBig
//
this.btnBig.Location = new System.Drawing.Point(624, 12);
this.btnBig.Name = "btnBig";
this.btnBig.Size = new System.Drawing.Size(75, 23);
this.btnBig.TabIndex = 4;
this.btnBig.Text = "5*5";
this.btnBig.UseVisualStyleBackColor = true;
this.btnBig.Click += new System.EventHandler(this.BtnBigClick);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(936, 364);
this.Controls.Add(this.btnBig);
this.Controls.Add(this.pbBig);
this.Controls.Add(this.btnSmall);
this.Controls.Add(this.pbSmall);
this.Controls.Add(this.pbSource);
this.Name = "MainForm";
this.Text = "MainForm";
((System.ComponentModel.ISupportInitialize)(this.pbSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pbSmall)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pbBig)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pbSource;
private System.Windows.Forms.PictureBox pbSmall;
private System.Windows.Forms.Button btnSmall;
private System.Windows.Forms.PictureBox pbBig;
private System.Windows.Forms.Button btnBig;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Convolution
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
@bolorundurowb
Copy link
Copy Markdown

Thanks for this, it is quite the help.

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