Ask Your Question

knighto05's profile - activity

2020-08-27 11:58:57 -0600 received badge  Supporter (source)
2020-08-27 11:58:50 -0600 marked best answer 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 (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.

2020-08-27 11:58:50 -0600 received badge  Scholar (source)
2020-08-27 11:58:48 -0600 commented answer Opencv.js - grabcut and GC_INIT_WITH_MASK

Thank you, for the precious info ! we got it running :D

2020-08-27 06:29:31 -0600 commented answer Opencv.js - grabcut and GC_INIT_WITH_MASK

Ah, now I understand, what you're talking about. I'll attempt some stuff and I'll let you know. Thank you already :)

2020-08-26 21:07:18 -0600 received badge  Editor (source)
2020-08-26 21:07:18 -0600 edited question Opencv.js - grabcut and GC_INIT_WITH_MASK

Opencv.js - grabcut and GC_INIT_WITH_MASK Hello everyone, I tried to replicate the Interactive Foreground extraction of

2020-08-26 21:06:45 -0600 commented answer Opencv.js - grabcut and GC_INIT_WITH_MASK

euh I corrected it later, you can see the corrected version in my fiddle, but I will update the post also to make it fai

2020-08-26 14:12:34 -0600 commented answer Opencv.js - grabcut and GC_INIT_WITH_MASK

Thank you for replying: first: mmh when you says single CV_8U channel, you mean this line: let mask = new cv.Mat.ones(ne

2020-08-26 05:54:28 -0600 asked a question Opencv.js - grabcut and GC_INIT_WITH_MASK

Opencv.js - grabcut and GC_INIT_WITH_MASK Hello everyone, I tried to replicate the Interactive Foreground extraction of