Emgu-WPF学习使用-中值模糊
Posted lonelyxmas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Emgu-WPF学习使用-中值模糊相关的知识,希望对你有一定的参考价值。
原文:Emgu-WPF学习使用-中值模糊
实现效果:
实现途径:
前提:Image File-> System.Drawing.Bitmap->Image<Bgr, byte>
string sFile = GlobalVar.DATAS_PATH + "Samples/Test1.png";
BitmapImage oOriginBitSrc = new BitmapImage(new Uri(sFile));
System.Drawing.Image oImgOrigin = System.Drawing.Image.FromFile(sFile);
System.Drawing.Bitmap oBitmap = new System.Drawing.Bitmap(oImgOrigin);
Image<Bgr, byte> imgSrc = new Image<Bgr, byte>(oBitmap);
oBitmap.Dispose();
第一行效果:原图->中值模糊->高斯模糊->灰度化->Otsu二值化。
//中值模糊
Image<Bgr, byte> imgMedian = imgSrc.SmoothMedian(5); //使用5*5的卷积核
// 高斯模糊
Image<Bgr, byte> imgGaussian = imgMedian.SmoothGaussian(5);
// 灰度化
Image<Gray, byte> imgGray = new Image<Gray, byte>(imgGaussian.Size);
CvInvoke.CvtColor(imgGaussian, imgGray, ColorConversion.Bgr2Gray);
// Otsu二值化
Image<Gray, byte> imgThresholdOtsu = new Image<Gray, byte>(imgGray.Size);
CvInvoke.Threshold(imgGray, imgThresholdOtsu, 0, 255, ThresholdType.Otsu);
第二行效果:原图->灰度化 ->Otsu二值化 ->中值模糊// 从原图直接灰度化
Image<Gray, byte> imgOriginGray = new Image<Gray, byte>(imgSrc.Size);
CvInvoke.CvtColor(imgSrc, imgOriginGray, ColorConversion.Bgr2Gray);
// Otsu二值化
Image<Gray, byte> imgOriginGrayThresholdOtsu = new Image<Gray, byte>(imgOriginGray.Size);
CvInvoke.Threshold(imgOriginGray, imgOriginGrayThresholdOtsu, 0, 255, ThresholdType.Otsu);
// 中值模糊
Image<Gray, byte> imgMedian = imgOriginGrayThresholdOtsu.SmoothMedian(5);
AppUtils.ShowGrayImage(this.ImgFun2Result3Zm, imgMedian);
第三行效果:原图?->灰度化?->高斯模糊->Otsu二值化?// 从原图直接灰度化
Image<Gray, byte> imgOriginGray = new Image<Gray, byte>(imgSrc.Size);
CvInvoke.CvtColor(imgSrc, imgOriginGray, ColorConversion.Bgr2Gray);
// 高斯模糊
Image<Gray, byte> imgGaussian = imgOriginGray.SmoothGaussian(5);
// Otsu二值化
Image<Gray, byte> imgOriginGrayThresholdOtsu = new Image<Gray, byte>(imgGaussian.Size);
CvInvoke.Threshold(imgGaussian, imgOriginGrayThresholdOtsu, 0, 255, ThresholdType.Otsu);
其他:转为BitmapSource在WPF的Image中呈现。 [DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
/// <summary>
/// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
/// </summary>
/// <param name="image">The Emgu CV Image</param>
/// <returns>The equivalent BitmapSource</returns>
public static BitmapSource ToBitmapSource(IImage image)
{
using (System.Drawing.Bitmap source = image.Bitmap)
{
IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
ptr,
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ptr); //release the HBitmap
return bs;
}
}
为了方便查看逐步效果,我提取了以下方法。
public static void ShowImage(System.Windows.Controls.Image oImage, UMat src)
{
oImage.Dispatcher.Invoke(() => {
oImage.Source = BitmapSourceConvert.ToBitmapSource(src);
});
}
public static void ShowBgrImage(System.Windows.Controls.Image oImage, Image<Bgr, byte> src)
{
oImage.Dispatcher.Invoke(() => {
oImage.Source = BitmapSourceConvert.ToBitmapSource(src);
});
}
public static void ShowGrayImage(System.Windows.Controls.Image oImage, Image<Gray, byte> src)
{
oImage.Dispatcher.Invoke(() => {
oImage.Source = BitmapSourceConvert.ToBitmapSource(src);
});
}
我参考了链接:点击打开链接?https://www.cnblogs.com/CoverCat/p/5055644.html
? ? ? ? ? ? ? ? ? ? ? ?点击打开链接 ?http://www.cnblogs.com/CoverCat/p/5043833.html?
以上是关于Emgu-WPF学习使用-中值模糊的主要内容,如果未能解决你的问题,请参考以下文章