'**SUMMARY**
Do a binary threshold the image, changing all values below thresh to maxv
and all above to black.  If a color tuple is provided, each color channel
is thresholded separately.
If threshold is -1 (default), an adaptive method (OTSU\'s method) is used.
If then a blocksize is specified, a moving average over each region of block*block
pixels a threshold is applied where threshold = local_mean - p.
**PARAMETERS**
* *thresh* - the threshold as an integer or an (r,g,b) tuple , where pixels below (darker) than thresh are set to to max value,
and all values above this value are set to black. If this parameter is -1 we use Otsu\'s method.
* *maxv* - The maximum value for pixels below the threshold. Ordinarily this should be 255 (white)
* *blocksize* - the size of the block used in the adaptive binarize operation.
.. Warning::
This parameter must be an odd number.
* *p* - The difference from the local mean to use for thresholding in Otsu\'s method.
**RETURNS**
A binary (two colors, usually black and white) SimpleCV image. This works great for the findBlobs
family of functions.
**EXAMPLE**
Example of a vanila threshold versus an adaptive threshold:
>>> img = Image("orson_welles.jpg")
>>> b1 = img.binarize(128)
>>> b2 = img.binarize(blocksize=11,p=7)
>>> b3 = b1.sideBySide(b2)
>>> b3.show()
**NOTES**
`Otsu\'s Method Description<http://en.wikipedia.org/wiki/Otsu\'s_method>`
**SEE ALSO**
:py:meth:`threshold`
:py:meth:`findBlobs`
:py:meth:`invert`
:py:meth:`dilate`
:py:meth:`erode`'

def binarize(self, thresh=(-1), maxv=255, blocksize=0, p=5):

    if is_tuple(thresh):
        r = self.getEmpty(1)
        g = self.getEmpty(1)
        b = self.getEmpty(1)
        cv.Split(self.getBitmap(), b, g, r, None)
        cv.Threshold(r, r, thresh[0], maxv, cv.CV_THRESH_BINARY_INV)
        cv.Threshold(g, g, thresh[1], maxv, cv.CV_THRESH_BINARY_INV)
        cv.Threshold(b, b, thresh[2], maxv, cv.CV_THRESH_BINARY_INV)
        cv.Add(r, g, r)
        cv.Add(r, b, r)
        return Image(r, colorSpace=self._colorSpace)
    elif (thresh == (-1)):
        newbitmap = self.getEmpty(1)
        if blocksize:
            cv.AdaptiveThreshold(self._getGrayscaleBitmap(), newbitmap, maxv, cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C, cv.CV_THRESH_BINARY_INV, blocksize, p)
        else:
            cv.Threshold(self._getGrayscaleBitmap(), newbitmap, thresh, float(maxv), (cv.CV_THRESH_BINARY_INV + cv.CV_THRESH_OTSU))
        return Image(newbitmap, colorSpace=self._colorSpace)
    else:
        newbitmap = self.getEmpty(1)
        cv.Threshold(self._getGrayscaleBitmap(), newbitmap, thresh, float(maxv), cv.CV_THRESH_BINARY_INV)
        return Image(newbitmap, colorSpace=self._colorSpace)
