2015-10-28 2 views
4

J'essaie de convertir une image OpenCV (de type cv :: Mat) en format matlab car c'est ce que demande le reste du programme. J'utilise le code suivant pour le faire:Modification du format d'image OpenCV au format matlab, assertion de débogage __acrt_first_block == header

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; 
} 

Il se bloque avec une assertion de débogage:

__acrt_first_block == header 

sur la dernière ligne (retour MatlabImage). En retraçant la source de l'assertion, il semble être connecté pour désallouer le vecteur ColorChannels. J'ai essayé plusieurs façons de le faire, c'est-à-dire en utilisant .clear, en utilisant le truc de swap, ou en désallocalisant chaque élément du vecteur mais l'assertion reste.

S'il est intégré dans la fonction principale du programme C++, ce code fonctionne parfaitement, il ne le sera pas dans une fonction dédiée.

I simplifié la fonction principale, qui appelle le code ci-dessus au strict minimum:

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); 
} 

Le problème reste le même: assertion

J'utilise Visual Studio 2015. Il fonctionne très bien dans mode de libération, mais jette l'assertion de débogage en mode débogage (évidemment), en particulier, il pointe vers le debug_heap.cpp, ligne 980.

Merci Pat

+0

Ce qui est exactement "le format Matlab"? B, B, B, ..., G, G, G, ..., R, R, R, au lieu de B, G, R, B, G, R, ... B, G, R? Et vont dans [0,1] au lieu de [0,255]? – Miki

+0

pouvez-vous également fournir un petit _main_ qui appelle cette fonction et produire l'erreur? – Miki

+0

ok, également colonnes et rangées inversées. puis 1) vous n'avez pas besoin d'appeler 'clear', puisque le vecteur sera libéré après la sortie de la fonction 2) cela fonctionne bien pour moi. S'il vous plaît fournir un exemple qui montre l'erreur – Miki

Répondre

2

Configurez opencv avec "BUILD_WITH_STATIC_CRT" désactivé, son activé par défaut. Je recevais le même échec d'assertion lorsque j'appelais detectMultiScale à partir d'un thread séparé, et la fonction d'appel renvoyée, jusqu'à ce que je recompile opencv avec ce drapeau désactivé.

+0

Je ne comprends pas comment CRT statique pourrait aider. J'ai essayé ce que vous proposez et cela ne change rien au problème. – Oswin

2

Avec votre code, construit avec Visual Studio 2015, j'obtiens votre assertion de débogage __acrt_first_block == header. Le code suivant ne donne pas l'assertion, j'ai simplement changé std::vector<cv::Mat> ColorChannels; en cv::Mat ColorChannels[3];.

Je pense que ma solution est rapide et sale et peut-être que le solution proposé par iedoc est meilleur (je ne l'ai pas testé).

#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; 
} 

testé avec cette image (prise de https://raw.githubusercontent.com/zukun/rcc/master/shape_sharing/code_release/pascal/VOC2010/JPEGImages/2008_000027.jpg)

enter image description here

sortie standard est:

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 
-----------------------------------------------------------------