Fast 2d histogram in OpenCL with PyOpenCL
<p>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).</p>

<p>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. </p>

<pre><code>hist, xbins, ybins = np.histogram2d(hsv_channels[0].ravel(), saturation_channel.ravel(), [180,256],[[0,180],[0,256]])
</code></pre>

<p>I'm <em>hoping</em> 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. </p>

<p>How would I get started with this? </p>

<p>EDIT: </p>

<p>I've thought about this a little more. I think the steps on the GPU I want to do are roughly the following: </p>

<ol>
<li>Turn image into a 1d array (if 10x10, turn into 100 long array)</li>
<li>Upload image to GPU</li>
<li>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.</li>
<li>(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. </li>
<li>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.</li>
<li>Create a new empty set of bins. </li>
<li>(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.</li>
<li>(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 #. </li>
</ol>

<p>Still, I don't know enough about GPU things with PyOpenCL (or OpenCL overall) to do this correctly. </p>