2016年6月2日 星期四

C#-examination-b

題目要求:
掃描Bracket1圖片的 (a)兩個寬度與 (b)中間空隙之寬度
圖片:Bracket1.bmp

##ReadMore##


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Imaging.Formats;

namespace examination_b
{
    public partial class Form1 : Form
    {
        Bitmap srcImg, tempImg, bwImg;

        int x = 400, y1 = 100, y2= 400;
        int[] y_value = new int[4];

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            srcImg = ImageDecoder.DecodeFromFile("Bracket1.bmp");
            tempImg = new Bitmap(srcImg);
            pictureBox1.Image = srcImg;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = Graphics.FromImage(srcImg);
            g.DrawLine(new Pen(Color.Red), new Point(x, y1), new Point(x, y2));

            pictureBox1.Image = srcImg;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Grayscale grayFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap grayImg = grayFilter.Apply(tempImg);

            OtsuThreshold otsuFilter = new OtsuThreshold();
            bwImg = otsuFilter.Apply(grayImg);

            pictureBox2.Image = bwImg;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int[,] point = new int[x+5, y2+5];
            int count = 0;

            for (int y = y1; y < y2; y++)
            {
                Color pixelColor = bwImg.GetPixel(x, y);
                point[x, y] = pixelColor.R;
            }

            for (int y = y1; y < y2; y++)
            {
                if (point[x, y] < 20)
                {
                    if (point[x, y] != point[x, y - 1] || point[x, y] != point[x, y + 1])
                    {
                        y_value[count] = y;
                        count++;
                        listBox1.Items.Add("(" + x + ", " + y + ")");
                    }
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Bitmap imgtwo = new Bitmap(bwImg);
            Graphics g2 = Graphics.FromImage(imgtwo);

            g2.DrawLine(new Pen(Color.Yellow), new Point(x, y_value[0]), new Point(x, y_value[1]));
            listBox2.Items.Add("上面寬度(Yellow) = " + (y_value[1] - y_value[0]).ToString());

            g2.DrawLine(new Pen(Color.Purple), new Point(x, y_value[1]), new Point(x, y_value[2]));
            listBox2.Items.Add("中間空隙(Purple) = " + (y_value[2] - y_value[1]).ToString());

            g2.DrawLine(new Pen(Color.Green), new Point(x, y_value[2]), new Point(x, y_value[3]));
            listBox2.Items.Add("上面寬度(Green) = " + (y_value[3] - y_value[2]).ToString());

            pictureBox2.Image = imgtwo;
        }
    }
}

沒有留言:

張貼留言