2016年6月2日 星期四

C#-examination-d

題目要求:
使用textbox show出各個blob的中心點座標
圖片:1-1.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.Formats;
using AForge.Imaging.Filters;
using AForge.Math;
using AForge.Video;

namespace examination_d
{
    public partial class Form1 : Form
    {
        Bitmap srcImg;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            srcImg = ImageDecoder.DecodeFromFile("1-1.bmp");
            pictureBox1.Image = srcImg;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int largestarea, ii, blobcount, totalarea, avearea;
            Grayscale grayFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap grayImg = grayFilter.Apply(srcImg);
            pictureBox2.Image = grayImg;

            Threshold thresFilter = new Threshold(254);
            Bitmap bimage = thresFilter.Apply(grayImg);
            pictureBox3.Image = bimage;

            // create an instance of blob counter algorithm
            BlobCounterBase bc = new BlobCounter();
            // set filtering options
            bc.FilterBlobs = true;
            bc.MinWidth = 5;
            bc.MinHeight = 5;
            // set ordering options
            bc.ObjectsOrder = ObjectsOrder.Size;
            // process binary image
            bc.ProcessImage(bimage);
            Blob[] blobs = bc.GetObjectsInformation();
            // extract the biggest blob

            largestarea = blobs[0].Area;
            textBox1.Text = largestarea.ToString();
            blobcount = bc.ObjectsCount;
            textBox2.Text = bc.ObjectsCount.ToString();
            textBox7.Text = blobs[0].CenterOfGravity.ToString();

            totalarea = 0;
            for (ii = 0; ii < blobcount; ii++)
            {
                totalarea = totalarea + blobs[ii].Area;

                listBox1.Items.Add(blobs[ii].Area.ToString());

            }

            for (ii = 0; ii < blobcount; ii++)
            {
                listBox2.Items.Add("blobs[" + ii + "] = (" + Convert.ToInt16(blobs[ii].CenterOfGravity.X).ToString() + ", " + Convert.ToInt16(blobs[ii].CenterOfGravity.Y).ToString() + ")");
            }

            textBox4.Text = totalarea.ToString();

            avearea = totalarea / blobcount;
            textBox5.Text = avearea.ToString();

            Invert filterinv = new Invert();
            Bitmap invimg = filterinv.Apply(bimage);
            pictureBox4.Image = invimg;

            // create filter
            Subtract filtersub = new Subtract(invimg);
            // apply the filter
            Bitmap blobimg = filtersub.Apply(grayImg);
            pictureBox5.Image = blobimg;


            // gather statistics
            ImageStatistics stat = new ImageStatistics(blobimg);
            // get red channel's histogram
            Histogram gray = stat.Gray;
            int blobimgarea = stat.PixelsCountWithoutBlack;
            double blobmean = gray.Mean * blobimg.Height * blobimg.Width / blobimgarea;
            textBox6.Text = blobmean.ToString();

        }

    }
}

沒有留言:

張貼留言