本文主要是介绍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 HbitmapBitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ptr,IntPtr.Zero,Int32Rect.Empty,System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());DeleteObject(ptr); //release the HBitmapreturn 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学习使用-中值模糊的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!