2013年2月4日 星期一

【AForge.NET】比較各種平滑化的方法Smoothing


  • AForge有許多平滑化的方法,以下我使用5種不同方法得到的結果,有幾種速度較慢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Math.Random;

namespace CSHARP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public FilterInfoCollection USB_Webcams = null;
        public VideoCaptureDevice Cam = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            USB_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (USB_Webcams.Count > 0)
            {
                button1.Enabled = true;
                Cam = new VideoCaptureDevice(USB_Webcams[0].MonikerString);
                Cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
            }
            else
            {
                button1.Enabled = false;
                MessageBox.Show("No video input device is connected.");
            }
        }

        void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            ResizeBilinear Resizefilter = new ResizeBilinear(320, 240);
            Bitmap CamImage = Resizefilter.Apply((Bitmap)eventArgs.Frame.Clone());
            Bitmap SrcImage = CamImage.Clone() as Bitmap;        

            Bitmap SmoothImage = SrcImage.Clone() as Bitmap;
            AdaptiveSmooth(ref SmoothImage);    

            Bitmap ConservativeSmoothImage = SrcImage.Clone() as Bitmap;
            ConservativeSmooth(ref ConservativeSmoothImage);
            
            Bitmap BilateralSmoothImage = SrcImage.Clone() as Bitmap;
            BilateralSmooth(ref BilateralSmoothImage);
            
            Bitmap MedianImage = SrcImage.Clone() as Bitmap;
            MedianSmooth(ref MedianImage);
            
            Bitmap MeanImage = SrcImage.Clone() as Bitmap;
            MeanSmooth(ref MeanImage);

            pictureBox1.Image = CamImage;
            pbGrayscale.Image = SmoothImage;
            pictureBox2.Image = ConservativeSmoothImage;
            pictureBox3.Image = BilateralSmoothImage;
            pictureBox4.Image = MedianImage;
            pictureBox5.Image = MeanImage;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Cam != null)
            {
                if (Cam.IsRunning)
                {
                    Cam.Stop();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Start")
            {
                button1.Text = "Stop";
                Cam.Start();
            }
            else
            {
                button1.Text = "Start";
                Cam.Stop();
            }
        }
        private void AdaptiveSmooth(ref Bitmap SrcImg)
        {
            AdaptiveSmoothing filter = new AdaptiveSmoothing();
            filter.ApplyInPlace(SrcImg);
        }
        private void ConservativeSmooth(ref Bitmap SrcImg)
        {
            ConservativeSmoothing filter = new ConservativeSmoothing( );
            filter.ApplyInPlace( SrcImg );
        }
        
        private void MedianSmooth(ref Bitmap SrcImg)
        {
            Median filter = new Median( );
            filter.ApplyInPlace( SrcImg );
        }
        
        private void MeanSmooth(ref Bitmap SrcImg)
        {
            Mean filter = new Mean( );
            filter.ApplyInPlace( SrcImg );
        }
        private void BilateralSmooth(ref Bitmap SrcImg)
        {
            BilateralSmoothing filter = new BilateralSmoothing( );
            filter.KernelSize    = 7;
            filter.SpatialFactor = 10;
            filter.ColorFactor   = 60;
            filter.ColorPower    = 0.5;
            filter.ApplyInPlace( SrcImg );
        }
    }
}



0 意見:

張貼留言