EmguCV-Canny函数抛出_src.depth()==CV_8U



我目前正在进行一个使用EmguCV 4.2 的项目

我需要使用Canny函数:

gray=gray.Canny(50, 200);

它抛出错误:

_src.depth()==CV_8U

我注意到只有当我在早期的图像上使用CvInvoke.Threshold((时才会发生异常

CvInvoke.Threshold(skeleton,img,0,255,Emgu.CV.CvEnum.ThresholdType.Binary);

我想知道为什么会发生这种情况,如果没有Threshold((函数,一切都会正常工作。这个函数是不是以某种方式改变了我图像的深度?如何将其转换回使用Canny((函数而不会出现问题?

提前谢谢。

根据我从您的问题中收集到的信息,您将图像转换为灰度可能存在问题。

下面的错误代码指的是图像的深度类型,即图像的每个像素可以容纳多少颜色值。在您的情况下,对于灰度图像,由于您只持有从黑色到白色的值,中间有不同的灰度变体,因此图像的深度类型会更小。

_src.depth()==CV_8U

如果只想将图像传递给Canny函数,则必须先将其转换为灰度。

// Read in the image from a file path
Mat img = CvInvoke.Imread(filePath, ImreadModes.AnyColor);
// Convert the image to gray scale
Image<Gray, byte> gray = img.ToImage<Gray, byte>();
// Threshold the image
CvInvoke.Threshold(gray, gray, 0, 100, ThresholdType.Binary);
// Canny the thresholded image
gray = gray.Canny(50, 200);

如果您只想在不通过阈值的情况下将图像传递给Canny函数,那么您可以使用下面的代码。

// Read in the image from a file path
Mat img = CvInvoke.Imread(filePath, ImreadModes.AnyColor);
// Convert the image to gray scale
Image<Gray, byte> gray = img.ToImage<Gray, byte>();
// Canny the thresholded image
gray = gray.Canny(50, 200);

最新更新