You could try to erase all the text with an erosion and then subtract the result from the grayscale image or use a tophat transform instead to uniform your background for global thresholding! Here you can find an example code! And why not use the existing adaptiveThreshold function ?