'**SUMMARY**
Perform a threshold operation on the given image. This involves iterating
over the image and comparing each pixel to the model. If the pixel is in the
model it is set to be either the foreground (white) or background (black) based
on the setting of mIsBackground.
**PARAMETERS**
* *img* - the image to perform the threshold on.
**RETURNS**
The thresholded image.
**EXAMPLE**
>>> cm = ColorModel()
>>> cm.add(color.RED)
>>> cm.add(color.BLUE)
>>> result = cm.threshold(Image("lenna")
>>> result.show()'

def threshold(self, img):

    a = 0
    b = 255
    if (self.mIsBackground == False):
        a = 255
        b = 0
    rs = np.right_shift(img.getNumpy(), self.mBits).reshape((-1), 3)
    mapped = np.array(map(self.mData.has_key, map(np.ndarray.tostring, rs)))
    thresh = np.where(mapped, a, b)
    return Image(thresh.reshape(img.width, img.height))
