简体   繁体   English

将OpenCV图像格式更改为matlab格式,调试断言__acrt_first_block == header

[英]Change OpenCV image format to matlab format, debug assertion __acrt_first_block == header

I am trying to convert an OpenCV Image (of type cv::Mat) into matlab-style format as this is what the rest of the program quires. 我试图将OpenCV图像(类型为cv :: Mat)转换为matlab样式格式,因为这是程序的其余部分所要求的。 I am using the following code to do that: 我使用以下代码来做到这一点:

inline double* ConvertCVImageToMATLABImage(Mat &CvImage)
{
    std::vector<cv::Mat> ColorChannels; // B, G, R channels
    cv::split(CvImage, ColorChannels);

    // remember to tranpose first because MATLAB is col-major!!!
    cv::transpose(ColorChannels[0], ColorChannels[0]);
    cv::transpose(ColorChannels[1], ColorChannels[1]);
    cv::transpose(ColorChannels[2], ColorChannels[2]);

    double *MatlabImage = new double[CvImage.rows*CvImage.cols * 3];

    int CounterCompleteImage = 0;
    int CounterEachColorChannel = 0;

    for (CounterEachColorChannel = 0; CounterEachColorChannel<CvImage.rows*CvImage.cols; ++CounterEachColorChannel, ++CounterCompleteImage)
    {
        MatlabImage[CounterCompleteImage] = static_cast<double>(ColorChannels[2].data[CounterEachColorChannel]);
    }

    for (CounterEachColorChannel = 0; CounterEachColorChannel<CvImage.rows*CvImage.cols; ++CounterEachColorChannel, ++CounterCompleteImage)
    {
        MatlabImage[CounterCompleteImage] = static_cast<double>(ColorChannels[1].data[CounterEachColorChannel]);
    }

    for (CounterEachColorChannel = 0; CounterEachColorChannel<CvImage.rows*CvImage.cols; ++CounterEachColorChannel, ++CounterCompleteImage)
    {
        MatlabImage[CounterCompleteImage] = static_cast<double>(ColorChannels[0].data[CounterEachColorChannel]);
    }

    return MatlabImage;
}

It crashes with a debug assertion: 它与调试断言崩溃:

__acrt_first_block == header

on the last line (return MatlabImage). 在最后一行(返回MatlabImage)。 Tracing back the source of the assertion, it seems to be connected to deallocating the vector ColorChannels. 追溯断言的来源,它似乎与解除分配矢量ColorChannels有关。 I have tried multiple ways of doing so, ie using .clear, using the swap trick, or deallocating every item in the vector but the assertation remains. 我尝试了多种方法,即使用.clear,使用交换技巧,或者解除分配向量中的每个项目,但断言仍然存在。

If embedded into the main function of the C++ program, this code works perfectly, it just won't to so in a dedicated function. 如果嵌入到C ++程序的主函数中,这段代码可以很好地工作,在专用函数中就不会这样。

I simplified the main function, which calls the above code to the bare minimum: 我简化了main函数,它将上面的代码调用到最低限度:

void main(void)
{
    cv::Mat CvImage = imread("E:\\VOC2012\\VOCdevkit\\VOC2012\\JPEGImages\\2008_000027.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file
    double* Image =  ConvertCVImageToMATLABImage(CvImage);
}

The problem remains the same: 问题依然存在: 断言

I am using Visual Studio 2015. It runs fine in release mode but throws the debug assertion in debug mode (obviously), specifically it is pointing to the debug_heap.cpp, Line 980. 我正在使用Visual Studio 2015.它在发布模式下运行良好但在调试模式下抛出调试断言(显然),特别是它指向debug_heap.cpp,第980行。

Thank you Pat 谢谢Pat

Configure opencv with "BUILD_WITH_STATIC_CRT" off, its on by default. 配置opencv并关闭“BUILD_WITH_STATIC_CRT”,默认情况下为on。 I was getting the same assertion failure when i called detectMultiScale from a separate thread, and the calling function returned, until i recompiled opencv with that flag turned off. 当我从一个单独的线程调用detectMultiScale时,我得到了相同的断言失败,并且调用函数返回,直到我重新编译打开该标志的opencv。

With your code, built with Visual Studio 2015, I get your debug assertion __acrt_first_block == header . 使用Visual Studio 2015构建的代码,我得到你的调试断言__acrt_first_block == header

The following code does not give the assertion, I simply changed std::vector<cv::Mat> ColorChannels; 下面的代码没有给出断言,我只是更改了std::vector<cv::Mat> ColorChannels; to cv::Mat ColorChannels[3]; to cv::Mat ColorChannels[3]; .

I think that my solution is quick and dirty and maybe the solution offered by iedoc is better (I did not test it). 我认为我的解决方案快速而且肮脏,也许iedoc提供的解决方案更好(我没有测试它)。

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

inline double* ConvertCVImageToMATLABImage(cv::Mat &CvImage)
{
    cv::Mat ColorChannels[3]; // B, G, R channels
    cv::split(CvImage, ColorChannels);

    // remember to tranpose first because MATLAB is col-major!!!
    cv::transpose(ColorChannels[0], ColorChannels[0]);
    cv::transpose(ColorChannels[1], ColorChannels[1]);
    cv::transpose(ColorChannels[2], ColorChannels[2]);

    double *MatlabImage = new double[CvImage.rows*CvImage.cols * 3];

    int CounterCompleteImage = 0;
    int CounterEachColorChannel = 0;

    for (CounterEachColorChannel = 0; CounterEachColorChannel<CvImage.rows*CvImage.cols; ++CounterEachColorChannel, ++CounterCompleteImage)
    {
        MatlabImage[CounterCompleteImage] = static_cast<double>(ColorChannels[2].data[CounterEachColorChannel]);
    }

    for (CounterEachColorChannel = 0; CounterEachColorChannel<CvImage.rows*CvImage.cols; ++CounterEachColorChannel, ++CounterCompleteImage)
    {
        MatlabImage[CounterCompleteImage] = static_cast<double>(ColorChannels[1].data[CounterEachColorChannel]);
    }

    for (CounterEachColorChannel = 0; CounterEachColorChannel<CvImage.rows*CvImage.cols; ++CounterEachColorChannel, ++CounterCompleteImage)
    {
        MatlabImage[CounterCompleteImage] = static_cast<double>(ColorChannels[0].data[CounterEachColorChannel]);
    }

    return MatlabImage;
}

#include <iostream>
int main(int,char**)
{
    std::cout << cv::getBuildInformation();
    cv::Mat CvImage = cv::imread("c:\\img\\2008_000027.jpg", CV_LOAD_IMAGE_COLOR);   // Read the file
    double* Image = ConvertCVImageToMATLABImage(CvImage);
    return 0;
}

Tested with this image (taken from https://raw.githubusercontent.com/zukun/rcc/master/shape_sharing/code_release/pascal/VOC2010/JPEGImages/2008_000027.jpg ) 使用此图像测试(摘自https://raw.githubusercontent.com/zukun/rcc/master/shape_sharing/code_release/pascal/VOC2010/JPEGImages/2008_000027.jpg

在此输入图像描述

Standard output is: 标准输出是:

General configuration for OpenCV 2.4.4 =====================================
  Version control:               unknown

  Platform:
    Host:                        Windows 6.1 x86
    CMake:                       2.8.10.2
    CMake generator:             Visual Studio 11 Win64
    CMake build tool:            C:/PROGRA~2/MICROS~3.0/Common7/IDE/devenv.com
    MSVC:                        1700

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/bin/x86_amd64/cl.exe  (ver 17.0.60315.1)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4  /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /MD /O2 /Ob2 /D NDEBUG  /Zi
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4  /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3   /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /MD /O2 /Ob2 /D NDEBUG  /Zi
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3   /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /STACK:10000000 /machine:x64   /INCREMENTAL:NO  /debug
    Linker flags (Debug):        /STACK:10000000 /machine:x64   /debug /INCREMENTAL 
    Precompiled headers:         YES

  OpenCV modules:
    To be built:                 core imgproc flann highgui features2d calib3d ml video objdetect contrib nonfree photo legacy gpu python stitching ts videostab
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 androidcamera java ocl

  GUI: 
    QT 4.x:                      NO
    Win32 UI:                    YES
    OpenGL support:              NO

  Media I/O: 
    ZLib:                        build (ver 1.2.7)
    JPEG:                        build (ver 62)
    PNG:                         build (ver 1.5.12)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)

  Video I/O:
    FFMPEG:                      YES (prebuilt binaries)
      codec:                     YES (ver 53.61.100)
      format:                    YES (ver 53.32.100)
      util:                      YES (ver 51.35.100)
      swscale:                   YES (ver 2.1.100)
      gentoo-style:              YES
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    DirectShow:                  YES
    XIMEA:                       NO

  Other third-party libraries:
    Use IPP:                     NO
    Use Eigen:                   NO
    Use TBB:                     NO
    Use OpenMP:                  NO
    Use GCD                      NO
    Use Concurrency              YES
    Use C=:                      NO
    Use Cuda:                    NO
    Use OpenCL:                  NO

  Python:
    Interpreter:                 C:/Python27/python.exe (ver 2.7.3)
    Libraries:                   C:/Python27/libs/python27.lib (ver 2.7.3)
    numpy:                       C:/Python27/lib/site-packages/numpy/core/include (ver 1.7.0)
    packages path:               C:/Python27/Lib/site-packages

  Java:
    ant:                         NO
    JNI:                         NO
    Java tests:                  YES

  Documentation:
    Build Documentation:         NO
    Sphinx:                      NO
    PdfLaTeX compiler:           NO

  Tests and samples:
    Tests:                       YES
    Performance tests:           YES
    C/C++ Examples:              NO

  Install path:                  C:/opencv/opencv244/visual_studio/install

  cvconfig.h is in:              C:/opencv/opencv244/visual_studio
-----------------------------------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM