Lọc Trung Vị-Source Code

File Input.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace XLA
{
    public partial class Input : Form
    {
        frmMain frmain;
        public Input(frmMain main)
        {
            this.frmain = main;
            InitializeComponent();
        }

        private void Input_Load(object sender, EventArgs e)
        {
            frmain.Enabled = false;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtW.Text != string.Empty)
                {
                    frmain.w = Convert.ToInt32(txtW.Text);
                    if (frmain.w == (frmain.w / 2) * 2)
                    {
                        frmain.w–;
                    }
                    if (frmain.w <= 0 || frmain.w > frmain.rong || frmain.w > frmain.cao)
                    {
                        frmain.w = 3;
                    }
                }
                if (this.txtDelta.Text != string.Empty)
                {
                    frmain.delta = Convert.ToInt32(txtDelta.Text);
                    if (frmain.delta <= 1 || frmain.delta > 255)
                    {
                        frmain.delta = 3;
                    }
                }
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show(“Ngưỡng và delta phải là số nguyên”, “Error =.=!”);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Input_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmain.Enabled = true;
        }

    }
}

 

 

Main.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace XLA
{
    public partial class frmMain : Form
    {
        Bitmap newbitmap;
        private Bitmap img;
        public int w = 3, delta = 3, rong, cao;
        private int r, g, b;    

        public frmMain()
        {
            InitializeComponent();
        }

      private Bitmap Median_LockBits(Bitmap bm)
        {
            Byte[] mang = new Byte[9];
            Bitmap bitmap = new Bitmap(bm);
            Rectangle rec = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rec, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
            int w = bmpData.Stride;
            IntPtr ptr = bmpData.Scan0;
            int bytes = bmpData.Stride * bitmap.Height;
            Byte[] rgbValues = new Byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            for (int counter = w; counter < rgbValues.Length – w; counter += 4)
            {
                if ((counter % w != 0) && ((counter + 4) % w != 0))
                {
                    mang[0] = rgbValues[counter];
                    mang[1] = rgbValues[counter – 4];
                    mang[2] = rgbValues[counter + 4];
                    mang[3] = rgbValues[counter – w];
                    mang[4] = rgbValues[counter + w];
                    mang[5] = rgbValues[counter – w – 4];
                    mang[6] = rgbValues[counter – w + 4];
                    mang[7] = rgbValues[counter + w – 4];
                    mang[8] = rgbValues[counter + w + 4];

                    Array.Sort(mang);
                    rgbValues[counter] = mang[4];
                    rgbValues[counter + 1] = mang[4];
                    rgbValues[counter + 2] = mang[4];
                }
            }
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
            bitmap.UnlockBits(bmpData);
            return bitmap;

        }

        #region Xử lý file Bitmap đầu vào bằng phương thức Set-GetPixels
        private int[] xuLy(Bitmap img, int jj, int ii, int m)
        {
            int[] gt = new int[w * w];
            int x = 0;
            if (m == 1)
            {
                for (int t = -(w – 1) / 2; t <= (w – 1) / 2; t++)
                {
                    for (int u = -(w – 1) / 2; u <= (w – 1) / 2; u++)
                    {
                        if ((jj + u < 0) || (jj + u > rong – 1) || (ii + t < 0) || (ii + t > cao – 1))
                        {
                            gt[x] = 0;
                            x++;
                            continue;
                        }
                        gt[x] = img.GetPixel(jj + u, ii + t).R;
                        x++;
                    }
                }
            }
            else if (m == 2)
            {
                for (int t = -(w – 1) / 2; t <= (w – 1) / 2; t++)
                {
                    for (int u = -(w – 1) / 2; u <= (w – 1) / 2; u++)
                    {
                        if ((jj + u < 0) || (jj + u > rong – 1) || (ii + t < 0) || (ii + t > cao – 1))
                        {
                            gt[x] = 0;
                            x++;
                            continue;
                        }
                        gt[x] = img.GetPixel(jj + u, ii + t).G;
                        x++;
                    }
                }
            }
            else if (m == 3)
            {
                for (int t = -(w – 1) / 2; t <= (w – 1) / 2; t++)
                {
                    for (int u = -(w – 1) / 2; u <= (w – 1) / 2; u++)
                    {
                        if ((jj + u < 0) || (jj + u > rong – 1) || (ii + t < 0) || (ii + t > cao – 1))
                        {
                            gt[x] = 0;
                            x++;
                            continue;
                        }
                        gt[x] = img.GetPixel(jj + u, ii + t).B;
                        x++;
                    }
                }
            }
            return gt;
        }
        #endregion

        private int[] sapXep(int[] gt)//Sắp xếp ma trận sau khi so sánh với cửa sổ W
        {
            int temp;
            for (int v = 0; v < w * w – 1; v++)
            {
                for (int p = w * w – 1; p > v; p–)
                {
                    if (gt[p] < gt[p – 1])
                    {
                        temp = gt[p];
                        gt[p] = gt[p – 1];
                        gt[p – 1] = temp;
                    }
                }
            }
            return gt;
        }

        private void btnMedian_ItemClick(object sender, ItemClickEventArgs e)
        {                             
            img = newbitmap;
            int val;
            int[] pk = new int[w * w];
            for (int i = 0; i < cao; i++)
            {
                for (int j = 0; j < rong; j++)
                {
                    r = img.GetPixel(j, i).R;
                    pk = sapXep(xuLy(img, j, i, 1));
                    val = pk[(w * w – 1) / 2];
                    if ((r – val > delta) || (val – r > delta))
                    {
                        r = val;
                    }
                    g = img.GetPixel(j, i).G;
                    pk = sapXep(xuLy(img, j, i, 2));
                    val = pk[(w * w – 1) / 2];
                    if ((g – val > delta) || (val – g > delta))
                    {
                        g = val;
                    }
                    b = img.GetPixel(j, i).B;
                    pk = sapXep(xuLy(img, j, i, 3));
                    val = pk[(w * w – 1) / 2];
                    if ((b – val > delta) || (val – b > delta))
                    {
                        b = val;
                    }
                    img.SetPixel(j, i, Color.FromArgb(r, g, b));
                }
            }
            pictureBox2.Image = img;
        }

        private void btnMedianLocksBit_ItemClick(object sender, EventArgs e)
        {
            Image result = Median_LockBits(newbitmap);
            pictureBox2.Image = result;
        }

        #region Xử lý form
        private void frmMain_Load(object sender, EventArgs e)
        {
        }

        private void btnOpen_ItemClick(object sender, EventArgs e)
        {

            OpenFileDialog open = new OpenFileDialog();
            open.Filter = “Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg|All Files|*.*”;
            open.Title = “Choose an Image File”;
            if (open.ShowDialog() == DialogResult.OK)
            {

                //Grayscale GF = new Grayscale(0.2125, 0.7154, 0.0721);
                //Bitmap Sapmle = GF.Apply(newbitmap);
                newbitmap = new Bitmap(open.FileName);
                pictureBox1.Image = newbitmap;
                rong = newbitmap.Width;
                cao = newbitmap.Height;

            }
            MessageBox.Show(“Các phép toán thực hiện bằng phương thức Set-GetPixels nên tốc độ chậm.\n Không nên mở file ảnh quá lớn tránh treo máy !!”);

        }

        private void btnSave_ItemClick(object sender,EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = “JPEG Image (*.jpeg)|*.jpg|Bitmap Image (*.bmp)|*.bmp|Gif Image (*.gif)|*.gif”;
            save.Title = “Save an Image File”;
            save.ShowDialog();
            if (save.FileName != “”)
            {

                System.IO.FileStream fs = (System.IO.FileStream)save.OpenFile();

                switch (save.FilterIndex)
                {
                    case 1:
                        this.pictureBox2.Image.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;

                    case 2:
                        this.pictureBox2.Image.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Bmp);
                        break;

                    case 3:
                        this.pictureBox2.Image.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Gif);
                        break;
                }

                fs.Close();
            }

        }

        private void btnNhieuAnh_ItemClick(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            pictureBox1.Invalidate();
            pictureBox2.Image = null;
            pictureBox2.Invalidate();
        }

        private void btnAbout_ItemClick(object sender, EventArgs e)
        {
            frmInfomation info = new frmInfomation();           
            info.Show();             
        }

        private void btnTacGia_ItemClick(object sender, EventArgs e)
        {
            About about = new About();
            about.Show();
        }

        private void Input_ItemClick(object sender, EventArgs e)
        {
            Input frmInput = new Input(this);
            frmInput.Show();
        }
        #endregion

      
    }
 }
 

   

 

Gửi phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s