执行时此代码显示预期输出但在结尾处打印分段错误(核心转储):
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
比较CV_Assert
在opencv2/core/mat.inl.hpp
的500号线上,data
与NULL
相比。基本上,当你在构造函数中给出一个非零值,如1
,或者使用他们的Scalar
数据类型来提供值时,你的代码就可以了。
顺便说一句,我没有收到段错误,但只是一个CV::Exception
尝试运行你的代码后连接opencv_core
。