2016年6月2日 星期四

C#-examination-c

題目要求:
算板手(p5)的面積與中間寬度
圖片:p5.jpg

##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;
using AForge.Math;

namespace examination_c
{
    public partial class Form1 : Form
    {
        Bitmap srcImg, tempImg;
        int x = 400, y1 = 100, y2 = 500;

        public Form1()
        {
            InitializeComponent();
        }

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

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

            OtsuThreshold otsuFilter = new OtsuThreshold();
            tempImg = otsuFilter.Apply(tempImg);

            Invert invFilter = new Invert();
            tempImg = invFilter.Apply(tempImg);

            FillHoles holeFilter = new FillHoles();
            holeFilter.MaxHoleHeight = 100;
            holeFilter.MaxHoleWidth = 100;
            holeFilter.CoupledSizeFiltering = false;
            tempImg = holeFilter.Apply(tempImg);

            pictureBox2.Image = tempImg;
        }

        private void button3_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 button4_Click(object sender, EventArgs e)
        {
            Bitmap imgtwo = new Bitmap(tempImg);
            Graphics g2 = Graphics.FromImage(imgtwo);

            int[,] point = new int[x + 5, y2 + 5];
            int count = 0;
            int[] value = new int[2];

            for (int y = y1; y < y2; y++)
            {
                Color pixelColor = imgtwo.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])
                    {
                        value[count] = y;
                        count++;
                    }
                }
            }

            listBox1.Items.Add("第一點座標 = (" + x + ", " + value[0] + ")");
            listBox1.Items.Add("第二點座標 = (" + x + ", " + value[1] + ")");
            listBox1.Items.Add("板手寬度 = " + (value[1] - value[0]).ToString());
            g2.DrawLine(new Pen(Color.Red), new Point(x, value[0]), new Point(x, value[1]));
            pictureBox2.Image = imgtwo;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // create filter
            BlobsFiltering filter = new BlobsFiltering();
            // configure filter
            filter.CoupledSizeFiltering = true;
            filter.MinWidth = 1;
            filter.MinHeight = 1;
            // apply the filter
            Bitmap dstImage = filter.Apply(tempImg);
            //pictureBox2.Image = dstImage;


            BlobCounterBase bc = new BlobCounter();
            bc.FilterBlobs = true;
            bc.MinWidth = 1;
            bc.MinHeight = 1;
            bc.ObjectsOrder = ObjectsOrder.Area;

            bc.ProcessImage(dstImage); // process binary image
            Blob[] blobs = bc.GetObjectsInformation();

            textBox1.Text = "Blob Count = " + bc.ObjectsCount.ToString();//總個數

            int totalarea = 0;
            for (int ii = 0; ii < bc.ObjectsCount; ii++)
            {
                totalarea = totalarea + blobs[ii].Area;
                listBox2.Items.Add("Blob[" + ii + "] = " + blobs[ii].Area.ToString());
            }
            listBox2.Items.Add("Total Area = " + totalarea);
        }
    }
}

沒有留言:

張貼留言