You can first convert the indexed image and its color correction to a grayscale image using the IND2GRAY function:
[X,map] = imread('SecCode.php.png'); %# Read the indexed image and colormap grayImage = ind2gray(X,map); %# Convert to grayscale image
Then you can apply the code you have above:
level = graythresh(grayImage); %
EDIT:
If you want to make this a generalized approach for any type of image, here is one way to do this:
%
This should cover most types of images, with the exception of some outliers (for example, alternative color spaces for TIFF images ).
source share