You are here: Start » AVL.NET » Adaptive Vision Library .NET Usage » Designers

Designers

The following example is a simple image thresholding application that allows to threshold an image within the user-defined ROI which can be defined in the RegionDesigner.

The application is a single-form application that uses the RegionDesigner from the Avl.Net.Designers.dll assembly and contains two picture boxes, two track bar controls responsible for setting the thresholds and several buttons that allow to load images:

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


    public partial class Form1 : Form
    {
        Region roi;
        /// <summary>
        /// image being thresholded
        /// </summary>
        Image image;

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Loads an image
        /// </summary>
        private void image_Click(object sender, EventArgs e)
        {
            Image newImage = null;
            Image dummyImage = null;
            if (sender == lenaRgbBtn)
                AVL.TestImage(TestImageId.Lena, out newImage, out dummyImage);
            else if (sender == fromFileBtn)
            {
                if (openImageDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        AVL.LoadImage(openImageDialog.FileName, false, out newImage);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Loading error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            if (newImage != null && image != null && !image.IsDisposed)
                image.Dispose();

            if (newImage != null)
                image = newImage;

            if (dummyImage != null)
                dummyImage.Dispose();

            if (roi != null && (roi.FrameHeight > image.Height || roi.FrameWidth > image.Width))
            {
                AVL.CropRegion(
                    roi,
                    new Box(0, 0,
                        Math.Min(roi.FrameWidth, image.Width),
                        Math.Min(roi.FrameHeight, image.Height)),
                    out roi);
            }

            UpdatePreview();
            UpdateThresholdResult();
        }

        /// <summary>
        /// Performs image thresholding either in defined ROI or of a cropped image.
        /// </summary>
        private void UpdateThresholdResult()
        {
            if (image == null)
            {
                resultPreview.Image = null;
                return;
            }

            Image thresholdedImage = null;

                AVL.ThresholdImage(image, roi, minTrackBar.Value, maxTrackBar.Value, 1.0f, out thresholdedImage);

            if (thresholdedImage != null)
            {
                resultPreview.Image = thresholdedImage.CreateBitmap();
                thresholdedImage.Dispose();
            }
        }

        /// <summary>
        /// Updates loaded image miniature with drawn either a ROI or a box into with image is cropped
        /// </summary>
        private void UpdatePreview()
        {
            if (image == null || image.IsDisposed)
            {
                imagePreview.Image = null;
                return;
            }

            Image miniature = new Image(image);

            if (roi != null)
                AVL.DrawRegion(ref miniature, roi, Pixel.Blue, 0.5f);

            imagePreview.Image = miniature.CreateBitmap();

            miniature.Dispose();
        }

        /// <summary>
        /// Opens a RegionDesigner to edit a ROI within which an image will be thresholded.
        /// </summary>
        private void regionButton_Click(object sender, EventArgs e)
        {
            using (RegionDesigner designer = new RegionDesigner())
            {
                if (image != null)
                    designer.Backgrounds = new Image[] { image };

                designer.Region = roi;
                if (designer.ShowDialog(this) == DialogResult.OK)
                {
                    roi = designer.Region;
                }
            }

            UpdatePreview();
            UpdateThresholdResult();
        }

        private void trackBar_ValueChanged(object sender, EventArgs e)
        {
            UpdateThresholdResult();
        }
Previous: Blob Analysis Next: Settings example