Click here to Skip to main content
12,622,291 members (35,405 online)
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads

Stats

3.8M views
137.6K 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
// Image Processing Lab
//
// Copyright � Andrew Kirillov, 2005
// andrew.kirillov@gmail.com
//

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace IPLab
{
    // Events arguments
    public class HistogramEventArgs : EventArgs
    {
        private int min, max;

        // Constructor
        public HistogramEventArgs( int pos )
        {
            this.min = pos;
        }
        public HistogramEventArgs( int min, int max )
        {
            this.min = min;
            this.max = max;
        }

        // Min property
        public int Min
        {
            get { return min; }
        }
        // Max property
        public int Max
        {
            get { return max; }
        }
        // Position property
        public int Position
        {
            get { return min; }
        }
    }


    /// <summary>
    /// Summary description for ColorHistogram.
    /// </summary>
    public class Histogram : System.Windows.Forms.Control
    {
        private Color color = Color.Black;
        private int width = 256;
        private int height = 150;
        private bool log = false;

        private int[] values;
        private int max;
        private double maxLog;
        private bool allowSelection = false;

        private Pen blackPen = new Pen( Color.Black, 1 );
        private Pen whitePen = new Pen( Color.White, 1 );
        private Pen drawPen = new Pen( Color.Black );
        private bool tracking = false;
        private bool over = false;
        private int start, stop;

        // Color property
        [DefaultValue( typeof( Color ), "Black" )]
        public Color Color
        {
            get { return color; }
            set
            {
                color = value;

                drawPen.Dispose( );
                drawPen = new Pen( color );
                Invalidate( );
            }
        }
        // Width property
        [DefaultValue( 256 )]
        public int HistogramWidth
        {
            get { return width; }
            set
            {
                width = value;
                Invalidate( );
            }
        }
        // Height property
        [DefaultValue( 150 )]
        public int HistogramHeight
        {
            get { return height; }
            set
            {
                height = value;
                Invalidate( );
            }
        }
        // Allow selection property
        [DefaultValue( false )]
        public bool AllowSelection
        {
            get { return allowSelection; }
            set { allowSelection = value; }
        }
        // LogView property
        [DefaultValue( false )]
        public bool LogView
        {
            get { return log; }
            set
            {
                log = value;
                Invalidate( );
            }
        }


        // Values property
        [Browsable( false )]
        public int[] Values
        {
            set
            {
                values = value;

                if ( values != null )
                {
                    // get the max
                    max = 0;
                    foreach ( int v in values )
                    {
                        if ( v > max )
                        {
                            max = v;
                            maxLog = Math.Log10( max );
                        }
                    }
                }
                Invalidate( );
            }
        }

        // events
        public delegate void HistogramEventHandler( object sender, HistogramEventArgs e );
        public event HistogramEventHandler PositionChanged;
        public event HistogramEventHandler SelectionChanged;

        // Constructor
        public Histogram( )
        {
            InitializeComponent( );
            SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw |
                ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true );
        }

        // Dispose
        protected override void Dispose( bool disposing )
        {
            if ( disposing )
            {
                blackPen.Dispose( );
                whitePen.Dispose( );
                drawPen.Dispose( );
            }
            base.Dispose( disposing );
        }

        // Init component
        private void InitializeComponent( )
        {
            // 
            // Histogram
            // 
            this.MouseUp += new System.Windows.Forms.MouseEventHandler( this.Histogram_MouseUp );
            this.MouseMove += new System.Windows.Forms.MouseEventHandler( this.Histogram_MouseMove );
            this.MouseLeave += new System.EventHandler( this.Histogram_MouseLeave );
            this.MouseDown += new System.Windows.Forms.MouseEventHandler( this.Histogram_MouseDown );

        }

        // Paint control
        protected override void OnPaint( PaintEventArgs pe )
        {
            Graphics g = pe.Graphics;
            int x = ( ClientRectangle.Right - width ) / 2;
            int y = 5;
            int l;

            // draw rectangle around the image
            g.DrawRectangle( blackPen, x - 1, y - 1, width + 1, height + 1 );

            if ( values != null )
            {
                int count = Math.Min( values.Length, width );
                int start = Math.Min( this.start, this.stop );
                int stop = Math.Max( this.start, this.stop );

                if ( tracking )
                {
                    // fill region of selection
                    Brush brush = new SolidBrush( Color.FromArgb( 92, 92, 92 ) );
                    g.FillRectangle( brush, x + start, y, Math.Abs( start - stop ), height );
                    brush.Dispose( );
                }

                // draw histogram
                for ( int i = 0; i < count; i++ )
                {
                    if ( log )
                    {
                        l = ( values[i] == 0 ) ? 0 : (int) ( Math.Log10( values[i] ) * height / maxLog );
                    }
                    else
                    {
                        l = (int) ( values[i] * height / max );
                    }

                    if ( l != 0 )
                    {
                        g.DrawLine( ( ( tracking ) && ( i >= start ) && ( i <= stop ) ) ? whitePen : drawPen,
                            new Point( x + i, y + height - 1 ),
                            new Point( x + i, y + height - l )
                            );
                    }
                }
            }

            // Calling the base class OnPaint
            base.OnPaint( pe );
        }

        // On mouse down
        private void Histogram_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
        {
            if ( ( allowSelection ) && ( values != null ) )
            {
                int x = ( ClientRectangle.Right - width ) / 2;
                int y = 5;

                if ( ( e.X >= x ) && ( e.Y >= y ) && ( e.X < x + width ) && ( e.Y < y + height ) )
                {
                    // start selection
                    tracking = true;
                    start = e.X - x;
                    this.Capture = true;
                }
            }
        }

        // On mouse up
        private void Histogram_MouseUp( object sender, System.Windows.Forms.MouseEventArgs e )
        {
            if ( tracking )
            {
                // stop selection
                tracking = false;
                this.Capture = false;
                Invalidate( );
            }
        }

        // On mouse move
        private void Histogram_MouseMove( object sender, System.Windows.Forms.MouseEventArgs e )
        {
            if ( ( allowSelection ) && ( values != null ) )
            {
                int x = ( ClientRectangle.Right - width ) / 2;
                int y = 5;

                if ( !tracking )
                {
                    if ( ( e.X >= x ) && ( e.Y >= y ) && ( e.X < x + width ) && ( e.Y < y + height ) )
                    {
                        over = true;

                        // moving over
                        this.Cursor = Cursors.Cross;

                        // notify parent
                        if ( PositionChanged != null )
                            PositionChanged( this, new HistogramEventArgs( e.X - x ) );
                    }
                    else
                    {
                        this.Cursor = Cursors.Default;

                        if ( over )
                        {
                            over = false;

                            // notify parent
                            if ( PositionChanged != null )
                                PositionChanged( this, new HistogramEventArgs( -1 ) );
                        }
                    }
                }
                else
                {
                    // selecting region
                    stop = e.X - x;

                    stop = Math.Min( stop, 255 );
                    stop = Math.Max( stop, 0 );

                    Invalidate( );

                    // notify parent
                    if ( SelectionChanged != null )
                        SelectionChanged( this, new HistogramEventArgs( Math.Min( start, stop ), Math.Max( start, stop ) ) );
                }
            }
        }

        // On mouse leave
        private void Histogram_MouseLeave( object sender, System.EventArgs e )
        {
            if ( ( allowSelection ) && ( values != null ) && ( !tracking ) )
            {
                // notify parent
                if ( PositionChanged != null )
                    PositionChanged( this, new HistogramEventArgs( -1 ) );
            }
        }
    }
}

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...

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