使用EmguCV在C#中转换为灰度图像

12

我是EmguCV的新手。 我想将彩色图像转换为灰度图像。 为了进行转换,我使用了以下代码

Image<Gray,byte> grayImage = ColordImage.Convert<Gray, byte>();

现在当我在 C# 中编译这段代码时,它没有错误,但是当我运行它后,几秒钟后就会在这行代码处抛出异常,表示 OpenCV 不支持这种类型的转换。现在有谁可以帮助我解决这个问题。

问候 Amal


1
Tom Wright的回答对我有用。如果它对你也有用,请接受这个答案。 - Chad
4个回答

15

这可能取决于ColordImage的颜色类型。例如,以下代码是可行的:

Capture cap = new Capture(1);
Image <Bgr,Byte> ColordImage = cap.QueryFrame();
Image <Gray,Byte> grayImage = ColordImage.Convert<Gray, Byte>();
imageBox1.Image = grayImage;

如果您能提供更多的代码,可能会更加清楚发生了什么。


5

5

如果您使用的是Emgu CV 2.2.1.1150,您需要将opencv_imgproc220.dll粘贴到您的项目bin/debug文件夹中。


2
这个答案看起来很蠢,但它是正确的答案。我在ILSpy中检查了ColordImage.Convert()函数,该函数使用Image.ConvertColor(),其中包含try/catch块,可以捕获所有内容,包括dll加载错误。 - Markos

4

一种简单的方法是在构造函数中传递彩色图像的位图;

Image<Bgr, byte> inputImage = //your original bgr image
Image<Gray, Byte> result = new Image<Gray,byte>(inputImage.Bitmap);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接