2016年9月18日 星期日

如何對視訊(連續影像)做不同色彩空間轉換_去噪技巧_膚色偵測_開關切換的程式邏輯_流程圖設計

簡易型(基礎)介面





基礎(簡易)設計流程圖架構

練習一下  畫流程圖喔!!!

這裡我只是簡易示範




代碼示例 :


第一階段 _ 解決Camera開啟

效果示例

代碼


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;

namespace emgucv_HSV_webcam
{
    public partial class Form1 : Form
    {
        #region Globle Var
        private Capture capture = new Capture();
        Image<Bgr, Byte> ImageFrame = new Image<Bgr, Byte>(320, 240);
        #endregion
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            Application.Idle += ProcessFrame;
        }

        private void ProcessFrame(object sender, EventArgs e)
        {
            ImageFrame = capture.QueryFrame();
            ImageFrame = ImageFrame.Resize(pictureBox1.Width, pictureBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
            pictureBox1.Image = ImageFrame.ToBitmap();
        }
    }
}



第二階段 _ 不同色彩空間轉換


HSV 轉換

想像你家浴室的按鈕 (開/關)  ----> 正/反  去做 切換




這裡幫大家測出了  比較好的 針對黑色物件(體)的

低門檻值
//Hue:88,Satuation:0,value:11
高門檻值
//Hue:150,Satuation:255,value:255    


HSV部分做了三種效果

原始、做完中值濾波、做完平滑濾波的視訊效果

效果示例








using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;

namespace emgucv_HSV_webcam
{
    public partial class Form1 : Form
    {
        #region Globle Var
        private Capture capture = new Capture();
        Image<Bgr, Byte> ImageFrame = new Image<Bgr, Byte>(320, 240);
        Image<Hsv, byte> imgHsv = new Image<Hsv, Byte>(320, 240);
        private bool hsvButton;
        private bool RgbButton;
        private bool YCbCrButton;
        #endregion
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            Application.Idle += ProcessFrame;
            RgbButton = true;
            hsvButton = false;
        }
        
        private void ProcessFrame(object sender, EventArgs e)
        {
            //flag = 0;
            if (RgbButton)
            {
                ImageFrame = capture.QueryFrame();
                ImageFrame = ImageFrame.Resize(pictureBox1.Width, pictureBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
                pictureBox1.Image = ImageFrame.ToBitmap();
            }
            
            if(hsvButton)
            {
                imgHsv = capture.QueryFrame().Convert<Hsv,byte>();
                imgHsv = imgHsv.Resize(pictureBox1.Width, pictureBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
                //可以追蹤黑色物件
                Hsv lowerLimit = new Hsv(88, 0, 11); //Hue:88,Satuation:0,value:11
                Hsv upperLimit = new Hsv(150, 255, 255); //Hue:150,Satuation:255,value:255                
                imgHsv.Resize(pictureBox1.Width, pictureBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
                //imgHsv.SmoothGaussian(7);

                //原始HSV
                //pictureBox1.Image = imgHsv.InRange(lowerLimit, upperLimit).ToBitmap();

                //做完中值濾波(非線性濾波)後,必須是大於1的正奇數 ---> 去除雜訊常用
                pictureBox1.Image = imgHsv.InRange(lowerLimit, upperLimit).SmoothMedian(3).ToBitmap();

                //做完3*3平滑 ---> 平均平滑
                //將每個像素替換為相鄰矩形內像素的平均值,以3×3大小的核心來說,每個像素權重是1/9
                //用這些像素的加總,替換當前的像素值
                //pictureBox1.Image = imgHsv.InRange(lowerLimit, upperLimit).SmoothBlur(3,3).ToBitmap(); 


                //Image<Gray, byte> imgGray = capture.QueryFrame().Convert<Gray, byte>();
                //pictureBox1.Image = imgGray.ToBitmap();
            }
        }

        private void btnHSV_Click(object sender, EventArgs e)
        {
            hsvButton = true;
            RgbButton = false;
        }
    }
}




代碼示例:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;

namespace emgucv_HSV_webcam
{
    public partial class Form1 : Form
    {
        #region Globle Var
        private Capture capture = new Capture();
        Image<Bgr, Byte> ImageFrame = new Image<Bgr, Byte>(320, 240);
        Image<Hsv, byte> imgHsv = new Image<Hsv, Byte>(320, 240);
        //Image<Ycc, byte> imgYcc = new Image<Ycc, byte>(320, 240);
        private bool hsvButton;
        private bool RgbButton;
        private bool YCbCrButton;
        #endregion
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            Application.Idle += ProcessFrame;
            RgbButton = true;
            hsvButton = false;            
            YCbCrButton = false;
        }
        
        private void ProcessFrame(object sender, EventArgs e)
        {
            //flag = 0;
            if (RgbButton)
            {
                ImageFrame = capture.QueryFrame();
                ImageFrame = ImageFrame.Resize(pictureBox1.Width, pictureBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
                pictureBox1.Image = ImageFrame.ToBitmap();
            }
            
            if(hsvButton)
            {
                imgHsv = capture.QueryFrame().Convert<Hsv,byte>();
                imgHsv = imgHsv.Resize(pictureBox1.Width, pictureBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
                //可以追蹤黑色物件
                Hsv lowerLimit = new Hsv(88, 0, 11); //Hue:88,Satuation:0,value:11
                Hsv upperLimit = new Hsv(150, 255, 255); //Hue:150,Satuation:255,value:255                
                imgHsv.Resize(pictureBox1.Width, pictureBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
                //imgHsv.SmoothGaussian(7);

                //原始HSV
                //pictureBox1.Image = imgHsv.InRange(lowerLimit, upperLimit).ToBitmap();

                //做完中值濾波(非線性濾波)後,必須是大於1的正奇數 ---> 去除雜訊常用
                pictureBox1.Image = imgHsv.InRange(lowerLimit, upperLimit).SmoothMedian(3).ToBitmap();

                //做完3*3平滑 ---> 平均平滑
                //將每個像素替換為相鄰矩形內像素的平均值,以3×3大小的核心來說,每個像素權重是1/9
                //用這些像素的加總,替換當前的像素值
                //pictureBox1.Image = imgHsv.InRange(lowerLimit, upperLimit).SmoothBlur(3,3).ToBitmap(); 


                //Image<Gray, byte> imgGray = capture.QueryFrame().Convert<Gray, byte>();
                //pictureBox1.Image = imgGray.ToBitmap();
            }
            if (YCbCrButton)
            {
                Image<Ycc, byte> imgYcc = capture.QueryFrame().Convert<Ycc, byte>();
                imgYcc  = imgYcc.Resize(pictureBox1.Width, pictureBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
                //YCRCB膚色範圍
                //134 < Cr < 176 
                //97 < Cb < 142
                //80 < Y
                //Ycc(double y , double Cr , double Cb)
                Ycc lowerLimit = new Ycc(80, 134, 97);
                Ycc upperLimit = new Ycc(255, 176, 142);
                //在此範圍的 output 白色為所要之顏色,黑色為不落在此範圍之顏色

                pictureBox1.Image = imgYcc.InRange(lowerLimit,upperLimit).ToBitmap();
            }
        }

        private void btnHSV_Click(object sender, EventArgs e)
        {
            RgbButton = false;
            hsvButton = true;            
            YCbCrButton = false;
        }

        private void btnYCbCr_Click(object sender, EventArgs e)
        {
            RgbButton = false;
            hsvButton = false;            
            YCbCrButton = true;
        }
    }
}



最後一個是 灰階單通道

比較簡單就自己做吧~~


沒有留言:

張貼留言