MATLAB: Adjusting CData to match CLim for grayscale Image

cdataclimcolormapgrayscaleimadjust()image analysisimage processingImage Processing Toolboximshow

I have a grayscale image in a figure displayed through imshow. I adjust the axes CLim limits [a b] to improve it's contrast to desired levels. How can I adjust the image matrix (CData) to match the image displayed by my CLim limits? I've tried imadjust function i.e. Image2=imadjust(Image1,[a b],[]) , but the resultant Image2 contrast doesn't look like the one displayed by CLim limits [a b].

Best Answer

t = double(max(min(Image1, b),a));
adjusted_image = (t - a) / (b - a);
now adjusted_image is in the range 0 (corresponding to a) to 1 (corresponding to b). Remember to caxis([0 1])