C++和OpenCV:问题将图像转换为灰度

Jas*_*son 3 c++ rgb opencv grayscale

这是我的代码.这很简单.

Mat image  = imread("filename.png");

imshow("image", image);
waitKey();

//Image looks great.

Mat image_gray;
image.convertTo(image_gray, CV_RGB2GRAY);

imshow("image", image_gray);
waitKey();
Run Code Online (Sandbox Code Playgroud)

但是当我调用该image.convertTo(image_gray, CV_RGB2GRAY);行时,我收到以下错误消息:

OpenCV Error: Assertion failed (func != 0) in unknown function, file ..\..\..\sr
c\opencv\modules\core\src\convert.cpp, line 1020
Run Code Online (Sandbox Code Playgroud)

使用OpenCV 2.4.3

Ove*_*Ove 6

该方法convertTo不进行颜色转换.

如果你想从BGR转换为GRAY你可以使用函数cvtColor:

Mat image_gray;
cvtColor(image, image_gray, CV_BGR2GRAY);
Run Code Online (Sandbox Code Playgroud)