I am trying to convert an OpenCV image (like cv :: Mat) into a matlab-style format, as this is what the rest of the program requires. For this, I use the following code:
inline double* ConvertCVImageToMATLABImage(Mat &CvImage)
{
std::vector<cv::Mat> ColorChannels;
cv::split(CvImage, ColorChannels);
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 statement:
__acrt_first_block == header
in the last line (return MatlabImage). Tracking the source of approval seems to be related to the deployment of vector ColorChannels. I tried several ways to do this, i.e. using .clear, using the swap trick, or freeing every element in the vector, but the statement remains.
If it is built into the main function of a C ++ program, this code works fine, it simply will not be in a special function.
I simplified the main function, which calls the code above minimal:
void main(void)
{
cv::Mat CvImage = imread("E:\\VOC2012\\VOCdevkit\\VOC2012\\JPEGImages\\2008_000027.jpg", CV_LOAD_IMAGE_COLOR);
double* Image = ConvertCVImageToMATLABImage(CvImage);
}
:
Visual Studio 2015. , (), , debug_heap.cpp, Line 980.
.