C OPENCV:分配值后打印垫时发生错误
C++ Opencv: Error when printing Mat after assigning values
我使用循环说明了一个垫子并将值分配给每个元素。然后,我想打印其值。但是,我的核心转储错误发生了。我的代码如下:
#include "opencv2/opencv.hpp"
#include <opencv2/core/core.hpp>
#include <iostream>
#include <time.h>
using namespace std;
using namespace cv;
int main(int argc, char const *argv[])
{
int n = 4, i, j;
srand (time(NULL));
int n_Channel = 1;
int mySizes[2] = {2, 4};
Mat M = Mat::zeros(2, mySizes, CV_32FC(n_Channel));
cout << M.rows << "," << M.cols << "," << M.channels() << endl;
cout << M << endl;
for (i = 0; i < M.rows; ++i)
{
for (j = 0; j < M.cols; ++j)
{
M.at<Vec3f>(i,j)[0] = rand() % n;
cout << "i=" << i << ", j=" << j << ", M.at<Vec3f>(i,j)[0]=" << M.at<Vec3f>(i,j)[0] << endl;
}
}
cout << "???????" << endl;
cout << M << endl;
return 0;
}
cout工作直到完成打印" ???????"。然后发生核心转储错误。屏幕消息如下:
2,4,1
[0, 0, 0, 0;
0, 0, 0, 0]
i=0, j=0, M.at<Vec3f>(i,j)[0]=3
i=0, j=1, M.at<Vec3f>(i,j)[0]=3
i=0, j=2, M.at<Vec3f>(i,j)[0]=3
i=0, j=3, M.at<Vec3f>(i,j)[0]=1
i=1, j=0, M.at<Vec3f>(i,j)[0]=3
i=1, j=1, M.at<Vec3f>(i,j)[0]=3
i=1, j=2, M.at<Vec3f>(i,j)[0]=0
i=1, j=3, M.at<Vec3f>(i,j)[0]=0
???????
*** Error in `./my_app': malloc(): memory corruption (fast): 0x000000000245dfb0 ***
======= Backtrace: =========
我的代码怎么了?为什么它报告双免费错误?
感谢您帮助我!
第一个评论解决了我的问题。我只是在这里复制他的评论:
将int n_channel = 1更改为const int int n_channel = 1,然后将所有m.at更改为m.at>。您的实际示例只有一个通道,因此使用VEC3F是错误的。使用VEC使您有可能解决任意数量的频道的浮动图像。因此,N_CHANNEL必须为const。
谢谢@hanshirse。
相关文章:
- 使用 <list> (错误 C2760) 打印队列的元素
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 为什么重载运算符<<打印特征类成员会导致段错误?
- 我编写了以下代码来读取C++矩阵,然后打印其行和列.我收到此错误
- 我在C++中编写了一个方法来打印树类的预序,但它显示了分割错误
- 错误含义;以二进制形式打印数字
- 通过基类引用派生类后打印的错误值
- 如何在没有打印语句的情况下报告用户输入错误
- 为什么没有打印错误消息
- C++打印模板容器错误(错误:"运算符<<"的不明确重载)理解?
- 错误:变量或字段'PrintEntity'声明无效无效打印实体(实体 e);
- 将矢量的整数内容打印为字符串会导致分割错误
- 为什么我的C++代码在以下打印链表的代码片段中显示分段错误?
- C++ 添加编译器警告,以错误使用自定义打印/日志功能
- 尝试用纯 c 编程 arduino,得到以下错误:预期的"="、""、";",'asm'或"打印"之前的'__attribute__'|
- Python - 从 c++ DLL 错误打印值
- 使用std :: String ptr的错误打印std :: String
- 枚举类的GoogleTest测试错误打印
- 分段错误:打印动态数组时为 11
- Ustring错误(打印期间)