How to use Imtool adjust contrast function in code

2 views (last 30 days)
Hello,
I am analysing some cracks in a surface by edge detection and the image processing toolbox. I have attached a file (matrix) which contains distance data to the surface, taken from a sensor.
After using a sobel filter I can see cracks on the surface, after using the imtool contrast feature.
When I load gs_filt with imtool I can change the contrast of that "image" in asufficient way with Scale Display Range (Eliminate outliers, 2%). I want to do exactly this in my code, so that I can perform further steps.
I tried rescale(), imadjust() and stretchlim(). But I could not make it work. I guess the problem is, that filtering the data results in negative and positive values. The negative ones are the cracks, the positive ones the intact surface. Those negative values disappear when I transform into grayscale (mat2gray).
Any help will be apreciated :)
Greetings,
Valentin
% load file
load('m_AofI.mat')
% filter with sobel
gs_filt = filter2(fspecial('sobel'), m_AofI, 'valid');
% almost only black image
figure; imshow(gs_filt)
% stretchlim() results in only the positive high value and 0 for the low value.
% I need the the negative value.
low_high = stretchlim(gs_filt, [0.01 0.99])
% with imtool contrast I can choose a fitting contrast. HOW TO DO THIS IN CODING?
imtool(gs_filt); % set Eliminate outliers to 2%

Answers (1)

Anay
Anay on 1 Jul 2025
Hi Valentin,
I understand that you want to adjust the contrast of your image through code to achieve results like the contrast adjustment by “imtool”.
The “Adjust Contrast” tool removes 2% outliers. You can follow the below link to know more about the “Adjust Contrast” tool:
You can implement this outlier elimination to produce similar results by following the below steps:
  1. Compute the 2nd and 98th percentile of the image data. You can adjust these percentiles to get results as per your preference.
  2. Just like the “Adjust Contrast” tool, discard the lowest 2% and highest 2% to remove outliers.
  3. Rescale the image intensities such that lowest 2% intensity pixels are 0 (black) and highest 2% intensity pixels are white (1). All the pixels in between are scaled linearly.
  4. Clip the values outside [0 1] and display the image.
You can use the following code for reference:
% Load data
load('m_AofI.mat');
% Compute 2nd and 98th percentiles (eliminate 2% outliers on each end)
p = prctile(gs_filt(:), [2, 98]); % [2% low, 98% high]
% Linearly map [p(1), p(2)] to [0, 1] and clip outliers
gs_adjusted = (gs_filt - p(1)) / (p(2) - p(1)); % Scale to [0,1]
gs_adjusted(gs_adjusted < 0) = 0; % Clip values below 0 (darker than 2%)
gs_adjusted(gs_adjusted > 1) = 1; % Clip values above 1 (brighter than 98%)
% Display adjusted image
imshow(gs_adjusted);
Here is the result I got for the data you provided:

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!