2015年12月31日 星期四

C# - 影像處理圖片灰階、黑白、邊緣化、B-Channel

練習濾鏡(Filter)特效:
  • 灰階 : Grayscale()
  • 黑白(二階):Grayscale() → OtsuThreshold()
  • 邊緣化 : Grayscale() → OtsuThreshold() → CannyEdgeDetector() 
  • B-Channel → ExtractChannel(RGB.B)
##ReadMore##

Step1. 檔案 → 新增專案 → Windows Form 應用程式 → 拉物件 Form1.cs[設計] → 撰寫 Form1.cs

Step2. 方案總管 → 參考 → 右鍵加入參考 → 瀏灠(預設路徑) C:\Program Files (x86)\AForge.NET\Framework\Release → 加入參考
AForge.Imaging.dll
AForge.Imaging.Formats.dll

Step3. Windows Form 拉入需要工具:
  • pictureBox × 2
  • button × 6
  • openFileDialog × 1 
  • saveFileDialog × 1

Step4. coding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AForge.Imaging.Formats;
using AForge.Imaging.Filters;
using AForge.Imaging;

namespace Lesson_p90
{
    public partial class Form1 : Form
    {
        Bitmap img1, img2;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "所有檔案(*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                img1 = ImageDecoder.DecodeFromFile(openFileDialog1.FileName);
                pictureBox1.Image = img1;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            img2 = filter.Apply(img1);
            pictureBox2.Image = img2;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Grayscale grayfilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap grayImg = grayfilter.Apply(img1);

            OtsuThreshold filter = new OtsuThreshold();
            img2 = filter.Apply(grayImg);
            pictureBox2.Image = img2;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Grayscale grayfilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap edgeImg = grayfilter.Apply(img1);

            OtsuThreshold filter = new OtsuThreshold();
            filter.ApplyInPlace(edgeImg);

            CannyEdgeDetector filter2 = new CannyEdgeDetector();
            img2 = filter2.Apply(edgeImg);
            pictureBox2.Image = img2;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ExtractChannel filter = new ExtractChannel(RGB.B);
            img2 = filter.Apply(img1);
            pictureBox2.Image = img2;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = @"imgone.bmp";
            saveFileDialog1.Filter = "Bitmap file(*.bmp)|*.bmp";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                img2.Save(saveFileDialog1.FileName);
            }
        }
    }
}

沒有留言:

張貼留言