Wednesday, October 23

Emgu Convert Colour image to Gray,Binary Image

พอดีมีคนถามหาโค๊ด ซึ่งเป็นโค๊ดที่ผมเริ่มศึกษา Emgu ใหม่ๆ (ตอนนี้ ก็ยังใหม่อยู่เหมือนเดิม ;P ) ก็เลยเอามาอัพไว้ที่บล๊อกสักหน่อย

ไม่มีอะไรมากครับ เป็นการโหลด Image เข้ามา แล้วทำการแปลงเป็นภาพ Gray , Binary Image เฉยๆ

Emgu Convert Colour image to Gray,Binary Image

ไลบรารี่ที่ดึงเข้ามาใช้ในโปรเจค

Emgu Convert Colour image to Gray,Binary Image

อันนี้เป็นโค๊ด

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 System.IO;
using Emgu.CV;
using Emgu.CV.Structure;


namespace CovertImage
{
    public partial class Form1 : Form
    {
        string imageFileName;
        Image<Bgr, Byte> imageCoverted;
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string extension;
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
            openFile.FilterIndex = 1;
            openFile.RestoreDirectory = true;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                extension = Path.GetExtension(openFile.FileName);
                if (extension == ".jpg" | extension == ".gif" | extension == ".bmp")
                {
                    imageFileName = openFile.FileName;
                    Image<Bgr, byte> img = new Image<Bgr, byte>(openFile.FileName);
                    imageCoverted = new Image<Bgr, Byte>(openFile.FileName);
                    pictureBox1.Image = img.ToBitmap(pictureBox1.Width, pictureBox1.Height);
                    pictureBox2.Image = null;
                }
                else
                {
                    pictureBox1.Image = null;
                    pictureBox2.Image = null;
                    MessageBox.Show("File does not supported");
                }

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            processImage(imageCoverted, 0);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            processImage(imageCoverted, 1);
        }

        private void processImage(Image<Bgr,byte> img,int Mode)
        {
            switch (Mode)
            {
                case 0:
                    label2.Text = "Gray";
                    pictureBox2.Image = img.Convert<Gray, byte>().ToBitmap(pictureBox2.Width, pictureBox2.Height);
                    break;
                case 1:
                    label2.Text = "Binary";
                    pictureBox2.Image = img.Convert<Gray, Byte>().ThresholdBinary(new Gray(127), new Gray(255)).ToBitmap(pictureBox2.Width, pictureBox2.Height);
                    break;
            }

        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://vs-visual-studio.blogspot.com/");
        }
    }
}

ดาวน์โหลดซอร์สโค๊ดและโปรเจค https://dl.dropboxusercontent.com/u/65353188/CovertImage.zip

2 ความคิดเห็น:

J_Paykan on 25 January 2016 at 19:04 said...

ผมแอดไฟล์ .jpg เข้ามาแต่ขึ้นเออเร่อก่อนครับ แก้ยังไงอ่ะคับ
ปล.ผมพึ่งฝึกเขียนภาษา C# นะครับ

J_Paykan on 25 January 2016 at 19:07 said...

เออเรอบอกว่า
TypelnitializationException was unhandled
An unhandled exception of type 'System.TypelnitializationException' occurred in Emgu.CV.dll

Post a Comment

 

แจกฟรี พื้นที่ฝากไฟล์ 2 GB

ads

ติดตามข่าวสารผ่าน Twitter

ติดตาม Blog นี้

Blog อื่นๆของฉัน

  • Test AMR Fleet Management program - ระบบบริหารจัดการจราจรและจัดคิวงานของรถ AMR (Autonomous Mobile Robot) ที่พัฒนาโดยบริษัท WP Solution Co., Ltd. จากภาษา C# ทั้งในส่วนของ Backend (Service app...
  • Gearset matching 2021 program - เป็นงานใหญ่ที่เพิ่งจบไป ที่ระยอง ปลวกแดง บริษัทผลิตชิ้นส่วนรถยนต์ คอนเซปต์ คือต้องการเก็บค่าหลังจากการทดสอบชิ้นส่วยรถยนต์ เพื่อเก็บไว้ในฐานข้อมูลให้แผนกถ...
  • RT ToolBox3 Standard version 1.70Y - กำลังศึกษาการใช้งานโปรแกรม RT ToolBox3 ในการเขียนโปรแกรมเพื่อควบคุมแขนกล Robots-MELFA ของค่าย Mitsubishi Facebook link
  • Free Screen Capture โปรแกรมบันทึกหน้าจอ ฟรีๆ - หากเพื่อนๆ จะมองหาโปรแกรม ที่ไว้สำหรับบันทึกหน้าจอคอมพิวเตอร์ ไม่ว่าจะบันทึกในรูปของภาพนีิ่ง (Screen shot) หรือแบบบันทึกเป็นภาพเคลื่อนไหว ( screencas...

จำนวนการเยี่ยมชมบล๊อก