简体   繁体   English

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

[英]OpenCV segmentation fault after successfully printing each element

This code when executed displays the expected output but prints segmentation fault (core dumped) at the end : 该代码在执行时显示预期的输出,但最后显示分段错误(核心转储):

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;
} 

Please write the minimal working example so that we can simply copy and paste the code and test it. 请编写最小的工作示例,以便我们可以简单地复制和粘贴代码并对其进行测试。

#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;
}

The problem seems to be that opencv compares, at CV_Assert on line 500 of opencv2/core/mat.inl.hpp , data with respect to NULL . 这个问题似乎是opencv进行比较,在CV_Assert上的500行opencv2/core/mat.inl.hppdata相对于NULL Basically, when you give a non-zero value such as 1 in the constructor, or use their Scalar data-type to provide the value, your code just works fine. 基本上,当您在构造函数中提供非零值(例如1或使用其Scalar数据类型提供值时,您的代码可以正常工作。

By the way, I did not receive a segfault, but simply a CV::Exception when trying to run your code after linking against opencv_core . 顺便说一句,我没有收到段错误,而只是在链接到opencv_core之后尝试运行代码时遇到了CV::Exception

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM