Ask Your Question
0

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

3 answers

Sort by ยป oldest newest most voted
0

answered 2020-12-19 17:45:05 -0500

The way you created your ranges is incorrect, and initializing a Mat from a 3 column array instead of a 4 column array gives the error

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

The way to create the range works through using the factory method matFromArray(). the ranges must be created by

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

instead of

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

https://docs.opencv.org/3.4/de/d06/tu...

edit flag offensive delete link more
2

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
1

answered 2019-12-05 02:54:39 -0500

berak gravatar image

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 3,330 times

Last updated: Dec 19 '20