我想通过使用锐化内核执行 CvInvoke.Filter2d 来锐化图像: {[-1,-1,-1],[-1, 9,-1],[-1,-1,-1]}
如何在 C# 下的 EMGU 中定义这个内核?
我使用的是 EMGU 版本 4.3.0.3890
这是我的代码:
// Load image
Image<Bgr, Byte> img = null;
img = new Image<Bgr, byte>(@"D:\Sample.png");
// Convert the image to grayscale
Image<Gray, float> gray = img.Convert<Gray, float>()
.ThresholdBinary(new Gray(150), new Gray(255));
// Add use of Filter2D here
// Load image
Image<Bgr, byte> img = new Image<Bgr, byte>(@"path/img.jpg");
// Convert the image to grayscale
Image<Gray, byte> gray = img.Convert<Gray, byte>().ThresholdBinary(new Gray(150), new Gray(255));
// Add use of Filter2D here
CvInvoke.Imshow("GrayImage", gray);
float[,] matrix = new float[3, 3] {
{ 1, 1, 1 },
{ 1, -9, 1},
{ 1, 1, 1 }
};
ConvolutionKernelF matrixKernel = new ConvolutionKernelF(matrix);
var dst = new Mat(gray.Size, DepthType.Cv8U, 1);
CvInvoke.Filter2D(gray, dst, matrixKernel, new Point(0, 0));
CvInvoke.Imshow("FiltredImage", dst);
CvInvoke.WaitKey(0);
PS:
最好使用 Mat 和 Matrix(如果需要访问像素)而不是图像。 --> 来源:https://www.emgu.com/wiki/index.php/Working_with_Images
转换为灰度时,需要采用byte类型,而不是float