成功打印每个元素后,OpenCV分段错误

问题描述 投票:0回答:1

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

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;
} 
c++ opencv
1个回答
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比较CV_Assertopencv2/core/mat.inl.hpp的500号线上,dataNULL相比。基本上,当你在构造函数中给出一个非零值,如1,或者使用他们的Scalar数据类型来提供值时,你的代码就可以了。

顺便说一句,我没有收到段错误,但只是一个CV::Exception尝试运行你的代码后连接opencv_core

© www.soinside.com 2019 - 2024. All rights reserved.