Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法

本文详细介绍了图像统计滤波的实现过程,包括如何通过调整阈值来优化滤波效果,以及具体步骤如计算窗口内像素值的平均值、标准差,并基于这些统计结果进行图像处理。


[函数名称]

  图像统计滤波   WriteableBitmap StatisticalFilter(WriteableBitmap src,double T)

/// <summary>
        /// Statistical filtering process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <param name="T">The threshould to adjust filter effect.</param>
        /// <returns></returns>
        public static WriteableBitmap StatisticalFilter(WriteableBitmap src,double T)////图像统计滤波
        {
            if (src != null)
            {
                int w = src.PixelWidth;
                int h = src.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[] temp = src.PixelBuffer.ToArray();
                byte[] tempMask = (byte[])temp.Clone();
                double mean = 0;
                double std = 0;
                int[] windowValue = new int[9];
                double mR = 0;
                double mG = 0;
                double mB = 0;
                for (int j = 1; j < h-1; j++)
                {
                    for (int i = 1; i < w-1; i++)
                    {
                        windowValue[0] = (int)(tempMask[(i - 1) * 4 + (j - 1) * w * 4] * 0.114 + tempMask[(i - 1) * 4 + 1 + (j - 1) * w * 4] * 0.587 + tempMask[(i - 1) * 4 + 2 + (j - 1) * w * 4] * 0.299);
                        windowValue[1] = (int)(tempMask[i * 4 + (j - 1) * w * 4] * 0.114 + tempMask[i * 4 + 1 + (j - 1) * w * 4] * 0.587 + tempMask[i * 4 + 2 + (j - 1) * w * 4] * 0.299);
                        windowValue[2] = (int)(tempMask[(i + 1) * 4 + (j - 1) * w * 4] * 0.114 + tempMask[(i + 1) * 4 + 1 + (j - 1) * w * 4] * 0.587 + tempMask[(i + 1) * 4 + 2 + (j - 1) * w * 4] * 0.299);
                        windowValue[3] = (int)(tempMask[(i-1) * 4 + j * w * 4] * 0.114 + tempMask[(i-1) * 4 + 1 + j * w * 4] * 0.587 + tempMask[(i - 1) * 4 + 2 + j * w * 4] * 0.299);
                        windowValue[4] = (int)(tempMask[i * 4 + j * w * 4] * 0.114 + tempMask[i * 4 + 1 + j * w * 4] * 0.587 + tempMask[i * 4 + 2 + j * w * 4] * 0.299);
                        windowValue[5] = (int)(tempMask[(i + 1) * 4 + j * w * 4] * 0.114 + tempMask[(i + 1) * 4 + 1 + j * w * 4] * 0.587 + tempMask[(i + 1) * 4 + 2 + j * w * 4] * 0.299);
                        windowValue[6] = (int)(tempMask[(i - 1) * 4 + (j + 1) * w * 4] * 0.114 + tempMask[(i - 1) * 4 + 1 + (j + 1) * w * 4] * 0.587 + tempMask[(i - 1) * 4 + 2 + (j + 1) * w * 4] * 0.299);
                        windowValue[7] = (int)(tempMask[i * 4 + (j + 1) * w * 4] * 0.114 + tempMask[i * 4 + 1 + (j + 1) * w * 4] * 0.587 + tempMask[i * 4 + 2 + (j + 1) * w * 4] * 0.299);
                        windowValue[8] = (int)(tempMask[(i + 1) * 4 + (j + 1) * w * 4] * 0.114 + tempMask[(i + 1) * 4 + 1 + (j + 1) * w * 4] * 0.587 + tempMask[(i + 1) * 4 + 2 + (j + 1) * w * 4] * 0.299);
                        for (int n = 0; n < 9; n++)
                        {
                            mean += (double)((double)(windowValue[n]) / 9);
                        }
                        for (int n = 0; n < 9; n++)
                        {
                            std += Math.Pow((double)(windowValue[n]) - mean, 2)/9;
                        }
                        if (windowValue[4] >= T * Math.Sqrt(std))
                        {
                            for (int k = -1; k < 2; k++)
                            {
                                for (int m = -1; m < 2; m++)
                                {
                      mB += (double)tempMask[(i + m) * 4 + (j + k) * w * 4] / 9;
                      mG += (double)tempMask[(i + m) * 4 + 1 + (j + k) * w * 4] / 9;
                      mR += (double)tempMask[(i + m) * 4 + 2 + (j + k) * w * 4] / 9;
                                }
                            }
                            temp[i * 4 + j * w * 4] = (byte)mB;
                            temp[i * 4 + 1 + j * w * 4] = (byte)mG;
                            temp[i * 4 + 2 + j * w * 4] = (byte)mR;
                        }
                        mean = 0;
                        std = 0;
                        mR = mG = mB = 0;
                    }
                }
                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return srcImage;
            }
            else
            {
                return null;
            }
        }

代码介绍 MetroForWinForm(win8风格模版) using System; using System.Drawing; using System.Globalization; using System.Windows.Forms; using MetroFramework.Forms; namespace MetroFramework.Demo { public partial class MainForm : MetroForm { public MainForm() { InitializeComponent(); metroStyleManager.Theme = MetroThemeStyle.Default; metroStyleManager.Style = MetroColorStyle.Teal; } private void metroTileSwitch_Click(object sender, EventArgs e) { var m = new Random(); int next = m.Next(0, 13); metroStyleManager.Style = (MetroColorStyle)next; } private void metroTile1_Click(object sender, EventArgs e) { metroStyleManager.Theme = metroStyleManager.Theme == MetroThemeStyle.Light ? MetroThemeStyle.Dark : MetroThemeStyle.Light; } private void metroButton1_Click(object sender, EventArgs e) { MetroTaskWindow.ShowTaskWindow(this, "SubControl in TaskWindow", new TaskWindowControl(), 10); } private void metroButton2_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "Do you like this metro message box?", "Metro Title", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk); } private void metroButton5_Click(object sender, EventArgs e) { metroContextMenu1.Show(metroButton5, new Point(0, metroButton5.Height)); } private void metroButton6_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` only button", "MetroMessagebox", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void metroButton10_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } private void metroButton7_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes` and `No` button", "MetroMessagebox", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } private void metroButton8_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes`, `No` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); } private void metroButton11_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Retry` and `Cancel` button. With warning style.", "MetroMessagebox", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning); } private void metroButton9_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Abort`, `Retry` and `Ignore` button. With Error style.", "MetroMessagebox", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error); } private void metroButton12_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample `default` MetroMessagebox ", "MetroMessagebox"); } private void metroButton4_Click(object sender, EventArgs e) { var testform = new TestForm1(); testform.ShowDialog(); } private void metroButton4_Click_1(object sender, EventArgs e) { metroTextBox2.Focus(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trent1985

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值