'**SUMMARY**
This is the main entry point for palette generation. A palette, for our purposes,
is a list of the main colors in an image. Creating a palette with 10 bins, tries
to cluster the colors in rgb space into ten distinct groups. In hue space we only
look at the hue channel. All of the relevant palette data is cached in the image
class.
**PARAMETERS**
* *bins* - an integer number of bins into which to divide the colors in the image.
* *hue* - if hue is true we do only cluster on the image hue values.
* *centroids* - A list of tuples that are the initial k-means estimates. This is handy if you want consisten results from the palettize.
**RETURNS**
Nothing, but creates the image\'s cached values for:
self._mDoHuePalette
self._mPaletteBins
self._mPalette
self._mPaletteMembers
self._mPalettePercentages
**EXAMPLE**
>>> img._generatePalette(bins=42)
**NOTES**
The hue calculations should be siginificantly faster than the generic RGB calculation as
it works in a one dimensional space. Sometimes the underlying scipy method freaks out
about k-means initialization with the following warning:
UserWarning: One of the clusters is empty. Re-run kmean with a different initialization.
This shouldn\'t be a real problem.
**SEE ALSO**
ImageClass.getPalette(self,bins=10,hue=False
ImageClass.rePalette(self,palette,hue=False):
ImageClass.drawPaletteColors(self,size=(-1,-1),horizontal=True,bins=10,hue=False)
ImageClass.palettize(self,bins=10,hue=False)
ImageClass.binarizeFromPalette(self, palette_selection)
ImageClass.findBlobsFromPalette(self, palette_selection, dilate = 0, minsize=5, maxsize=0)'

def _generatePalette(self, bins, hue, centroids=None):

    if ((self._mPaletteBins != bins) or (self._mDoHuePalette != hue)):
        total = float((self.width * self.height))
        percentages = []
        result = None
        if (not hue):
            pixels = np.array(self.getNumpy()).reshape((-1), 3)
            if (centroids == None):
                result = scv.kmeans(pixels, bins)
            else:
                if isinstance(centroids, list):
                    centroids = np.array(centroids, dtype='uint8')
                result = scv.kmeans(pixels, centroids)
            self._mPaletteMembers = scv.vq(pixels, result[0])[0]
        else:
            hsv = self
            if (self._colorSpace != ColorSpace.HSV):
                hsv = self.toHSV()
            h = hsv.getEmpty(1)
            cv.Split(hsv.getBitmap(), None, None, h, None)
            mat = cv.GetMat(h)
            pixels = np.array(mat).reshape((-1), 1)
            if (centroids == None):
                result = scv.kmeans(pixels, bins)
            else:
                if isinstance(centroids, list):
                    centroids = np.array(centroids, dtype='uint8')
                    centroids = centroids.reshape(centroids.shape[0], 1)
                result = scv.kmeans(pixels, centroids)
            self._mPaletteMembers = scv.vq(pixels, result[0])[0]
        for i in range(0, bins):
            count = np.where((self._mPaletteMembers == i))
            v = (float(count[0].shape[0]) / total)
            percentages.append(v)
        self._mDoHuePalette = hue
        self._mPaletteBins = bins
        self._mPalette = np.array(result[0], dtype='uint8')
        self._mPalettePercentages = percentages
