如何发布从函数返回的Mat?

时间:2015-12-11 13:45:05

标签: java android opencv mat

如果在函数内部我创建了一个新Mat,然后我返回Mat,那么Mat对象什么时候会被释放?

说我有这个样本函数:

    Mat sampleFunction(Mat frameHSV) {
    Mat filtered2 = new Mat();  // create a new Mat object 
    Mat frameRGB = new Mat();    // create a new Mat object
    Imgproc.cvtColor(frameHSV, frameRGB, Imgproc.COLOR_HSV2RGB); // convert to RGB

    Core.subtract(frameRGB, frameHSV, filtered2); // subtract Mats, just a made up operation here
    frameRGB.release(); // release the RGB mat to clear up memory
    return filtered2;
    }

所以我传入frameHSV,然后在示例Function-filtered2和frameRGB中创建了两个Mats。 FrameRGB在函数结束时发布,但Filtered2返回,因此从未发布。

如何发布filtered2?或者当我退回垫子时自动释放?

2 个答案:

答案 0 :(得分:0)

愚蠢的问题,但谢谢Selvin的快速回答。

正如塞尔文所说,只是发布被称为

的结果
  result = sampleFunction(frameHSV)
  result.release()  // releases the Mat that was returned from the function

答案 1 :(得分:0)

如果我错了,请纠正我,但using System; using System.Text; using System.Web; using System.Web.UI; using System.Threading; namespace DemoGenericHandler { public class MyHandler : System.Web.IHttpHandler { public virtual bool IsReusable { get { return false; } } public virtual void ProcessRequest (HttpContext context) { //if you need get the value sent from client (ajax-post) //string valueSendByClient = context.Request.Form["data"] ?? string.Empty; //you must use a library like JSON.NET (newtonsoft) to serialize an object //here for simplicity i'll build the json object in a string variable: string jsonObj = "{\"Value1\": \"1\",\"Value2\": \"2\",\"Value3\": \"3\"}"; //await 5 seconds: (imitates the time that your wcf services take) Thread.Sleep(5000); //send the result to the client context.Response.ContentType = "text/json"; context.Response.Write(jsonObj); } } } 应在函数调用结束后自动释放。由于变量Mat的生命周期在函数调用之后结束。