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. 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). Tracing back the source of the assertion, it seems to be connected to deallocating the vector ColorChannels. I have tried multiple ways of doing so, i.e. using .clear, using the swap trick, or deallocating every item in the vector but the assertation remains.
If embedded into the main function of the C++ program, this code works perfectly, it just won't to so in a dedicated function.
I simplified the main function, which calls the above code to the bare 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);
}
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.
Thank you Pat
Configure opencv with "BUILD_WITH_STATIC_CRT" off, its on by default. 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.
With your code, built with Visual Studio 2015, I get your debug assertion
__acrt_first_block == header
.The following code does not give the assertion, I simply changed
std::vector<cv::Mat> ColorChannels;
tocv::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).
Tested with this image (taken from https://raw.githubusercontent.com/zukun/rcc/master/shape_sharing/code_release/pascal/VOC2010/JPEGImages/2008_000027.jpg)
Standard output is: