WPF+Emgucv实现在图像上画出感兴趣的区域 并进行掩膜获取 得到图像均值

2021/8/3 6:05:56

本文主要是介绍WPF+Emgucv实现在图像上画出感兴趣的区域 并进行掩膜获取 得到图像均值,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

先上效果图

XAML:




<Grid.RowDefinitions>


</Grid.RowDefinitions>

        <Grid>
            <UniformGrid Columns="2">
                <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                    <InkCanvas Name="ink" Background="Transparent">
                    <Image Name="ImgShow" Source="/temp.png" IsHitTestVisible="False">

                    </Image>
                </InkCanvas>
                </ScrollViewer>
                <UniformGrid Rows="2">
                    <GroupBox Header="Mask" >
                        <Image x:Name="imgMask"></Image>
                    </GroupBox>
                    <GroupBox Header="Result">
                        <Image x:Name="imgResult"></Image>
                    </GroupBox>
                </UniformGrid>
            </UniformGrid>
            

        </Grid>
       
        <DockPanel Grid.Row="1" Margin="20">
            
                <Grid Grid.Row="1" VerticalAlignment="Center" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <RadioButton Grid.Column="0" VerticalAlignment="Center" Content="绘制墨迹" Click="RadioButton_Click"/>
                    <RadioButton Grid.Column="1" Content="按点擦除" Click="RadioButton_Click"/>
                    <RadioButton Grid.Column="2" Content="按线擦除" Click="RadioButton_Click"/>
                    <RadioButton Grid.Column="3" Content="选中墨迹" Click="RadioButton_Click"/>
                    <RadioButton Grid.Column="4" Content="停止操作" Click="RadioButton_Click"/>
                </Grid>
           
            <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20 0 0 0">颜色选择:</TextBlock>
            <Grid x:Name="colorchk" Background="Black" Width="100" MouseLeftButtonDown="Grid_MouseLeftButtonDown" ></Grid>
            <Button Width="100" Margin="20 0 0 0" Height="30" Click="Button_Click">开始计算</Button>
            <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20 0 0 0">兴趣区域平均值:<Run Foreground="#e03997" Name="txt_meanValue"></Run></TextBlock>
           
        </DockPanel>
    </Grid>
   
</Grid>
CS主要代码: //准备掩膜图像 Image<gray, byte=""> mask = new Image<gray, byte="">(OldImage.Width, OldImage.Height); //设置全黑 mask.SetZero(); //把轮廓全部绘制成白色在mask上变成感兴趣的区域 类似roi for (int i = 0; i < contours.Size; i++) { CvInvoke.DrawContours(mask, contours, i, new MCvScalar(255, 255, 255), -1, LineType.AntiAlias, null, int.MaxValue); } //准备结果图像 Image<bgr, byte=""> Result = new Image<bgr, byte="">(OldImage.Width, OldImage.Height); //显示 imgMask.Source = BitmapToBitmapImage(mask.ToBitmap()); //CvInvoke.Imshow("DrawContours", mask); //获取感兴趣的区域到结果图 OldImage.CopyTo(Result, mask); //显示 //CvInvoke.Imshow("mask", Result); imgResult.Source = BitmapToBitmapImage(Result.ToBitmap());

这篇关于WPF+Emgucv实现在图像上画出感兴趣的区域 并进行掩膜获取 得到图像均值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程