5. 2 維卷積

namespace Q5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        TextBox[,] tbi = new TextBox[7, 7];
        TextBox[,] tbk = new TextBox[3, 3];
        TextBox[,] tbo = new TextBox[7, 7];

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int y = 0; y < 7; y++)
            {
                {
                    var lbl = new Label();
                    lbl.Text = y.ToString();
                    lbl.Left = y * 60 + 60;
                    lbl.Top = 20;
                    lbl.Size = new Size(20, 20);
                    groupBox1.Controls.Add(lbl);
                    groupBox3.Controls.Add(lbl);
                }

                {
                    var lbl = new Label();
                    lbl.Text = y.ToString();
                    lbl.Top = y * 60 + 40;
                    lbl.Left = 20;
                    lbl.Size = new Size(20, 20);
                    groupBox1.Controls.Add(lbl);
                    groupBox3.Controls.Add(lbl);
                }

                for (int x = 0; x < 7; x++)
                {
                    {
                        var tb = new TextBox();
                        tb.TextAlign = HorizontalAlignment.Center;
                        tbi[x, y] = tb;
                        tb.Left = x * 60 + 40;
                        tb.Top = y * 60 + 40;
                        tb.Size = new Size(50, 50);
                        tb.Text = "0";
                        groupBox1.Controls.Add(tb);
                    }

                    {
                        var tb = new TextBox();
                        tb.TextAlign = HorizontalAlignment.Center;
                        tbo[x, y] = tb;
                        tb.Left = x * 60 + 40;
                        tb.Top = y * 60 + 40;
                        tb.Size = new Size(50, 50);
                        tb.Text = "0";
                        groupBox3.Controls.Add(tb);
                    }
                }
            }

            for (int y = 0; y < 3; y++)
            {
                {
                    var lbl = new Label();
                    lbl.Text = (y - 1).ToString();
                    lbl.Left = y * 60 + 60;
                    lbl.Top = 20;
                    lbl.Size = new Size(20, 20);
                    groupBox2.Controls.Add(lbl);
                }

                {
                    var lbl = new Label();
                    lbl.Text = (y - 1).ToString();
                    lbl.Top = y * 60 + 40;
                    lbl.Left = 20;
                    lbl.Size = new Size(20, 20);
                    groupBox2.Controls.Add(lbl);
                }

                for (int x = 0; x < 3; x++)
                {
                    var tb = new TextBox();
                    tbk[x, y] = tb;
                    tb.Left = x * 60 + 40;
                    tb.Top = y * 60 + 40;
                    tb.Size = new Size(50, 50);
                    tb.Text = "0";
                    groupBox2.Controls.Add(tb);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int WH = 49;

            int[,] I = new int[7, 7];
            int[,] K = new int[3, 3];
            int[,] O = new int[7, 7];

            for (int y = 0; y < 7; y++)
                for (int x = 0; x < 7; x++)
                {
                    I[x, y] = int.Parse(tbi[x, y].Text);
                }

            for (int x = 0; x < 3; x++)
                for (int y = 0; y < 3; y++)
                {
                    K[2 - x, 2 - y] = int.Parse(tbk[x, y].Text);
                }

            for (int x = 1; x < 6; x++)
                for (int y = 1; y < 6; y++)
                {
                    O[x, y] = calc(K, I, x, y);
                    tbo[x, y].Text = O[x, y].ToString();
                }

            double MSE = 0;
            double MAE = 0;

            for (int y = 0; y < 7; y++)
                for (int x = 0; x < 7; x++)
                {
                    MSE += Math.Pow(I[x, y] - O[x, y], 2);
                    MAE += Math.Abs(I[x, y] - O[x, y]);
                }
            MSE /= WH;
            MAE /= WH;

            mse.Text = MSE.ToString();
            mae.Text = MAE.ToString();
            psmr.Text = (10 * Math.Log10(255 * 255 / MSE)).ToString();
        }

        int calc(int[,] k, int[,] c, int xx, int yy)
        {
            int s = 0;
            for (int x = 0; x < 3; x++)
                for (int y = 0; y < 3; y++)
                {
                    s += k[x, y] * c[xx - 1 + x, yy - 1 + y];
                }
            return s;
        }
    }
}

Last updated