无法在OpenCV中分配字节

时间:2014-06-23 08:20:25

标签: c# opencv memory-management memory-leaks emgucv

我正在使用 Emgu.CV 从视频文件中提取帧并从每一帧中提取面部,问题是应用程序在一段时间后内存不足。我想我释放内存并处理我创建的每个对象。这是方法,任何优化,想法将不胜感激。

public List<FileItem> ExtractFacesFromImage(FileItem selectedFrame, string outputFolderPath, int minNeighbors, double scaleFactor, int widthIncrement = 80, int heightIncrement = 102)
{
    lock (lockObj)
    {
        _currentFrame = new Image<Bgr, byte>(selectedFrame.Path);
        _gray = _currentFrame.Convert<Gray, Byte>();
        var result = new List<FileItem>();

        using (_face = new HaarCascade(_xmlFilePath))
        {
            if (_gray != null)
            {
                var facesDetected = _gray.DetectHaarCascade(_face, scaleFactor, minNeighbors,Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_ROUGH_SEARCH,new Size(20, 20));

                for (int index = 0; index < facesDetected[0].Length; index++)
                {
                    var imgName = SaveExtractedFace(outputFolderPath, facesDetected,
                                    index, widthIncrement, heightIncrement);
                    result.Add(new FileItem
                    {
                        IsSelected = true,
                        Path = imgName,
                        Name = Path.GetFileNameWithoutExtension(imgName)
                    });
                }
                DisposeObject(_gray);
                DisposeObject(_currentFrame);
            }
            return result;
        }
    }
}

private void DisposeObject(IDisposable disposable)
{
    if (disposable != null)
    {
        disposable.Dispose();
        disposable = null;
    }
}

private string SaveExtractedFace(string outputFolderPath, MCvAvgComp[][] facesDetected, int index, int widthIncrement, int heightIncrement)
{
    var f = facesDetected[0][index];

    var cropRectangle = GetNewRectangle(f.rect, _currentFrame.Width, _currentFrame.Height, widthIncrement, heightIncrement);

    _tempImage = _currentFrame.Copy(cropRectangle);
    string imgName = outputFolderPath + "\\" + UniqueNameManager.Generate() + ".jpg";
    _tempImage.Save(imgName);
    DisposeObject(_tempImage);
    return imgName;
}

2 个答案:

答案 0 :(得分:0)

请尝试运行此代码,而不是使用全局或一般变量。

public List<FileItem> ExtractFacesFromImage(FileItem selectedFrame, string outputFolderPath,
    int minNeighbors, double scaleFactor, int widthIncrement = 80, int heightIncrement = 102)
{
    lock (lockObj)
    {
        var currentFrame = new Image<Bgr, byte>(selectedFrame.Path);
        var gray = _currentFrame.Convert<Gray, Byte>();
        var result = new List<FileItem>();

        using (_face = new HaarCascade(_xmlFilePath))
        {
            if (gray != null)
            {
                var facesDetected = gray.DetectHaarCascade(_face, scaleFactor, minNeighbors,Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_ROUGH_SEARCH,new Size(20, 20));

                for (int index = 0; index < facesDetected[0].Length; index++)
                {
                    var imgName = SaveExtractedFace(outputFolderPath, facesDetected, index, widthIncrement,
                                                    heightIncrement);
                    result.Add(new FileItem
                    {
                        IsSelected = true,
                        Path = imgName,
                        Name = Path.GetFileNameWithoutExtension(imgName)
                    });
                }

            }
            return result;
        }
    }
}

private string SaveExtractedFace(string outputFolderPath, MCvAvgComp[][] facesDetected, int index, int widthIncrement, int heightIncrement)
{
    var f = facesDetected[0][index];

    var cropRectangle = GetNewRectangle(f.rect, _currentFrame.Width, _currentFrame.Height, widthIncrement, heightIncrement);

    _tempImage = _currentFrame.Copy(cropRectangle);
    string imgName = outputFolderPath + "\\" + UniqueNameManager.Generate() + ".jpg";
    _tempImage.Save(imgName);
    DisposeObject(_tempImage);
    return imgName;
}

答案 1 :(得分:-1)

我使用的是Emgu.CV 2.1版,在版本2.4中修复了问题。我认为他们修复了错误或为代码添加了一些增强功能。我建议使用版本2.4而不是版本2.1