You are here: Start » AVL.NET » Aurora Vision Library .NET Usage » Basic Image Thresholding

Basic Image Thresholding

This example thresholds monochromatic TestImage output image using ThresholdImage function and displays it in the picture box. To have the example work, a single form with PictureBox and TrackBar controls is needed. Changing the trackbar position changes the threshold and displays updated result image in the PictureBox.

//...
using AvlNet;
    //...

    public partial class Form1 : Form
    {
        /// <summary>Original image to be thresholded</summary>
        Image lenaMonoImage;

        /// <summary>Thresholded image buffer that will be filled for each given threshold values</summary>
        Image thresholdedImage;

        public Form1()
        {
            InitializeComponent();

            // Create an empty image that will be filled in the TestImage method.
            // That object will be a reusable input for thresholding
            lenaMonoImage = new Image();

            // RGB version is useless in this example thus its calculation can be skipped by passing a null
            AVL.TestImage(TestImageId.Lena, null, lenaMonoImage);
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            // In the form designer trackBar1 Maximum property is set to 255
            // so the thresholding may be performed in the full range.

            // each time the threshold is changed, the same image buffer will be used so there is no need to disposing it
            if (thresholdedImage == null)
                thresholdedImage = new Image();

            // get thresholded image with AVL function.
            AVL.ThresholdImage(lenaMonoImage, null, null, trackBar1.Value, 1.0f, thresholdedImage);

            //dispose previously displayed image
            if (pictureBox1.Image != null)
                pictureBox1.Image.Dispose();

            //create .NET Bitmap that can be displayed by the PictureBox control
            pictureBox1.Image = thresholdedImage.CreateBitmap();
        }
    }
Previous: Usage Examples Next: Basic Template Matching