WPF:图像处理(*)二值化演示程序及源代码下载

演示程序:

下载地址:

WPFBinarizationDemo.zip

演示程序源代码

MainWindow.xaml

<Window x:Class="Demo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"               
        Title="图像二值化" Height="600" Width="800" Icon="pack://application:,,,/images/ImageProcessing.ico" WindowStartupLocation="CenterScreen" WindowState="Normal">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />            
            <RowDefinition Height="*" />            
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Orientation="Horizontal" Background="CadetBlue">
            <ToolBar Height="40" Width="Auto" HorizontalAlignment="Left" Background="CadetBlue">
                <Button Margin="4,0" Click="buttonFileOpen_Click">
                    <Image Source="pack://application:,,,/images/FileOpen.png" ToolTip="打开目录" />
                </Button>
                <Button Margin="4,0" Click="buttonAbout_Click">
                    <Image Source="pack://application:,,,/images/Info.png" ToolTip="关于" />
                </Button>
            </ToolBar>
            
            <TextBlock Text="测试图像目录:" FontSize="16" Foreground="White" Margin="16,0,0,0" VerticalAlignment="Center" />
            <TextBlock Name="textBlock1" FontSize="16" Foreground="White" VerticalAlignment="Center" />
        </StackPanel>

        <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <StackPanel Name="stackPanel_Showcase" CanHorizontallyScroll="True" Height="208" Orientation="Horizontal" />
        </ScrollViewer>
        
        <Grid Grid.Row="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />                    
            </Grid.ColumnDefinitions>

            <Button Grid.Row="0" Grid.Column="0" FontSize="16" Margin="8,4" Click="Button_Click">
                灰度图像(保存)
            </Button>

            <Button Grid.Row="0" Grid.Column="1" FontSize="16" Margin="8,4" Click="Button_Click">
                大津法二值图像(保存)
            </Button>

            <Button Grid.Row="0" Grid.Column="2" FontSize="16" Margin="8,4" Click="Button_Click">
                迭代法二值图像(保存)
            </Button>

            <Border Grid.Row="1" Grid.Column="0" Margin="4" BorderThickness="4" BorderBrush="DarkGreen">
                <Image  Name="image1" Stretch="Uniform"/>
            </Border>

            <Border Grid.Row="1" Grid.Column="1" Margin="4" BorderThickness="4" BorderBrush="DarkGreen">
                <Image Name="image2" Stretch="Uniform"/>
            </Border>

            <Border Grid.Row="1" Grid.Column="2" Margin="4" BorderThickness="4" BorderBrush="DarkGreen">
                <Image Name="image3" Stretch="Uniform"/>
            </Border>

            <TextBlock Name="OstuTextBlock" Grid.Row="2" Grid.Column="1" FontSize="16" TextAlignment="Center" VerticalAlignment="Center" />
            <TextBlock Name="IterativeTextBlock" Grid.Row="2" Grid.Column="2" FontSize="16" TextAlignment="Center" VerticalAlignment="Center" />
        </Grid>        
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using Splash.Imaging;

namespace Demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        // 获取测试图像目录
        private void buttonFileOpen_Click(object sender, RoutedEventArgs e)
        {   // 文件夹选择对话框
            FolderBrowserDialog dialog = new FolderBrowserDialog();

            // 不显示创建新文件夹按钮
            dialog.ShowNewFolderButton = false;

            // 设置初始目录
            dialog.SelectedPath = AppDomain.CurrentDomain.BaseDirectory;  

            // 获取用户选择文件夹
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {   // 显示用户选择文件夹
                textBlock1.Text = dialog.SelectedPath;
                textBlock1.ToolTip = textBlock1.Text;
                
                // 获取所有图像文件列表
                String[] images = Folder.GetImages(textBlock1.Text, SearchOption.TopDirectoryOnly);
                if (images != null)
                {   // 更新图像显示列表区
                    Folder.DisplayImages(stackPanel_Showcase, images, ImageMouseDown);
                }
            }
        }       

        // 图像框鼠标点击事件处理
        private void ImageMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {   // 获取图像控件
                Image image = sender as Image;
                if (image == null) return;

                Border SelectedBorder = image.Parent as Border;
                StackPanel panel = SelectedBorder.Parent as StackPanel;
                foreach(Border item in panel.Children)
                {
                    if (item == SelectedBorder)
                    {   // 红色边框
                        item.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
                    }
                    else
                    {   // 墨绿边框
                        item.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.DarkGreen);
                    }
                }

                // 获取位图图像
                BitmapImage bitmap = image.Source as BitmapImage;
                if (bitmap == null) return;

                // 显示灰度图像
                image1.Source = bitmap.ToGrayBitmap();

                // 显示大津法二值化图像
                Int32 Threshold;
                image2.Source = bitmap.ToBinaryBitmap(BinarizationMethods.Otsu, out Threshold);
                OstuTextBlock.Text = Threshold.ToString();

                // 显示迭代法二值化图像
                image3.Source = bitmap.ToBinaryBitmap(BinarizationMethods.Iterative, out Threshold);
                IterativeTextBlock.Text = Threshold.ToString();
            }
        }

        private void buttonAbout_Click(object sender, RoutedEventArgs e)
        {
            About AboutWindow = new About();
            AboutWindow.Owner = this;
            AboutWindow.ShowDialog();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            BitmapSource bitmap = null;
            System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;            
            if ((String)button.Content == "灰度图像(保存)")
            {
                bitmap = image1.Source as BitmapSource;
            }
            else if ((String)button.Content == "大津法二值图像(保存)")
            {
                bitmap = image2.Source as BitmapSource;
            }
            else if ((String)button.Content == "迭代法二值图像(保存)")
            {
                bitmap = image3.Source as BitmapSource;
            }

            if (bitmap == null) return;
            
            // 保存图像
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "图片(*.jpg)|*.jpg";
            dialog.Title = "保存图片";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                
                FileStream fileStream = new FileStream(dialog.FileName, FileMode.Create, FileAccess.Write);                
                encoder.Save(fileStream);                
                fileStream.Close();

                System.Windows.MessageBox.Show("保存成功!"); 
            }  
        }
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
水印的嵌入与提取 fft()实现一维信号和二维信号的FFT(快速傅里叶变换),ifft()分别实现一维信号和二维信号的IFFT(逆向快速傅里叶变换)。下面以256×256 的灰度图像lena 为原始宿主图像、以32×32 的二值图像flag 为水印图像为例,给出利用MATLAB 实现数字水印的过程。 水印攻击实验 由于数字水印在实际应用中可能会遭到各种各样的攻击,因此对算法进行攻击测试是衡量一个水印算法优劣的重要手段。 JPEG 压缩实验 首先对嵌入水印后的图像进行JPEG(Quality=45),而后从压缩的图像中提取出水印,如图5 所示。从图中可以看到DCT 域的水印算法抵抗JPEG 压缩攻击的效果是比较好的。imwrite()函数中的jpg 和quality 参数能对图像进行可控jpg压缩 噪声实验 加入噪声是对水印鲁棒性考验的一种常见的攻击。本实验是在嵌入水印的图像中分别加入均值为0,方差为0.002 的高斯噪声和强度为0.02 的椒盐噪声,并从中提取水印,如图6 所示。imnoise()函数可以对图像加入各种噪声,如白噪声、椒盐噪声等 结论 嵌入水印信息后,原图与嵌入水印信息后的图像在视觉效果上没有明显分别,用肉眼几乎分辨不出,这说明这种算法充分利用了人眼的视觉HVS 特性,利用FFT域嵌入水印后,水印的不可见性相当好,图像在嵌入水印前后视觉效果改变不大,不影响图像的正常使用。从图5 可明显看出:嵌入水印后的图像经过参数"Quality"为"45"的JPEG 压缩后,还能从中提取出水印且非常清晰;对含水印的图像进行方差为0.002 的高斯噪声攻击后,加入水印后的图像已经模糊,但是提取出的水印仍清晰可辩。以上实验证实了这种嵌入算法的抗攻击性较好,而且检测和提取易于实现。
WPF MaterialDesign演示程序是一款基于Windows Presentation Foundation(WPF)框架开发的演示程序。MaterialDesign是一种现代化的设计风格,以扁平化、简洁化和有层次感的界面设计为特点。这个演示程序提供了丰富的MaterialDesign控件和效果,帮助开发人员更好地理解和应用这种设计风格。 要下载WPF MaterialDesign演示程序,可以按照以下步骤进行: 1. 打开你常用的浏览器程序,如Google Chrome、火狐浏览器等。 2. 在浏览器地址栏中输入“WPF MaterialDesign演示程序下载”关键词进行搜索。 3. 选择一个可信赖的官方网站或者开发者网站,找到相关的下载页面。 4. 在下载页面中,可能会提供不同版本的WPF MaterialDesign演示程序。选择适合你的操作系统和开发环境的版本。 5. 点击下载按钮并等待下载完成。这个过程可能需要一定的时间,取决于你的下载速度。 6. 下载完成后,找到下载的安装文件,双击运行安装程序。 7. 按照安装程序的引导进行安装,选择安装路径和其他设置参数。 8. 等待安装完成后,就可以打开WPF MaterialDesign演示程序进行使用了。 需要注意的是,下载和安装WPF MaterialDesign演示程序之前,建议确保你的电脑满足相应的系统和硬件要求,以及没有安全软件的阻止。 总结起来,下载WPF MaterialDesign演示程序只需要在浏览器中搜索并选择可信赖的下载来源,然后按照相应的步骤进行下载和安装即可。这个演示程序将为你提供一个实践和学习MaterialDesign设计风格的平台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值