gpt4 book ai didi

c++ - 为什么在 OpenCV 中访问此矩阵时出现内存错误?

转载 作者:太空狗 更新时间:2023-10-29 20:56:20 25 4
gpt4 key购买 nike

我只是想写入给定大小的矩阵。当我在 Valgrind 中运行这个程序时,出现内存错误,如下所示:

主要.cpp:

#include <iostream>
#include <opencv2/opencv.hpp>

int main()
{
cv::Mat m = cv::Mat::zeros(cv::Size(59, 9), CV_32SC1);
m.at<int>(9, 4) = 1;
}

编译说明:

g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary  main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching

最后运行 Valgrind:

valgrind ./binary

它在我的机器上返回这条消息:

==98408== Invalid write of size 4
==98408== at 0x1000017F8: main (main.cpp:7)
==98408== Address 0x10dd202cc is 4 bytes after a block of size 2,152 alloc'd
==98408== at 0x100009EAB: malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==98408== by 0x10001D1E6: cv::fastMalloc(unsigned long) (in /usr/local/Cellar/opencv/2.4.12/lib/libopencv_core.2.4.12.dylib)
==98408== by 0x1000F4C77: cv::Mat::create(int, int const*, int) (in /usr/local/Cellar/opencv/2.4.12/lib/libopencv_core.2.4.12.dylib)
==98408== by 0x1000F0A51: cv::MatOp_Initializer::assign(cv::MatExpr const&, cv::Mat&, int) const (in /usr/local/Cellar/opencv/2.4.12/lib/libopencv_core.2.4.12.dylib)
==98408== by 0x1000018FB: cv::MatExpr::operator cv::Mat() const (mat.hpp:1227)
==98408== by 0x1000017BC: main (main.cpp:6)

这些是我的机器的规范:

Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin15.0.0
Thread model: posix

homebrew/science/opencv 2.4.12

最佳答案

您似乎混淆了矩阵的维度。你构造一个 59 列 9 行的矩阵,并访问第 10 行和第 4 列:

cv::Size(width,height); // size specification
m.at<int>(y,x); // access

所以第 9 行超出范围。要么交换维度,要么交换索引!

关于c++ - 为什么在 OpenCV 中访问此矩阵时出现内存错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33824695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com