1

这是一个任务。

我在 C# 中创建了一个非常简单的图像处理程序,它允许用户对灰度图像执行某些操作。

我目前遇到的问题是我需要将多个方法传递给轨迹栏。这将允许跟踪栏更改任何调用方法的阈值。

我已经搜索了谷歌,但似乎对这个问题没有什么帮助。那么有人可以给我一些关于如何做到这一点的建议吗?

我目前拥有的代码是这样的。

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

namespace DIP_START
{


    public partial class Form1 : Form
    {
        public Bitmap original_image, proc_image;
        public int[] bins = new int[256];
        int max;

        public Form1()
        {
            InitializeComponent();
            original_image = null;
            proc_image = null;

        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (original_image != null)
            {
                Graphics g = e.Graphics;
                Rectangle r = new Rectangle(10, 50, original_image.Width, original_image.Height);
                g.DrawImage(original_image, r);


            }

        }

        //                OPEN IMAGE FILE
        /******************************************************************************************/

        private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            // show the openFile dialog box            
            Graphics g = this.CreateGraphics();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                original_image = new Bitmap(openFileDialog1.FileName);

            }

            Rectangle r = new Rectangle(10, 50, original_image.Width, original_image.Height);
            g.DrawImage(original_image, r);
        }
        //                 EXIT APPLICATION
        /************************************************************************************/

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Dispose();
            Application.Exit();
        }


        /***********************************************************************************/
        //           EDGE DETECTION
        /***********************************************************************************/

        /***********************************************************************************/
        //           ROBERTS GRADIENT - WITH THRESHOLD
        /***********************************************************************************/

        private void withoutToolStripMenuItem_Click(object sender, System.EventArgs e)
        {


            Graphics g = this.CreateGraphics();

            int width = original_image.Width - 1;
            int height = original_image.Height - 1;

            int threshold = thresholdBar1.Value;
            thresholdBar1.Visible = true;
            thresholdValueBox.Visible = true;
            string thresholdBarVal = Convert.ToString(thresholdBar1.Value);
            thresholdValueBox.Text = thresholdBarVal;




            Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
            Rectangle r2 = new Rectangle(0, 0, original_image.Width, original_image.Height);

            proc_image = original_image.Clone(r2, PixelFormat.Format8bppIndexed);

            g.DrawImage(original_image, r2);

            BitmapData bmDataOR = original_image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            BitmapData bmDataPR = proc_image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);


            int stride = bmDataOR.Stride;
            // System.IntPtr Scan0 = bmData.Scan0;



            unsafe
            {
                byte* o = (byte*)(void*)bmDataOR.Scan0;
                byte* p = (byte*)(void*)bmDataPR.Scan0;

                byte[] pix_val = new byte[4];

                for (int y = 0; y < height; ++y)
                {
                    for (int x = 0; x < width; ++x)
                    {
                        pix_val[0] = o[y * stride + x];
                        pix_val[1] = o[(y + 1) * stride + x + 1];
                        pix_val[2] = o[(y + 1) * stride + x];
                        pix_val[3] = o[y * stride + x + 1];
                        int new_pix_val = Math.Abs(pix_val[0] - pix_val[1]) + Math.Abs(pix_val[2] - pix_val[3]);

                        if (new_pix_val > threshold)
                        {

                            p[y * stride + x] = 255;


                        }
                        else
                            p[y * stride + x] = 0;
                    }
                }

                proc_image.UnlockBits(bmDataPR);
                original_image.UnlockBits(bmDataOR);

                g.DrawImage(proc_image, r);
                //thresholdBar1.Visible = false;

            }

        }

        /***********************************************************************************/
        //           NEIGHBOURHOOD AVERAGING WITHOUT THRESHOLD
        /***********************************************************************************/

        private void withoutThresholdToolStripMenuItem_Click(object sender, System.EventArgs e)
        {

            Graphics g = this.CreateGraphics();

            int width = original_image.Width - 2;
            int height = original_image.Height - 2;

            int total_val = 0;
            int avg = 0;


            Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
            Rectangle r2 = new Rectangle(0, 0, original_image.Width, original_image.Height);

            proc_image = original_image.Clone(r2, PixelFormat.Format8bppIndexed);


            BitmapData bmDataOR = original_image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
            BitmapData bmDataPR = proc_image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            int stride = bmDataOR.Stride;



            unsafe
            {
                byte* o = (byte*)(void*)bmDataOR.Scan0;
                byte* p = (byte*)(void*)bmDataPR.Scan0;

                byte[] pix_val = new byte[9];


                for (int y = 0; y < height; ++y)
                {
                    for (int x = 0; x < width; ++x)
                    {
                        pix_val[0] = o[y * stride + x];
                        pix_val[1] = o[y * stride + x + 1];
                        pix_val[2] = o[y * stride + x + 2];
                        pix_val[3] = o[(y + 1) * stride + x];
                        pix_val[4] = o[(y + 1) * stride + x + 1];
                        pix_val[5] = o[(y + 1) * stride + x + 2];
                        pix_val[6] = o[(y + 2) * stride + x];
                        pix_val[7] = o[(y + 2) * stride + x + 1];
                        pix_val[8] = o[(y + 2) * stride + x + 2];

                        for (int i = 0; i < 9; ++i)
                        {

                            total_val = total_val + pix_val[i];

                        }

                        avg = total_val / 9;

                        p[y * stride + x] = (byte)avg;

                        total_val = 0;
                    }
                }

                proc_image.UnlockBits(bmDataPR);
                original_image.UnlockBits(bmDataOR);

                g.DrawImage(proc_image, r);

            }

        }

    }
}

如您所见,轨迹条被硬编码到罗伯茨梯度中,我也需要邻域平均来采用轨迹条。

在 Visual Studio 中,在轨迹栏的事件标题下,我插入了 Roberts Gradient 的方法

4

0 回答 0