cloister
Full Member
Offline
Posts: 138
Re: How to reduce an array to unique values?
Reply #2 - Feb 10th , 2009, 11:08pm
Colors measured from live video are going to include a lot of noise that will make nearby colors (indistinguishable to the human eye) seem different to an algorithm that doesn't account for that. So, for starters, we'll assume that you're doing some kind of de-noising step first, or that you're looking for an algorithm that is inherently robust to that kind of noise. Because you're operating on live video, I'm going to assume that you're looking for an algorithm that is FAST. Methods that rely on ArrayList or other dynamic data structures may not do the job. I would suggest you make a 3d array of "bins", and use each bin to represent a group of nearby colors in whatever colorspace you're working in. I'd recommend HSB, because the groupings better match human perception than RGB, but if you're working with video then YUV may be a more natural colorspace. Each bin can hold either a single bit, if all you care about is presence/absence of a color, or an int if you would also like to count how often each color appears so as to know which colors "really matter" to the image. Then what you do is really easy: for each frame: * start by setting all the values in bins[] to false (or zero) then for each pixel in the image, * get its H/S/B values (or Y/U/V values, as appropriate) * map those values from their native range to the range of allowable indexes for your 3-d array of bins, obtaining new indexes h/s/b (or y/u/v) * set bins[h,s,b] = true (or bins[h,s,v]++ if you're counting) Now iterate over the bins[] array for any bin that is true (or non-zero), output the color corresponding to the "center" of that bin. That is, the HSB color with values that are in the middle of each range that still falls within that bin.