Ask Your Question

HSV white color range in JS

asked 2019-12-05 01:59:35 -0500

Lucy8 gravatar image

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?

edit retag flag offensive close merge delete


I would like to track white color

maybe you think about that again. -- white is not really a "color", it can be any hue value in hsv.

iif you're just begin coding with this, start with an easier color, like yellow or pink

berak gravatar imageberak ( 2019-12-05 02:54:39 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-12-05 04:02:53 -0500

mvuori gravatar image

Nobody seems to know about the 4th element, but if I had to guess, JS stores even HSV images in four channels (unlike other platforms), and the fourth is the alpha channel.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-12-05 01:59:35 -0500

Seen: 37 times

Last updated: Dec 05 '19