hybridgroup / gocv

Go package for computer vision using OpenCV 4 and beyond. Includes support for DNN, CUDA, and OpenCV Contrib.
https://gocv.io
Other
6.42k stars 853 forks source link

Memory leak in ORB detector #1139

Open danhab99 opened 6 months ago

danhab99 commented 6 months ago

Description

I have a plain and simple implementation of ORB I got using a tutorial. When I try running this function 1000 times I see huge memory usage and it doesn't go down. I'm confident I closed all the gocv resources, I'm not passing mats between goroutines, it's just a pure function.

Steps to Reproduce

import (
    "image"
    "image/draw"

    "gocv.io/x/gocv"
)

func convertToGray(img image.Image) (*image.Gray, error) {
    bounds := img.Bounds()
    gray := image.NewGray(bounds)

    draw.Draw(gray, bounds, img, bounds.Min, draw.Src)

    return gray, nil
}

func Process(img image.Image) (result ProcessResults, err error) {
    gray, err := convertToGray(img)
    if err != nil {
        panic(err)
    }

    mat, err := gocv.ImageGrayToMatGray(gray)
    if err != nil {
        panic(err)
    }
    defer mat.Close()

    orb := gocv.NewORB()
    defer orb.Close()

    mask := gocv.NewMat()
    defer mask.Close()

    keypoints, description := orb.DetectAndCompute(mat, mask)
    defer description.Close()

    result.Description = description.ToBytes()
    result.DescCols = description.Cols()
    result.DescRows = description.Rows()
    result.DescType = description.Type()

    result.Keypoints = keypoints

    return
}

Your Environment