成功打印每个元素后出现 OpenCV 分割错误

OpenCV segmentation fault after successfully printing each element

本文关键字:OpenCV 分割 错误 打印 元素 成功      更新时间:2023-10-16

此代码在执行时显示预期的输出,但在末尾打印分段错误(核心转储(:

int main(){
    Mat src(100,100,CV_32F,0);
    for(int i=0 ; i < src.rows ; i++ ){
        for(int j=0 ; j < src.cols ; j++ ){
            src.at<float>(i,j)=0;
        }
    }
    for(int i=0 ; i < src.rows ; i++ ){
        for(int j=0 ; j < src.cols ; j++ ){
            cout<<src.at<float>(i,j)<<" ";
        }
        cout<<endl;
    }
    return 0;
} 

请编写最小的工作示例,以便我们可以简单地复制和粘贴代码并对其进行测试。

#include <iostream>
#include <opencv2/core.hpp>
using namespace std;
using namespace cv;
int main() {
  /* Mat src(100, 100, CV_32F, 0); */
  Mat src(100, 100, CV_32F, Scalar(0));
  for (int i = 0; i < src.rows; i++) {
    for (int j = 0; j < src.cols; j++) {
      src.at<float>(i, j) = 0;
    }
  }
  for (int i = 0; i < src.rows; i++) {
    for (int j = 0; j < src.cols; j++) {
      cout << src.at<float>(i, j) << " ";
    }
    cout << endl;
  }
  return 0;
}

问题似乎是opencv比较,在opencv2/core/mat.inl.hpp的第 500 行CV_Assertdata相对于NULL。基本上,当您在构造函数中给出非零值(例如1(或使用其Scalar数据类型来提供值时,您的代码就可以正常工作。

顺便说一句,我没有收到段错误,而只是在链接opencv_core后尝试运行代码时出现CV::Exception