c++ - cv::内存位置出现异常错误

标签 c++ visual-studio opencv memory-management out-of-memory

我的项目已经很大,因为它正在从另一个项目中使用,所以我无法添加整个代码。 我正在使用 Visual Studios 2015、OpenCV-3.1.0 和 OpenNI2。

通过调试,我已经能够找到导致此处所示错误的确切行:

enter image description here

此错误发生在以下代码行:

    cvRectangle(&img_8bit, (pt1.x,pt1.y), (pt2.x,pt2.y), CV_RGB(255, 255, 255), -1);

下图显示了在该行执行之前这些变量中的每一个都有数据,它还显示了每个变量的数据类型:

enter image description here

我查看了此论坛上出现的其他一些关于类似问题的帖子,但未能找到适合我的情况的解决方案。

我已经尝试通过将其拆分为 x 和 y 变量而不是仅仅说 pt1 和 pt2 来更改输入两个点的方式。 我也尝试过更改图像的输入方式,但没有成功。

谁能告诉我这个问题是与变量有关,还是与矩形函数有关,还是其他什么?

-----编辑---------------- 我已经能够用更少量的代码重新创建错误,以便可以显示所有内容。 请注意,如果 cvRectangle 行被注释掉,那么它将从存储的位置完美地显示所有图像。

#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <iostream>
#include <sstream>
#include <Windows.h>
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{

    int nFrame = 0;//Current is the first few frames
    cv::Mat depthMap;
    char m_hitKey = 0;
    char image_name[100];
    CvPoint pt1, pt2;
    pt1.x = 31;
    pt1.y = 89;
    pt2.x = 96;
    pt2.y = 119;

    for (int filenum = 0; filenum< 2276; filenum++)
    {
        ++nFrame;//This is the first record at a few frames
        std::sprintf(image_name, "%s%d%s", "C:\\Users\\James Mallett\\Dep\\dep-", filenum, ".png");

        depthMap = cv::imread(image_name, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);

        if (m_hitKey = cv::waitKey(50))//Analyzing Key

        {
            if (m_hitKey == 27)
            {
                break;
            }
        }
        cvRectangle(&depthMap, pt1, pt2, CV_RGB(255, 255, 255), -1);
        cv::imshow("RGB", depthMap);
    }

    cv::destroyAllWindows();

    return 0;
}

最佳答案

在你的例子中:

cvRectangle(&depthMap, pt1, pt2, CV_RGB(255, 255, 255), -1);

这看起来非常错误,因为 the signature I found online(是的,这是早期版本的文档,但是 this hasn't changed)是......

void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )

... 和 depthMapcv:Mat 类型。像这样使用名为 cv:rectangle 的匹配签名调用 C++ 函数 ...

cv:rectangle(depthMap, pt1, pt2, CV_RGB(255, 255, 255), -1);

... 会更有意义。

CvArr 是 - according to the docs - 一种“元类型”(API 的愚蠢选择,IMO),因此它可能指向任何 plImage*CvMat* 甚至 CvSeq*。请注意,cv:Mat 不在该列表中。我在这个网站上发现了一个问题,详细说明了如何 c onvert between these 类型。

关于c++ - cv::内存位置出现异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39384300/

相关文章:

c++ - 为什么在检查窗口服务状态时 OpenService() 不起作用?

C++ 用法 : what does "*(new int); " do?

c++ - 在 C++ 中将值作为类的成员传递给 vector

c++ - Visual Studio 的预编译 header 包含什么?

image - 如何在C++中加载24位Tiff图像

c++ - 如何避免前向声明错误?

visual-studio - 用于选择启动项目的 Visual Studio 宏

c# - 将类移动到文件夹后更新所有类文件中的命名空间名称

c++ - OpenCV minMaxLoc 函数中的段错误

python - 如何在完全透明的物体中找到矩形?