Click here to Skip to main content
12,406,528 members (27,308 online)
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads

Stats

2.4M views
134.9K downloads
1.2K bookmarked
Posted

Image Processing Lab in C#

, 13 Mar 2007 GPL3
A tool and library for image processing
iplab_demo
AForge.dll
AForge.Imaging.chm
AForge.Imaging.dll
AForge.Math.dll
DockingToolbar.dll
iplab.exe
SourceGrid2.dll
SourceLibrary.dll
WeifenLuo.WinFormsUI.dll
iplab_src
icons
about.gif
clone.gif
convolution.gif
copy.gif
cut.gif
disk.gif
fit_to_size.gif
folder_out.gif
fourier.gif
grayscale.gif
histogram_window.gif
levels.gif
morphology.gif
paste.gif
pie-chart.gif
resize.gif
rotate.gif
threshold.gif
view_1_1.gif
zoom_in.gif
zoom_out.gif
iplab
App.ico
banner.gif
column-chart.ico
Controls
Filters Forms
line-chart.ico
References
AForge.dll
AForge.Imaging.dll
AForge.Math.dll
DockingToolbar.dll
SourceGrid2.dll
SourceLibrary.dll
WeifenLuo.WinFormsUI.dll
Resources
arrow.bmp
harrow.cur
hmove.cur
Tools
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using AForge.Imaging.Filters;

namespace IPLab
{
    /// <summary>
    /// Summary description for MathMorphologyForm.
    /// </summary>
    public class MathMorphologyForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.ComboBox sizeCombo;
        private System.Windows.Forms.Label label1;
        private IPLab.GridArrayInt grid;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox operatorCombo;

        private static int[] sizes = new int[] { 3, 5, 7, 9, 11, 13, 15 };
        private short[,] se;
        private AForge.Imaging.Filters.IFilter filter;
        private System.Windows.Forms.Button OkButton;
        private System.Windows.Forms.Button cancelButton;
        private System.Windows.Forms.Button loadButton;
        private System.Windows.Forms.Button saveButton;
        private System.Windows.Forms.OpenFileDialog ofd;
        private System.Windows.Forms.SaveFileDialog sfd;
        private System.Windows.Forms.GroupBox groupBox2;
        private IPLab.FilterPreview filterPreview;
        private FilterTypes filterType = FilterTypes.Simple;
        private System.Windows.Forms.Label legendLabel;

        // Type enumeration
        public enum FilterTypes
        {
            Simple = 0,
            HitAndMiss = 1
        }

        // Filter property
        public IFilter Filter
        {
            get { return filter; }
        }
        // Image property
        public Bitmap Image
        {
            set { filterPreview.Image = value; }
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        // Constructor
        public MathMorphologyForm( FilterTypes filterType )
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent( );

            // filter type
            this.filterType = filterType;

            if ( filterType == FilterTypes.Simple )
            {
                legendLabel.Text = "1 - foreground, -1 - don't care";

                this.operatorCombo.Items.AddRange( new object[] {
																   "Errosion",
																   "Dilatation",
																   "Opening",
																   "Closing"} );
            }
            else
            {
                legendLabel.Text = "1 - foreground, 0 - background, -1 - don't care";

                this.operatorCombo.Items.AddRange( new object[] {
																   "Hit And Miss",
																   "Thickening",
																   "Thinning"} );
            }

            // default kernel
            se = new short[3, 3] {
											 {1, 1, 1},
											 {1, 1, 1},
											 {1, 1, 1}};
            grid.LoadData( se );

            // add kernel sizes
            foreach ( int size in sizes )
            {
                string item = size.ToString( ) + " x " + size.ToString( );
                this.sizeCombo.Items.Add( (object) item );
            }

            // default size
            this.sizeCombo.SelectedIndex = 0;

            // default kernel
            this.operatorCombo.SelectedIndex = 0;
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if ( disposing )
            {
                if ( 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.groupBox1 = new System.Windows.Forms.GroupBox( );
            this.legendLabel = new System.Windows.Forms.Label( );
            this.loadButton = new System.Windows.Forms.Button( );
            this.saveButton = new System.Windows.Forms.Button( );
            this.grid = new IPLab.GridArrayInt( );
            this.sizeCombo = new System.Windows.Forms.ComboBox( );
            this.label1 = new System.Windows.Forms.Label( );
            this.label2 = new System.Windows.Forms.Label( );
            this.operatorCombo = new System.Windows.Forms.ComboBox( );
            this.OkButton = new System.Windows.Forms.Button( );
            this.cancelButton = new System.Windows.Forms.Button( );
            this.ofd = new System.Windows.Forms.OpenFileDialog( );
            this.sfd = new System.Windows.Forms.SaveFileDialog( );
            this.groupBox2 = new System.Windows.Forms.GroupBox( );
            this.filterPreview = new IPLab.FilterPreview( );
            this.groupBox1.SuspendLayout( );
            this.groupBox2.SuspendLayout( );
            this.SuspendLayout( );
            // 
            // groupBox1
            // 
            this.groupBox1.Anchor = ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom )
                | System.Windows.Forms.AnchorStyles.Left )
                | System.Windows.Forms.AnchorStyles.Right );
            this.groupBox1.Controls.AddRange( new System.Windows.Forms.Control[] {
																					this.legendLabel,
																					this.loadButton,
																					this.saveButton,
																					this.grid} );
            this.groupBox1.Location = new System.Drawing.Point( 10, 10 );
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size( 425, 375 );
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Structuring Element";
            // 
            // legendLabel
            // 
            this.legendLabel.Anchor = ( System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left );
            this.legendLabel.BackColor = System.Drawing.Color.White;
            this.legendLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.legendLabel.Location = new System.Drawing.Point( 10, 345 );
            this.legendLabel.Name = "legendLabel";
            this.legendLabel.Size = new System.Drawing.Size( 240, 18 );
            this.legendLabel.TabIndex = 4;
            this.legendLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // loadButton
            // 
            this.loadButton.Anchor = ( System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right );
            this.loadButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.loadButton.Location = new System.Drawing.Point( 340, 345 );
            this.loadButton.Name = "loadButton";
            this.loadButton.TabIndex = 2;
            this.loadButton.TabStop = false;
            this.loadButton.Text = "&Load";
            this.loadButton.Click += new System.EventHandler( this.loadButton_Click );
            // 
            // saveButton
            // 
            this.saveButton.Anchor = ( System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right );
            this.saveButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.saveButton.Location = new System.Drawing.Point( 255, 345 );
            this.saveButton.Name = "saveButton";
            this.saveButton.TabIndex = 3;
            this.saveButton.TabStop = false;
            this.saveButton.Text = "&Save";
            this.saveButton.Click += new System.EventHandler( this.saveButton_Click );
            // 
            // grid
            // 
            this.grid.Anchor = ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom )
                | System.Windows.Forms.AnchorStyles.Left )
                | System.Windows.Forms.AnchorStyles.Right );
            this.grid.AutoSizeMinHeight = 10;
            this.grid.AutoSizeMinWidth = 10;
            this.grid.AutoStretchColumnsToFitWidth = false;
            this.grid.AutoStretchRowsToFitHeight = false;
            this.grid.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.grid.ContextMenuStyle = SourceGrid2.ContextMenuStyle.None;
            this.grid.CustomSort = false;
            this.grid.GridToolTipActive = true;
            this.grid.Location = new System.Drawing.Point( 10, 20 );
            this.grid.Name = "grid";
            this.grid.Size = new System.Drawing.Size( 405, 315 );
            this.grid.SpecialKeys = SourceGrid2.GridSpecialKeys.Default;
            this.grid.TabIndex = 0;
            this.grid.ValueChanged += new IPLab.GridArrayInt.GridEventHandler( this.grid_ValueChanged );
            // 
            // sizeCombo
            // 
            this.sizeCombo.Anchor = ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right );
            this.sizeCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.sizeCombo.Location = new System.Drawing.Point( 450, 30 );
            this.sizeCombo.Name = "sizeCombo";
            this.sizeCombo.Size = new System.Drawing.Size( 170, 21 );
            this.sizeCombo.TabIndex = 1;
            this.sizeCombo.SelectedIndexChanged += new System.EventHandler( this.sizeCombo_SelectedIndexChanged );
            // 
            // label1
            // 
            this.label1.Anchor = ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right );
            this.label1.Location = new System.Drawing.Point( 450, 15 );
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size( 132, 15 );
            this.label1.TabIndex = 0;
            this.label1.Text = "Structuring element size:";
            // 
            // label2
            // 
            this.label2.Anchor = ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right );
            this.label2.Location = new System.Drawing.Point( 450, 60 );
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size( 120, 15 );
            this.label2.TabIndex = 2;
            this.label2.Text = "Morpology operator:";
            // 
            // operatorCombo
            // 
            this.operatorCombo.Anchor = ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right );
            this.operatorCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.operatorCombo.Location = new System.Drawing.Point( 450, 75 );
            this.operatorCombo.Name = "operatorCombo";
            this.operatorCombo.Size = new System.Drawing.Size( 170, 21 );
            this.operatorCombo.TabIndex = 3;
            this.operatorCombo.SelectedIndexChanged += new System.EventHandler( this.operatorCombo_SelectedIndexChanged );
            // 
            // OkButton
            // 
            this.OkButton.Anchor = ( System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right );
            this.OkButton.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.OkButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.OkButton.Location = new System.Drawing.Point( 460, 355 );
            this.OkButton.Name = "OkButton";
            this.OkButton.TabIndex = 4;
            this.OkButton.Text = "Ok";
            // 
            // cancelButton
            // 
            this.cancelButton.Anchor = ( System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right );
            this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.cancelButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.cancelButton.Location = new System.Drawing.Point( 545, 355 );
            this.cancelButton.Name = "cancelButton";
            this.cancelButton.TabIndex = 5;
            this.cancelButton.Text = "Cancel";
            // 
            // ofd
            // 
            this.ofd.DefaultExt = "txt";
            this.ofd.Filter = "Text files (*.txt)|*.txt";
            this.ofd.Title = "Load structuring element from file";
            // 
            // sfd
            // 
            this.sfd.DefaultExt = "txt";
            this.sfd.FileName = "se.txt";
            this.sfd.Filter = "Text files (*.txt)|*.txt";
            this.sfd.Title = "Save structuring element to file";
            // 
            // groupBox2
            // 
            this.groupBox2.Anchor = ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right );
            this.groupBox2.Controls.AddRange( new System.Windows.Forms.Control[] {
																					this.filterPreview} );
            this.groupBox2.Location = new System.Drawing.Point( 450, 105 );
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size( 170, 175 );
            this.groupBox2.TabIndex = 10;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Preview";
            // 
            // filterPreview
            // 
            this.filterPreview.Image = null;
            this.filterPreview.Location = new System.Drawing.Point( 10, 15 );
            this.filterPreview.Name = "filterPreview";
            this.filterPreview.Size = new System.Drawing.Size( 150, 150 );
            this.filterPreview.TabIndex = 8;
            // 
            // MathMorphologyForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size( 5, 13 );
            this.CancelButton = this.cancelButton;
            this.ClientSize = new System.Drawing.Size( 627, 396 );
            this.Controls.AddRange( new System.Windows.Forms.Control[] {
																		  this.groupBox2,
																		  this.cancelButton,
																		  this.OkButton,
																		  this.operatorCombo,
																		  this.label2,
																		  this.label1,
																		  this.sizeCombo,
																		  this.groupBox1} );
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
            this.Name = "MathMorphologyForm";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Mathematical Morphology";
            this.groupBox1.ResumeLayout( false );
            this.groupBox2.ResumeLayout( false );
            this.ResumeLayout( false );

        }
        #endregion

        // Selection changed in size combo
        private void sizeCombo_SelectedIndexChanged( object sender, System.EventArgs e )
        {
            if ( se != null )
            {
                int size = sizes[this.sizeCombo.SelectedIndex];
                int oldSize = se.GetLength( 0 );
                int d = ( size - oldSize ) >> 1;
                short[,] array = new short[size, size];

                // copy old kernel into new
                for ( int i = 0; i < oldSize; i++ )
                {
                    if ( i + d < 0 )
                        continue;
                    if ( i + d >= size )
                        break;

                    for ( int j = 0; j < oldSize; j++ )
                    {
                        if ( j + d < 0 )
                            continue;
                        if ( j + d >= size )
                            break;
                        array[i + d, j + d] = se[i, j];
                    }
                }

                se = array;
                grid.LoadData( se );
                UpdateFilter( );
            }
        }

        // Operator changed
        private void operatorCombo_SelectedIndexChanged( object sender, System.EventArgs e )
        {
            UpdateFilter( );
        }

        // Value changed in grid
        private void grid_ValueChanged( object sender, IPLab.GridEventArgs e )
        {
            UpdateFilter( );
        }

        // Update filter
        private void UpdateFilter( )
        {
            if ( se != null )
            {
                if ( filterType == FilterTypes.Simple )
                {
                    switch ( this.operatorCombo.SelectedIndex )
                    {
                        case 0:
                            filter = new Erosion( se );
                            break;
                        case 1:
                            filter = new Dilatation( se );
                            break;
                        case 2:
                            filter = new Opening( se );
                            break;
                        case 3:
                            filter = new Closing( se );
                            break;
                    }
                }
                else
                {
                    switch ( this.operatorCombo.SelectedIndex )
                    {
                        case 0:
                            filter = new HitAndMiss( se, HitAndMiss.Modes.HitAndMiss );
                            break;
                        case 1:
                            filter = new HitAndMiss( se, HitAndMiss.Modes.Thickening );
                            break;
                        case 2:
                            filter = new HitAndMiss( se, HitAndMiss.Modes.Thinning );
                            break;
                    }
                }
            }
            filterPreview.Filter = filter;
        }

        // On "Save" button - save kernel
        private void saveButton_Click( object sender, System.EventArgs e )
        {
            if ( sfd.ShowDialog( ) == DialogResult.OK )
            {
                try
                {
                    Serialize2DimArray.Save( sfd.FileName, se );
                }
                catch ( ApplicationException )
                {
                    MessageBox.Show( this, "Failed saving structuring elemnt to specified file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
                }
            }
        }

        // On "Load" button - load kernel
        private void loadButton_Click( object sender, System.EventArgs e )
        {
            if ( ofd.ShowDialog( ) == DialogResult.OK )
            {
                try
                {
                    short[,] array = (short[,]) Serialize2DimArray.Load( ofd.FileName, typeof( short ) );
                    int size = array.GetLength( 0 );
                    int i;

                    // check size
                    if ( size != array.GetLength( 1 ) )
                        throw new ApplicationException( );

                    for ( i = 0; i < sizes.Length; i++ )
                    {
                        if ( size == sizes[i] )
                        {
                            this.sizeCombo.SelectedIndex = i;
                            break;
                        }
                    }
                    if ( i == sizes.Length )
                        throw new ApplicationException( );

                    se = array;
                    grid.LoadData( se );
                    UpdateFilter( );
                }
                catch ( ApplicationException )
                {
                    MessageBox.Show( this, "Failed loading structuring element from specified file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Andrew Kirillov
Software Developer IBM
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET and not so open Computer Vision Sandbox.

Going out of computers I am just a man loving his family, enjoying traveling, a bit of books, a bit of movies and a mixture of everything else. Always wanted to learn playing guitar, but it seems like 6 strings are much harder than few dozens of keyboard’s keys. Will keep progressing ...

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 13 Mar 2007
Article Copyright 2005 by Andrew Kirillov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid