在OpenCV中访问此矩阵时,为什么会出现内存错误? [英] Why am I getting memory errors when accessing this matrix in OpenCV?
本文介绍了在OpenCV中访问此矩阵时,为什么会出现内存错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想写给定大小的矩阵.当我在Valgrind中运行该程序时,出现如下所示的内存错误:
I'm simply trying to write to a matrix of a given size. When I run this program in Valgrind, I get memory errors as shown below:
main.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:
Finally run Valgrind:
valgrind ./binary
它在我的机器上返回此消息:
It returns this message on my machine:
==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)
这些是我的机器的规格:
These are the specifications of my machine:
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列:
You seem to have the dimensions of your matrix mixed up. You construct a matrix with 59 columns and 9 rows, and access the 10th row and 4th column:
cv::Size(width,height); // size specification
m.at<int>(y,x); // access
因此第9行超出了范围.要么交换尺寸,要么交换索引!
So row 9 is out of range. Either swap the dimensions, or the indices!
这篇关于在OpenCV中访问此矩阵时,为什么会出现内存错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文