c++ - 创建中的空指针(为缺少的输出数组调用 create()),file/home/m/OpenCV/modules/core/src/matrix_wrap.cpp,第 1461 行

标签 c++ pointers runtime-error ubuntu-16.04 opencv3.0

我正在尝试运行 O'reilly 书中的Drawing labeled connected components 示例,但我在运行时收到以下错误消息(我构建的源代码没有问题):

#include <opencv2/opencv.hpp>
#include <algorithm>
#include <iostream>
using namespace std;
//using namespace cv;

int main(int argc, char* argv[])
{
    cv::Mat img, img_edge, labels, img_color, stats;
// load image or show help if no image was provided
    if( argc != 2|| (img = cv::imread( argv[1], 0)).empty())
    {
        cout << "\nExample 8_3 Drawing Connected componnents\n" << "Call is:\n" <<argv[0] <<" image\n\n";
        return -1;
    }
    cv::threshold(img, img_edge, 128, 255, cv::THRESH_BINARY);
    cv::imshow("Image after threshold", img_edge);
    int i, nccomps = cv::connectedComponentsWithStats (
                         img_edge, labels,
                         stats, cv::noArray()
                     );
    cout << "Total Connected Components Detected: " << nccomps << endl;
    vector<cv::Vec3b> colors(nccomps+1);
    colors[0] = cv::Vec3b(0,0,0); // background pixels remain black.
    for( i = 1; i <= nccomps; i++ )
    {
        colors[i] = cv::Vec3b(rand()%256, rand()%256, rand()%256);
        if( stats.at<int>(i-1, cv::CC_STAT_AREA) < 100 )
            colors[i] = cv::Vec3b(0,0,0); // small regions are painted with black too.
    }
    img_color = cv::Mat::zeros(img.size(), CV_8UC3);
    for( int y = 0; y < img_color.rows; y++ )
        for( int x = 0; x < img_color.cols; x++ )
        {
            int label = labels.at<int>(y, x);
            CV_Assert(0 <= label && label <= nccomps);
            img_color.at<cv::Vec3b>(y, x) = colors[label];
        }
    cv::imshow("Labeled map", img_color);
    cv::waitKey();
    return 0;
}

OpenCV(3.4.1) Error: Null pointer (create() called for the missing output array) in create, file /home/m/OpenCV/modules/core/src/matrix_wrap.cpp, line 1461 terminate called after throwing an instance of 'cv::Exception' what(): OpenCV(3.4.1) /home/m/OpenCV/modules/core/src/matrix_wrap.cpp:1461: error: (-27) create() called for the missing output array in function create

问题是什么,我该如何解决?

最佳答案

使用调试器显示此异常出现在调用 connectedComponentsWithStats() 期间。错误的描述将此推断为有一些不应该那样的空/空。

OpenCV 的 current docs表明第四个参数质心应该是 具有 2 列和“质心”行数的 CV_64F 矩阵。这个矩阵可能是由函数本身初始化的,只要它存在。

因此,您只需创建此矩阵并按照函数的预期提供它:

cv::Mat centroids;
int i, nccomps = cv::connectedComponentsWithStats (img_edge, labels, stats, centroids);

您的问题在于使用 OpenCV 3.4.1(最新),与您正在使用的书相比,它的语法发生了变化;这是任何开源库的常见问题。我建议您保留当前的 ​​OpenCV 文档,以确保书中的代码仍然有效,或者降级到旧版本。

关于c++ - 创建中的空指针(为缺少的输出数组调用 create()),file/home/m/OpenCV/modules/core/src/matrix_wrap.cpp,第 1461 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50915125/

相关文章:

c++ - 如何获取最里面的模板参数类型?

c++ - Boost::asio 和嵌套/递归 service.run()/run_one()

c - 生成大小超过 8 MB 的大型十六进制数数组

java - 运行时错误。 java 。创建一个类 Sort,按升序显示四个整数

c++ - 如何使用 C++ ping 一个 php 页面?

c++ - 如果我从 getter 生成迭代器,程序将中止

c - C中初始化char指针数组

c++ - 声明指向结构的指针后的值是多少

c - 使用 memmove 时发生总线错误

ruby-on-rails - Ruby on Rails ActiveModel::MassAssignmentSecurity::错误