Ask Your Question

Revision history [back]

Opencv.js - grabcut and GC_INIT_WITH_MASK

Hello everyone,

I tried to replicate the Interactive Foreground extraction of python here into Javascript.

The first part with GC_INIT_WITH_RECT (which is already in the JS doc) works fine. But then I try to refine the selection with GC_INIT_WITH_MASK.

This is my code so far:

let src = cv.imread("canvasInput");
  cv.cvtColor(src, src, cv.COLOR_RGBA2RGB, 0);
  let newmask = cv.imread("canvasAdjust");
  let mask = new cv.Mat.ones(newmask.rows, newmask.cols, newmask.type());
  src.setTo(new cv.Scalar(0, 0, 255), mask);
  let bgdModel = new cv.Mat();
  let fgdModel = new cv.Mat();
  let rect = new cv.Rect();

// Setting the mask
for (let i = 0; i < newmask.rows; i++) {
    for (let j = 0; j < newmask.cols; j++) {
      if (newmask.ucharPtr(i, j)[1] == 128) {
        // if green, tell the mask that it's foreground
            mask.ucharPtr(i, j)[0] = cv.GC_FGD;
            mask.ucharPtr(i, j)[1] = cv.GC_FGD;
            mask.ucharPtr(i, j)[2] = cv.GC_FGD;
          }
      if (mask.ucharPtr(i, j)[0] == 255) {
            // if red, tell the mask that it's background
             mask.ucharPtr(i, j)[0] = cv.GC_BGD;
             mask.ucharPtr(i, j)[1] = cv.GC_BGD;
             mask.ucharPtr(i, j)[2] = cv.GC_BGD;
      }

    }
  }
  cv.grabCut(src, mask, rect, bgdModel, fgdModel, 1, cv.GC_INIT_WITH_MASK);

But all it does is so far is some errors with random numbers like Uncaught 6566864 . Pointing out in the line cv.grabCut.

Can you help me to figure out if my approach of setting the mask is wrong and where exactly?

Thank you in advance.

Opencv.js - grabcut and GC_INIT_WITH_MASK

Hello everyone,

I tried to replicate the Interactive Foreground extraction of python here into Javascript.

The first part with GC_INIT_WITH_RECT (which is already in the JS doc) works fine. But then I try to refine the selection with GC_INIT_WITH_MASK.

This is my code so far:

let src = cv.imread("canvasInput");
  cv.cvtColor(src, src, cv.COLOR_RGBA2RGB, 0);
  let newmask = cv.imread("canvasAdjust");
  let mask = new cv.Mat.ones(newmask.rows, newmask.cols, newmask.type());
  src.setTo(new cv.Scalar(0, 0, 255), mask);
  let bgdModel = new cv.Mat();
  let fgdModel = new cv.Mat();
  let rect = new cv.Rect();

// Setting the mask
for (let i = 0; i < newmask.rows; i++) {
    for (let j = 0; j < newmask.cols; j++) {
      if (newmask.ucharPtr(i, j)[1] == 128) {
        // if green, tell the mask that it's foreground
            mask.ucharPtr(i, j)[0] = cv.GC_FGD;
            mask.ucharPtr(i, j)[1] = cv.GC_FGD;
            mask.ucharPtr(i, j)[2] = cv.GC_FGD;
          }
      if (mask.ucharPtr(i, (newmask.ucharPtr(i, j)[0] == 255) {
            // if red, tell the mask that it's background
             mask.ucharPtr(i, j)[0] = cv.GC_BGD;
             mask.ucharPtr(i, j)[1] = cv.GC_BGD;
             mask.ucharPtr(i, j)[2] = cv.GC_BGD;
      }

    }
  }
  cv.grabCut(src, mask, rect, bgdModel, fgdModel, 1, cv.GC_INIT_WITH_MASK);

But all it does is so far is some errors with random numbers like Uncaught 6566864 . Pointing out in the line cv.grabCut.

Can you help me to figure out if my approach of setting the mask is wrong and where exactly?

Thank you in advance.