【发布时间】:2015-07-05 14:48:36
【问题描述】:
我尝试使用 countNonZero() 函数进行水平投影,如下所示。
Mat src = imread(INPUT_FILE, CV_LOAD_IMAGE_COLOR);
Mat binaryImage = src.clone();
cvtColor(src, src, CV_BGR2GRAY);
Mat horizontal = Mat::zeros(1,binaryImage.cols, CV_8UC1);
for (int i = 0; i<binaryImage.cols; i++)
{
Mat roi = binaryImage(Rect(0, 0, 1, binaryImage.rows));
horizontal.at<int>(0,i) = countNonZero(roi);
cout << "Col no:" << i << " >>" << horizontal.at<int>(0, i);
}
但是在调用countonZero()函数的那一行出现了错误。错误如下。
OpenCV Error: Assertion failed (src.channels() == 1 && func != 0) in cv::countNo
nZero, file C:\builds\2_4_PackSlave-win32-vc12-shared\opencv\modules\core\src\st
at.cpp, line 549
谁能指出错误?
【问题讨论】:
-
binaryImage 是 src 的副本,它是 3 通道彩色图像。试试 cvtColor(src, binaryImage, CV_BGR2GRAY);
-
还有另一个错误:将 Horizontal.at
(0,i) 切换为 Horizontal.at (0,i),因为您创建了 8 位数据类型。 -
我做了更改,错误已解决。感谢那。但现在我看到 countNonZero(roi) 函数返回的值始终为零。我还确认 binaryImage 不是完全黑色的图像。 (它到处都有黑白像素)
-
你换成cout了吗>" (0, i);也是?
-
输入矩阵的第一列完全为零吗?在每次迭代中,您都会阅读同一列!试试 Mat roi = binaryImage(Rect(i, 0, 1, binaryImage.rows));
标签: c++ opencv assertion projection-matrix