I'm trying to do a 2d histogram in Python on an HSV image, but even using numpy and opencv it isn't fast enough (I'm doing it with video actually, but consider each frame to be just an image).
I'm looking for the Hue value that is most saturated. Currently I have the following code, which works ok, but is just too slow.
hist, xbins, ybins = np.histogram2d(hsv_channels[0].ravel(), saturation_channel.ravel(), [180,256],[[0,180],[0,256]])
I'm hoping to do this with PyOpenCL instead, and push the computation to the GPU, but aside from the hello world programs in OpenCL. I've found some papers on doing them, but I'm unsure where to start.
How would I get started with this?
EDIT:
I've thought about this a little more. I think the steps on the GPU I want to do are roughly the following:
- Turn image into a 1d array (if 10x10, turn into 100 long array)
- Upload image to GPU
- split image into n slices for processing, where n is the number of parallel compute units. Or each can reference a specific range on this array.
- (Map) For each compute unit, allocate 180 'bins' that can contain 256 other bins each. The innermost content of each is just an integer for counting.
- For each hue (one of 180 bins), count how many of that hue there are for each saturation level (the other 256 bins). Do this for the sub-section of the array that can be counted on.
- Create a new empty set of bins.
- (Reduce) For all of these bin counts, then merge them together (adding values). I'm unsure if I need to wait until they are all done, or just each in sequence to merge in with the empty bins from above.
- (determine final answer) For the final set of bins, loop through them and find the maximum saturation value for that hue, and store this. Now find the hue with the maximum saturation. As the final answer, return this hue # and this maximum saturation #.
Still, I don't know enough about GPU things with PyOpenCL (or OpenCL overall) to do this correctly.