2016年2月22日 星期一

影像處理(二值化、斷開、閉合)

成果

程式碼
<Window x:Class="Opening_and_Closing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Opening_and_Closing"
        mc:Ignorable="d"
        Title="MainWindow" Height="562" Width="1247">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label Content="二值化" Grid.Row="0" Grid.Column="0" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <Label Content="斷開" Grid.Row="0" Grid.Column="1" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <Label Content="閉合" Grid.Row="0" Grid.Column="2" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <Image x:Name="img_threshold" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image x:Name="img_open" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image x:Name="img_close" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </Grid>
</Window>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.Drawing;
using System.IO;

namespace Opening_and_Closing
{
    /// &ltsummary&gt
    /// MainWindow.xaml 的互動邏輯
    /// &lt/summary&gt
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //圖片路徑
            string contentDir = Directory.GetCurrentDirectory();
            int dirLength = contentDir.Length;
            if (contentDir.ToLower().EndsWith(@"\bin\x64\debug"))
                contentDir = contentDir.Remove(dirLength - 14, 14);
            else if (contentDir.ToLower().EndsWith(@"\bin\release"))
                contentDir = contentDir.Remove(dirLength - 12, 12);
            string path = contentDir + @"\j.png";

            //載入灰階的原圖
            Image&ltGray, Byte&gt grayImage = new Image&ltGray, byte&gt(path);

            //二值化,閥值設定為『灰階的平均值』
            Gray grayAge = grayImage.GetAverage();
            Image&ltGray, Byte&gt thresholdImage = grayImage.ThresholdBinary(grayAge, new Gray(255));
            img_threshold.Source = Emgu.CV.WPF.BitmapSourceConvert.ToBitmapSource(thresholdImage);

            //斷開Open
            Mat kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new System.Drawing.Size(5, 5), new System.Drawing.Point(-1, -1));
            Image&ltGray, Byte&gt openImage = thresholdImage.MorphologyEx(MorphOp.Open, kernel, new System.Drawing.Point(-1, -1), 2, BorderType.Default, new MCvScalar(255, 0, 0, 255));
            //Image&ltGray, Byte&gt openImage = thresholdImage.Dilate(1).Erode(1);
            img_open.Source = Emgu.CV.WPF.BitmapSourceConvert.ToBitmapSource(openImage);

            //斷開Open
            Image&ltGray, Byte&gt closeImage = thresholdImage.MorphologyEx(MorphOp.Close, kernel, new System.Drawing.Point(-1, -1), 2, BorderType.Default, new MCvScalar(255, 0, 0, 255));
            img_close.Source = Emgu.CV.WPF.BitmapSourceConvert.ToBitmapSource(closeImage);
        }
    }
}

沒有留言:

張貼留言