Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

HSV white color range in JS

I would like to track white color using webcam and Opencv.js. Here is the question/answer with python. I want to do exactly the same with javascript and tried this

const hsv = new cv.Mat();
cv.cvtColor(initialImage, hsv, cv.COLOR_BGR2HSV)

const lower_white = [0, 0, 0];
const upper_white = [0, 0, 255];

const low = new cv.Mat(hsv.rows, hsv.cols, hsv.type(), lower_white);
const high = new cv.Mat(hsv.rows, hsv.cols, hsv.type(), upper_white);

const dst = new cv.Mat();
cv.inRange(hsv, low, high, dst);

But it doesn't work. I'm getting the following error

Uncaught TypeError: Incorrect number of tuple elements for Scalar: expected=4, actual=3

Error is occurring on this line

const low = new cv.Mat(hsv.rows, hsv.cols, hsv.type(), lower_white);

The problem is that it wants lower_white array to have 4 elements instead of 3.

What is the 4th element in HSV format?

I also tried to put the 4th element from 0 to 255

    const lower_white = [0, 0, 0, 0];
    const upper_white = [0, 0, 255, 255];

In this case, there is no error but it doesn't seem right. It doesn't work. Any suggestions or explanations of what should be lower_white and upper_white values?