Ask Your Question

browniegirl's profile - activity

2016-01-23 19:46:37 -0600 commented question Memory leak using copyTo UMat

I found a similar problem and a simple workaround I don't know why works. Simply use the UMat with some OpenCV call. For example this code works:

while(loop)
{
   cv::UMat inMat, outMat;
   vidInput >> inMat;
   cv::cvtColor(inMat, outMat, cv::COLOR_RGB2GRAY);
   vidOutput << outMat;
}

but this following code not calling color convert will have the same problem you are having which is running out of memory:

while(loop)
{
   cv::UMat inMat, outMat;
   vidInput >> inMat;
//   cv::cvtColor(inMat, outMat, cv::COLOR_RGB2GRAY);
   vidOutput << outMat;
}

Try calling some OpenCV function like resize or color convert that will queue up a OpenCL command. See if your memory problem goes away.

2016-01-23 01:18:01 -0600 asked a question Capture Mat copy to UMat fails on 32-bit app, works on 64-bit, timing?

Here's a interesting problem with OpenCV 3.1.0, this code fails with an cv:Exception in clEnqueue on a 32 bit build, and yes per MSVC Diagnoctics it grows to 2GB and fails. Same code/32bit-build with imshow/waitkey then no exception. On 64bit the mem usage goes up to 2GB but then drops back down, then goes back up! What is going on? Remove the im.copyTo the UMat then no memory leak... Weird.

#include "stdafx.h"
#include "opencv2\core.hpp"
#include "opencv2\core\ocl.hpp"
#include "opencv2\videoio.hpp"
#include "opencv2\highgui.hpp"
#include <queue>
#include <thread>
#include <conio.h>
#include <stdexcept>
#include <iostream>

using namespace std;
using namespace cv;
void SetupOpenCL(bool);

int main(int argc, char **argv)
{
    VideoCapture capture;
    string videoin = "c:\\file.avi";
    capture.open(videoin);
    if (capture.isOpened())
    {
        cout << "opened capture";
    }
    else
    {
        std::cout << "failed to open capture device or filename";
        exit(1);
    }

    SetupOpenCL(true); // bEnableOpenCL);

    while (1)
    {
        Mat im;
        UMat uim = im.getUMat(ACCESS_RW);
        uim.release();
        capture.read(im);
        im.copyTo(uim);

        if (uim.empty()) break;

        int frameNumber = static_cast<int>(capture.get(CAP_PROP_POS_FRAMES));
        std::cout << "frame:" << frameNumber << endl;

        //imshow("uim", uim);
        //waitKey(1);
    }
    if (capture.isOpened())
        capture.release();
}


void SetupOpenCL(bool enable)
{
    bool hocl = cv::ocl::haveOpenCL();
    if (enable && !hocl)
    {
        return;
    }
    cv::ocl::setUseOpenCL(enable);
    bool uocl = cv::ocl::useOpenCL();
}