先上效果图
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());