You are here: Start » AVL.NET » Adaptive 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 System.Linq;

using AvlNet;
    //...

    public partial class Form1 : Form
    {
        Image lenaMonoImage;

        public Form1()
        {
            InitializeComponent();

            //prepare reusable lena image that will be thresholded
            Image lenaRGBImage; //dummy TestImage output
            AVL.TestImage(TestImageId.Lena, out lenaRGBImage, out lenaMonoImage);
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            //get thresholded image with AVL function.
            //In the form designer trackBar1 Maximum property is set to 255
            //so the thresholding may be performed in the full range.
            Image thresholdedImage;
            AVL.ThresholdImage(lenaMonoImage, null, null, trackBar1.Value, 1.0f, out 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();

            //dispose AVL image. It's copy is already displayed in the pictureBox.
            thresholdedImage.Dispose();
        }
Previous: Usage Examples Next: Basic Template Matching