1

I have a camera with a double lens connected via USB to my computer and I have created an anaglyphic 3D video using OpenCV on Javascript.

The problem is, I reach an error called OutOfMemory. I have checked for leak memory from my variables that are in a loop and I could see that everyone remains with its same pointer, except one, here the code:

I had to debug and I realize that this variable changed its pointer every time the loop started again:

CorrIzq = dstIzq.roi(RectImgIzq);

So, I commented and I pass it directly on addWeighted() function, but now it assigned a new pointer to dstEstereoscopico (this is a variable Mat). So, I put it as const and seems it has the same pointer every loop, but still the memory overflow.

Can you help me, please?

Do you know if this could be a bug or maybe I'm using it wrong?

function processVideo() {
    begin = Date.now();
    try {
      //dstEstereoscopico = new cv.Mat(videoEstereoscopico.height - 15, videoEstereoscopico.width - 20, cv.CV_8UC1);
      console.log(dstEstereoscopico);

      //Dibujando en el objeto Canvas
      capIzq.read(srcIzq);
      capDer.read(srcDer);
      console.log(capIzq);


      //Obteniendo canales RGB
      cv.split(srcIzq, Array_rgbValores);
      Array_rgbValores.set(1, cv.Mat.zeros(canvasFrameIzq.height, canvasFrameIzq.width, cv.CV_8UC1));
      Array_rgbValores.set(2, cv.Mat.zeros(canvasFrameIzq.height, canvasFrameIzq.width, cv.CV_8UC1));
      cv.cvtColor(srcIzq, dstIzq, cv.COLOR_RGBA2GRAY);
      cv.merge(Array_rgbValores, dstIzq);

      //Aplicando corrección de posicionamiento
      //CorrIzq = dstIzq.roi(RectImgIzq);
      //console.log(CorrIzq);

      cv.split(srcDer, Array_rgbValores);
      Array_rgbValores.set(0, cv.Mat.zeros(canvasFrameIzq.height, canvasFrameIzq.width, cv.CV_8UC1));
      Array_rgbValores.set(1, cv.Mat.zeros(canvasFrameIzq.height, canvasFrameIzq.width, cv.CV_8UC1));
      cv.cvtColor(srcDer, dstDer, cv.COLOR_RGBA2GRAY);
      cv.merge(Array_rgbValores, dstDer);

      //CorrDer = dstDer.roi(RectImgDer);


      //corrDer = dstDer,
      cv.addWeighted(dstIzq.roi(RectImgIzq), 0.7, dstDer.roi(RectImgDer), 0.5, 1, dstEstereoscopico);
      contextDer.clearRect(0,0, canvasFrameDer.width, canvasFrameDer.height);
      contextIzq.clearRect(0,0, canvasFrameIzq.width, canvasFrameIzq.height);
      contextDestEste.clearRect(0,0, videoEstereoscopico.width, videoEstereoscopico.height);
      cv.imshow("videoEstereoscopico", dstEstereoscopico);
      // schedule next one.

      /*srcIzq.delete();
      srcDer.delete();
      dstIzq.delete();
      dstDer.delete();
      Array_rgbValores.delete();*/
      //dstEstereoscopico.delete();
      //dstEstereoscopico = [];

      //CorrIzq.delete();
      //CorrDer.delete();

      //console.log(performance.memory);
      //console.log("El contador es: " + contador++);
      //console.log("Tiempo transcurrido: " + (Date.now() - tiempoInicio)/1000 + " s");
      clearTimeout(id_SetProVid);
      delay = ((Date.now() - begin));
      id_SetProVid = setTimeout(processVideo, delay);

    } catch (err) {
      console.log("El error se ha cacheado");
      this.printError(err);
    }
  }
  setTimeout(processVideo, 0);
};
4

1 回答 1

1

昨天我解决了这个问题。实际上,问题也出在这些代码行上:

Array_rgbValores.set(1, cv.Mat.zeros(canvasFrameIzq.height, canvasFrameIzq.width, cv.CV_8UC1));
Array_rgbValores.set(2, cv.Mat.zeros(canvasFrameIzq.height, canvasFrameIzq.width, cv.CV_8UC1));

我认为这不是赖特的用途。但是,我通过以下方式更改它们:

R = Array_rgbValoresIzq.get(0);
G = new cv.Mat.zeros(videoIzq.height, videoIzq.width, cv.CV_8UC1);
B = new cv.Mat.zeros(videoIzq.height, videoIzq.width, cv.CV_8UC1);
A = Array_rgbValoresIzq.get(3);

Array_rgbBuffIzq.push_back(R);
Array_rgbBuffIzq.push_back(G);
Array_rgbBuffIzq.push_back(B);
Array_rgbBuffIzq.push_back(A);

我必须创建一个新的 MatVector 和另外 4 个 Mats。好消息是这解决了内存泄漏问题,而且效果很好。

于 2019-01-13T19:04:12.687 回答