2. 圖中人物身高體寬量測程式

找出一條全白的x座標

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

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            pictureBox1.Image = Bitmap.FromFile(openFileDialog1.FileName);

            button2.Enabled = true;
            button3.Enabled = true;
            textBox1.Enabled = true;
            textBox2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int my = int.MaxValue, My = int.MinValue;
            int myR = int.MaxValue, MyR = int.MinValue;

            var img = (Bitmap)pictureBox1.Image;

            var middle = 0;
            for (int x = img.Width / 3; x < img.Width * 2 / 3; x++)
            {
                bool allWhite = true;
                for (int y = 0; y < img.Height; y++)
                {
                    var p = img.GetPixel(x, y);
                    var color = p.R * 0.3 + p.G * 0.59 + p.B * 0.11;
                    if (color < 200)
                    {
                        allWhite = false;
                        break;
                    }
                }
                if (allWhite)
                {
                    middle = x;
                    break;
                }
            }

            for (int x = 0; x < middle; x++)
                for (int y = 0; y < img.Height; y++)
                {
                    var p = img.GetPixel(x, y);
                    var color = p.R * 0.3 + p.G * 0.59 + p.B * 0.11;
                    if (color >= 200)
                    {
                        continue;
                    }
                    if (y > My) My = y;
                    if (y < my) my = y;
                }


            for (int x = middle; x < img.Width; x++)
                for (int y = 0; y < img.Height; y++)
                {
                    var p = img.GetPixel(x, y);
                    var color = p.R * 0.3 + p.G * 0.59 + p.B * 0.11;
                    if (color >= 200)
                    {
                        continue;
                    }
                    if (y > MyR) MyR = y;
                    if (y < myR) myR = y;
                }

            var ratio = 830.0 / (My - my);
            textBox1.Text = $"{ratio * (MyR - myR)}";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int my = int.MaxValue, My = int.MinValue;
            int mxR = int.MaxValue, MxR = int.MinValue;

            var img = (Bitmap)pictureBox1.Image;
            var middle = 0;
            for (int x = img.Width / 3; x < img.Width * 2 / 3; x++)
            {
                bool allWhite = true;
                for (int y = 0; y < img.Height; y++)
                {
                    var p = img.GetPixel(x, y);
                    var color = p.R * 0.3 + p.G * 0.59 + p.B * 0.11;
                    if (color < 200)
                    {
                        allWhite = false;
                        break;
                    }
                }
                if (allWhite)
                {
                    middle = x;
                    break;
                }
            }

            for (int x = 0; x < middle; x++)
                for (int y = 0; y < img.Height; y++)
                {
                    var p = img.GetPixel(x, y);
                    var color = p.R * 0.3 + p.G * 0.59 + p.B * 0.11;
                    if (color >= 200)
                    {
                        continue;
                    }
                    if (y > My) My = y;
                    if (y < my) my = y;
                }


            for (int x = middle; x < img.Width; x++)
                for (int y = 0; y < img.Height; y++)
                {
                    var p = img.GetPixel(x, y);
                    var color = p.R * 0.3 + p.G * 0.59 + p.B * 0.11;
                    if (color >= 200)
                    {
                        continue;
                    }
                    if (x > MxR) MxR = x;
                    if (x < mxR) mxR = x;
                }

            var ratio = 830.0 / (My - my);
            textBox2.Text = $"{ratio * (MxR - mxR)}";
        }
    }
}

Last updated