【问题标题】:What does the error opencv error assertion failed (p [-1] <= 2) mean and how to deal with it?错误opencv error assertion failed (p [-1] <= 2) 是什么意思,如何处理?
【发布时间】:2018-04-03 04:48:21
【问题描述】:

我正在尝试使用 OpenCV 库将 Mathlab 代码翻译成加号。

在下面一行:

resize (sig_temp, sig_temp, \
Size (sig_temp.size [0] / 2 + sig_temp.size [0]% 2, \
sig_temp.size [1] / 2 + sig_temp.size [1]% 2));

程序出错:

opencv error assertion failed (p[-1] <= 2) in cv::Matsize:: operator ()

之前的错误(dims &lt;= 2, top / bottom / right / left&gt; = 0) 非常明显,因为它们很明显尺寸不应该超过两个,图像的边界应该是非负的。我立刻不明白 p [-1] 是什么意思,为什么它不应该超过两个(但我猜这里的东西又与层相连)。

sig_temp - 三通道Mat矩阵。

【问题讨论】:

    标签: c++ matlab opencv image-processing


    【解决方案1】:

    据我所知: https://github.com/opencv/opencv/pull/8718/files MatSize::p 是维度大小的数组。

    p[-1] 为您提供金额维度。也许您的 sig_temp 矩阵是 3 维或更多?

    我的建议是要么使用其 reshape 方法将 sig_temp 定义为

    cv::Mat3b sig_temp;
    

    【讨论】:

      【解决方案2】:

      cv::Sizeint width, int height 作为参数。 Mat::size 的第 0 个元素是高度。

      cv::Mat img = imread("image.jpg");
      
      cout<<img.size()<<endl; // This line prints [columns x rows]
      cout<<img.size[0]<<endl; // This line prints the rows
      cout<<img.size[1]<<endl; // This line prints the columns
      

      所以在这一行:

      Size (sig_temp.size [0] / 2 + sig_temp.size [0]% 2, \
      sig_temp.size [1] / 2 + sig_temp.size [1]% 2)
      

      您将int height, int width 作为参数提供给cv::Size,它将int width, int height 作为参数。

      您应该使用行和列以避免混淆。

      Size (sig_temp.cols / 2 + sig_temp.cols% 2, \
          sig_temp.rows / 2 + sig_temp.rows% 2)
      

      【讨论】:

      • 我不能使用 .cols 和 .rows ,因为它是 3 通道矩阵,在这种情况下它们的值为 -1。我尝试交换索引,但错误仍然存​​在。
      • 任何 RGB 图像都是 3 通道矩阵,行和列为 -1 与具有 3 通道的矩阵无关。查看您的矩阵是否创建正确,尝试打印矩阵。
      • 我引用 Mat 类的文档中的一行:行数和列数,或者当矩阵有超过 2 个维度时为 (-1, -1)。我的小虫也这样告诉我。
      • 无论如何,我将这些参数替换为标量 (256,256) 并且错误仍然存​​在。我认为我们应该找到它的意义,然后寻找它的原因。对不起,如果我的英语不好。
      • 渠道和维度是两个不同的东西。您应该在问题中提到图像尺寸大于两个。
      猜你喜欢
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多