2013年1月30日 星期三

【AForge.NET】灰階做直方圖平均化Equalization

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;

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 GrayImage = CamImage.Clone() as Bitmap;//複製影像

            pictureBox1.Image = CamImage;
            RGBtoGrayImage(GrayImage);//轉灰階再做值方圖平均化影像    
                
        }

        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 RGBtoGrayImage(Bitmap SrcImg)
        {
            Grayscale grayscaleFilter = new Grayscale(0.299, 0.587, 0.114);
            //設定一個灰階過濾鏡
            Bitmap GrayImage = grayscaleFilter.Apply(SrcImg);//執行灰階過濾鏡
            pbGrayscale.Image = GrayImage;
            Bitmap EqualizeImage = GrayImage.Clone() as Bitmap;
           
            Rectangle rect = new Rectangle(0, 0, EqualizeImage.Width, EqualizeImage.Height);            
            BitmapData GrayData = EqualizeImage.LockBits(rect, ImageLockMode.ReadWrite,
                EqualizeImage.PixelFormat);//建立影像的BitmapData型態
            HistogramEqualization Equalizefilter = new HistogramEqualization( );
            //設定一個直方圖平均化濾鏡
            
            Equalizefilter.ApplyInPlace(GrayData);
            EqualizeImage.UnlockBits(GrayData);//解除BitmapData型態
            
            pictureBox2.Image = EqualizeImage;            
        }
    }
}

0 意見:

張貼留言