You are here: Start » AVL.NET » Aurora 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AvlNet;
using AvlNet.Designers;

namespace Designers
{
    public partial class Form1 : Form
    {
        Region roi = new Region();
        /// <summary>
        /// image being thresholded
        /// </summary>
        Image image = new Image();
        
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Loads an image
        /// </summary>
        private void image_Click(object sender, EventArgs e)
        {

            if (sender == lenaRgbBtn)
            {
                AVL.TestImage(TestImageId.Lena, image, null);
            }
            else if (sender == fromFileBtn)
            {
                if (openImageDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        AVL.LoadImage(openImageDialog.FileName, false, image);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Loading error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            if (roi.FrameHeight > image.Height || roi.FrameWidth > image.Width)
            {
                using (var croppedRoi = new Region())
                {
                    AVL.CropRegion(
                        roi,
                        new Box(0, 0,
                            Math.Min(roi.FrameWidth, image.Width),
                            Math.Min(roi.FrameHeight, image.Height)),
                        croppedRoi);
                    roi.Reset(croppedRoi);
                }
            }

            UpdatePreview();
            UpdateThresholdResult();
        }

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

            // create an empty image to be filled in the ThresholdImage function
            using (var thresholdedImage = new Image())
            {
                AVL.ThresholdImage(image, roi, minTrackBar.Value, maxTrackBar.Value, 1.0f, thresholdedImage);

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

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

            using (var miniature = new Image(image))
            {
                if (roi != null)
                    AVL.DrawRegion(miniature, roi, Pixel.Blue, 0.5f);

                if (imagePreview.Image != null)
                    imagePreview.Image.Dispose();

                imagePreview.Image = miniature.CreateBitmap();
            }
        }

        /// <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 (var designer = new RegionDesigner())
            {
                if (image != null)
                    designer.Backgrounds = new Image[] { image };

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

            UpdatePreview();
            UpdateThresholdResult();
        }

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