cvtColor 断言失败(使用 C++ 的 OpenCV)抛出实例 cv::exception
Posted
技术标签:
【中文标题】cvtColor 断言失败(使用 C++ 的 OpenCV)抛出实例 cv::exception【英文标题】:cvtColor assertion failed ( OpenCV with C++ ) throwing an instance cv::exception 【发布时间】:2014-04-07 14:00:24 【问题描述】:我用opencv和c++做了一个运动检测程序,我得到了这个错误
root@raspberrypi:/home/pi/motion_src/src# OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /usr/src/packages/BUILD/opencv-2.4.1+dfsg/modules/imgproc/src/color.cpp, line 3205
在抛出 'cv::Exception' 实例后调用终止 什么():/usr/src/packages/BUILD/opencv-2.4.1+dfsg/modules/imgproc/src/color.cpp:3205:错误:(-215)scn == 3 ||函数 cvtColor 中的 scn == 4
这是我的源代码
// Take images and convert them to gray
Mat result, result_cropped;
Mat prev_frame = result = cvQueryFrame(camera);
Mat current_frame = cvQueryFrame(camera);
Mat next_frame = cvQueryFrame(camera);
cvtColor(current_frame, current_frame, CV_RGB2GRAY);
cvtColor(prev_frame, prev_frame, CV_RGB2GRAY);
cvtColor(next_frame, next_frame, CV_RGB2GRAY);
// d1 and d2 for calculating the differences
// result, the result of and operation, calculated on d1 and d2
// number_of_changes, the amount of changes in the result matrix.
// color, the color for drawing the rectangle when something has changed.
Mat d1, d2, motion;
int number_of_changes, number_of_sequence = 0;
Scalar mean_, color(0,255,255); // yellow
// Detect motion in window
int x_start = 10, x_stop = current_frame.cols-11;
int y_start = 350, y_stop = 530;
// If more than 'there_is_motion' pixels are changed, we say there is motion
// and store an image on disk
int there_is_motion = 5;
// Maximum deviation of the image, the higher the value, the more motion is allowed
int max_deviation = 20;
// Erode kernel
Mat kernel_ero = getStructuringElement(MORPH_RECT, Size(2,2));
// All settings have been set, now go in endless loop and
// take as many pictures you want..
while (true)
// Take a new image
prev_frame = current_frame;
current_frame = next_frame;
next_frame = cvQueryFrame(camera);
result = next_frame;
cvtColor(next_frame, next_frame, CV_RGB2GRAY);
// Calc differences between the images and do AND-operation
// threshold image, low differences are ignored (ex. contrast change due to sunlight)
absdiff(prev_frame, next_frame, d1);
absdiff(next_frame, current_frame, d2);
bitwise_and(d1, d2, motion);
threshold(motion, motion, 35, 255, CV_THRESH_BINARY);
erode(motion, motion, kernel_ero);
number_of_changes = detectMotion(motion, result, result_cropped, x_start, x_stop, y_start, y_stop, max_deviation, color);
// If a lot of changes happened, we assume something changed.
if(number_of_changes>=there_is_motion)
if(number_of_sequence>0)
saveImg(result,DIR,EXT,DIR_FORMAT.c_str(),FILE_FORMAT.c_str());
saveImg(result_cropped,DIR,EXT,DIR_FORMAT.c_str(),CROPPED_FILE_FORMAT.c_str());
number_of_sequence++;
else
number_of_sequence = 0;
// Delay, wait a 1/2 second.
cvWaitKey (DELAY);
return 0;
【问题讨论】:
请使用 camera.read(frame),而不是 cvQueryFrame。避免使用旧的 c-api 函数。在应用 cvtColor 之前还要检查返回值或 frame.empty() 【参考方案1】:“cvtColor”函数期望输入图像具有 3 或 4 个通道,用于您使用的转换类型 (CV_RGB2GRAY) - 这就是投诉的内容。检查输入是否满足此条件(或不为空)。
另外,由于您要更改通道数,我不确定是否可以使用该函数“就地”工作 - 您可能应该为函数的结果创建一个新变量。
color.cpp 源供参考 - https://github.com/Itseez/opencv/blob/master/modules/imgproc/src/color.cpp
【讨论】:
以上是关于cvtColor 断言失败(使用 C++ 的 OpenCV)抛出实例 cv::exception的主要内容,如果未能解决你的问题,请参考以下文章
void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int),文件 /../Linux/./../src/ 中的断言失败 (scn == 3
调试断言失败表达式:_pFirstBlock == pHead 使用 OpenCV 和 C++ 尝试调用 SurfFeatureDetector