这段代码工作正常,直到最后一行.它将正确的图像保存在磁盘上但在退出函数后显示"内存泄漏" - 堆损坏.我读过Mat不需要明确发布.在我的情况下,它会在释放和w/o释放时崩溃.请帮忙.
void CannyEdgeDetectionFilter::applyFilter(Mat& mat, Mat& mixedBandsMat)
{
//Mat mixedBandsMat;
vector<Mat> bandWiseImages;
split(mat, bandWiseImages);
//! Evaluate numChannels to be filtered in the input image
int numChannels = mat.channels();
int type = mat.type();
//! Multiplied by 8 to get bits from Bytes
int singleChannelDepth = 8*mat.elemSize1();
for (int i = 0; i < numChannels; i++)
{
Canny(bandWiseImages[i], bandWiseImages[i], m_LowerThreshold,
m_UpperThreshold, m_Kernel.rows);
}
//! Creating filteredImgMat in order to set DataValues
mixedBandsMat.create(mat.rows, mat.cols, mat.type());
//! Unifying the channels back to the output image
merge(bandWiseImages, mixedBandsMat);
#if 1
//Release bandWiseImages Mat memory
int bandWiseVecSize = bandWiseImages.size();
for(int i = 0; i < bandWiseVecSize; i++)
bandWiseImages[i].release();
bandWiseImages.clear();
//fromTo.clear();
#endif
imwrite("D:\\testAfterCannyEdgeDetetionFilter.jpg", mixedBandsMat);
mixedBandsMat.release();
}
Run Code Online (Sandbox Code Playgroud)
有了这些小信息,我只能给你一些支持,但没有真正的解决方案:
1.)我猜你使用的是Win7。所以请访问Dr. Memory并安装它(将路径添加到系统变量中)。然后您可以使用以下命令启动应用程序:drmemory.exe -no_follow_children C:\\the_path\\YourExecutable.exe argv[1] ... argv[n]。-no_follow_children 用于忽略其他第三方代码。运行此命令。Dr. Memory 会将结果写入C:\Users\NAMEHERE\AppData\Roaming\Dr. Memory\。看看吧,也许你现在有了提示。如果没有 -> 将其发布在这里。=)
2.)当您在没有压缩参数的情况下写入图像时,OpenCV 有时会崩溃。我已经经历过一段时间了,所以我总是给 imwrite 函数提供一个 int 参数向量:
vector<int> crparam;
crparam.push_back(CV_IMWRITE_PNG_COMPRESSION);
cv::imwrite("D:\\testAfterCannyEdgeDetetionFilter.png", mixedBandsMat, crparam);
Run Code Online (Sandbox Code Playgroud)