eduardolundgren / tracking.js

A modern approach for Computer Vision on the web
http://trackingjs.com
Other
9.43k stars 1.44k forks source link

How to track custom colors #175

Closed kushalvyas closed 7 years ago

kushalvyas commented 7 years ago

The Api mentions

var mTracker = new tracking.ColorTracker(['magenta', 'cyan', 'yellow']) ; var redcolor = ["#ff0000", "#e50000", "#cc0000", "#b20000", "#990000", "#7f0000", "#660000", "#4c0000"] myTracker.setColors(redcolor);

Is there support for a custom color such as #ff0000 or red ?

On writing this, there is no visible tracking seen. However, it seems to work in camera(colors) example.

remiglobal commented 7 years ago

hi,

actually im just started with trackingjs this morning. i have the same problem with you. i want to track red color.

from my investigation, you can duplicate this section in build/tracking.js

tracking.ColorTracker.registerColor('yellow', function(r, g, b) { var threshold = 50, dx = r - 255, dy = g - 255, dz = b - 0;

if ((r - b) >= threshold && (g - b) >= threshold) {
  return true;
}
return dx * dx + dy * dy + dz * dz < 10000;

});

change it to red. now you can include red in the ColorTracker object initialization.

however, i am still figuring out the treshold, dx, dy, and dz values...and also the condition executed after that.

i think the dx value might be r - 255. while dy = g-0 and dz = b-0.

have a look on this example:

https://github.com/eduardolundgren/tracking.js/blob/master/examples/color_video.html

I hope somebody can shed a light on this.

thanks

remiglobal commented 7 years ago

ah finally i understand it. please refer to this segment of the documentation. https://trackingjs.com/docs.html#color-tracker

excerpt...

How do I register my own color? By default tracking.js color tracker provides out of the box three default colors, they are: magenta, cyan and yellow. In addition to those, you can register any custom color you want to track, it's very simple, let's assume the color you want to track is green. In the RGB color space the green color could be some value close to (r, g, b) = (0, 255, 0), where (r, g, b) stands for red, green and blue, respectively. Once you understand the color to track in the RGB color space, it's time to register your color using tracking.ColorTracker.registerColor.

tracking.ColorTracker.registerColor('green', function(r, g, b) { if (r < 50 && g > 200 && b < 50) { return true; } return false; }); Note that the custom color function returns true to any value that the g value is close to 255, to make sure we exclude other colors that could fit with the green RGB pattern, it also checks r and b values to make sure they are below 50, hence close to (r, g, b) = (0, 255, 0).

that should do it.

kushalvyas commented 7 years ago

Thank you very much. It did work !

negnouga commented 7 years ago

hi i want track orange color with opencv and python using raspberry pi3 can someone help me please

Adilmar commented 6 years ago

Hi does anyone know how to return the detected color sequence without obeying this string var tracker = new tracking.ColorTracker (['magenta', 'cyan', 'yellow'));