The Wikipedia snippet you provide is correct and can be used to normalize the image using the following MATLAB code:
%% Create an Example Image: rand('seed', 1982); n = 16; myImg= rand(n,n)*.2 + .5; %% Normalize the Image: myRange = getrangefromclass(myImg(1)); newMax = myRange(2); newMin = myRange(1); myImgNorm = (myImg - min(myImg(:)))*(newMax - newMin)/(max(myImg(:)) - min(myImg(:))) + newMin;
The problem with some images is that although they occupy only a small range of possible values. If your values โโcan range from 0 to 1, then black will be 0 and white will be 1. However, if your darkest spot in the image is 0.5 and your brightest is 7, then it may look blurry for your processing or user when is rendered (note that MATLAB imagesc automatically normalizes the image before displaying for this very reason).
If you look at the histogram of the image using hist (myImg (:)), you can determine how the valid values โโthat the image actually uses should be. In a normalized image, the smallest value will be 0, and the largest will be 1 (or any other range that you use).
A common mistake in implementing this equation is the incorrect placement of brackets, rather than subtracting the minimum image before scaling or adding back to "newMin".
You can see everything together in the following code and image. Please note that the original image (1) uses only a small part of the space (2), so it looks blurry when we do not allow the images to autoscale the clearance parameter. However, as soon as we normalize (3), the image has both very dark and very light values, and the histogram extends from 0 to 1 (4). Although it is not entirely clear what your code does or does not do, comparing it with this example should solve your problem.
%% Create an Example Image: rand('seed', 1982); n = 16; myImg= rand(n,n)*.2 + .5; %% Normalize the Image: myRange = getrangefromclass(myImg(1)); newMax = myRange(2); newMin = myRange(1); myImgNorm = (myImg - min(myImg(:)))*(newMax - newMin)/(max(myImg(:)) - min(myImg(:))) + newMin; %% Display the Image: figure(42); clf; % Display the original: subplot(2,2,1); imagesc(myImg); set(gca, 'clim', [0,1]);; title('(1) Original Image'); % Display the hist of the original: subplot(2,2,3); hist(myImg(:)) xlim([0,1]); title('(2) Histogram Of Original Image'); % Display the normalized image: subplot(2,2,2); imagesc(myImgNorm); title('(3) Normalized Image'); % Display the hist of the normalized image: subplot(2,2,4); hist(myImgNorm(:)) title('(4) Histogram of Normalized Image'); xlim([0,1]); colormap gray
EDIT:
In addition, there are some important points for taking notes on how getrangefromclass (...) will work on your problem. This function returns the "default display range of the image based on its class", that is, returns what, according to MATLAB, is a reasonable range of values โโfor this data type to represent the image. For uint8 data, this is [0, 255]. For int16, this is [-32768, 32767]. For your case, double, the range [0, 1] is not because the minimum and maximum values, but because it is a regular and double data type, have a special representation that makes this range quite reasonable. Note that the range has nothing to do with your data. If you have data small or large than min and max, it will not be at all what MATLAB thinks, it is good for photographs. In the case of a double or single value, the value can be much larger. To normalize the values โโbetween [0, 1], we can use the code that we discussed.
In this case, we create a random image with large positive and negative values, but we will scale them all between zero and one. That is, we make the darkest color 0 and the lighted color 1 --- whereas the smallest were negative thousands, and the largest were positive thousands. However, pay attention to how the shape of the histogram remains unchanged, and the values โโalong the x axis change by 0.1. This should demonstrate why the MATLAB range is [0,1], but your min / max is different - that's fine, and your normalization code will fix everything between zero and one.
randn('seed', 1982); myImg = randn(n,n)*1000; % Normalize the Image: myRange = getrangefromclass(myImg(1)); newMax = myRange(2); newMin = myRange(1); myImgNorm = (myImg - min(myImg(:)))*(newMax - newMin)/(max(myImg(:)) - min(myImg(:))) + newMin; % Display the Image: figure(42); clf; % Display the original: subplot(2,2,1); imagesc(myImg); % set(gca, 'clim', [0,1]);; title('(1) Original Image'); colorbar % Display the hist of the original: subplot(2,2,3); hist(myImg(:)) title('(2) Histogram Of Original Image'); axis tight; % Display the normalized image: subplot(2,2,2); imagesc(myImgNorm); title('(3) Normalized Image'); colorbar % Display the hist of the normalized image: subplot(2,2,4); hist(myImgNorm(:)) title('(4) Histogram of Normalized Image'); axis tight; colormap gray