File Exchange

image thumbnail

Thresholding Tool

version (6.21 KB) by Robert Bemis
Interactively select intensity level for image thresholding.


Updated 01 Sep 2016

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

THRESH_TOOL launches a GUI (graphical user interface) for thresholding an intensity input image, IM. IM is displayed in the top left corner. A colorbar and IM's histogram are displayed on the bottom. A line on the histogram indicates the current threshold level. A binary image is displayed in the top right based on the selected level. To change the level, click and drag the line. The output image updates automatically.
There are two ways to use this tool.

Mode 1 - nonblocking behavior:
THRESH_TOOL(IM) launches GUI tool. You can continue using the MATLAB Desktop.Since no results are needed, the function does not block execution of other commands.

THRESH_TOOL(IM,CMAP) allows the user to specify the colormap, CMAP. If not specified, the default colormap is used.

THRESH_TOOL(IM,CMAP,DEFAULTLEVEL) allows the user to specify the default threshold level. If not specified, DEFAULTLEVEL is determined by GRAYTHRESH. Valid values for DEFAULTLEVEL must be consistent with the data type of IM for integer intensity images: uint8 [0,255], uint16 [0,65535], int16 [-32768,32767].

x = imread('rice.png');
%no return value, so MATLAB keeps running

Mode 2 - blocking behavior:
LEVEL = THRESH_TOOL(...) returns the user selected level, LEVEL, and MATLAB waits for the result before proceeding. This blocking behavior mode allows the tool to be inserted into an image processing algorithm to support an automated workflow.

[LEVEL,BW] = THRESH_TOOL(...) also returns the thresholded binary output image, BW.

x = imread('rice.png');
lev = thresh_tool(x) %MATLAB waits for GUI tool to finish

See also colormap, graythresh, im2bw.

Cite As

Robert Bemis (2021). Thresholding Tool (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (29)



Adam, someone did this already for you:
I use this a lot.

Adam Smith

I really like this tool and I've used it a lot for studying images of fibre reinforced polymers. Recently, I have been trying to modify it to allow for 2-level thresholding, where only the pixels between two intensity levels are set to a value of 1. So far, no luck. Would you be able to provide some guidance on how to do this?

Gopichandh Danala

Guilhem Chenon

Thanks a lot, very useful.

masyitah razi

Edmund Pickering

Great tool. I use this regularly. Thanks for the fantastic work.

Hirak Mazumdar

Error using ==> thresh_tool at 46

%defensive programming
Not enough input arguments. why this?


Very nice tool! Run smoothly! Thank you!

raju maher



Hey Robert Bemis, I was also experiencing the inverted text bug. I followed the steps listed in the link you provided and it corrected itself. Thanks for the insight!

Also this tool is fantastic!

Rajesh Gothwal

error occured ????
??? Undefined function or method 'thresh_tool' for input arguments of type 'uint8'.

xiang fiona

Nice work~Thanks for sharing!

Janelle Shane

Registered just so I could say thanks for sharing this very very useful program. Works great, and has saved me a ton of time.

Robert Bemis

David James, and anyone else who sees upside-down text, please see if this applies.

David James

Excellent tool, just one odd effect appears when running the gui in Matlab 2009a, where the text appears flipped vertically and horizontally

pkenned Kennedy

Hi Robert, thank you for this program, it is most helpful. I have a slight problem however, when I import a 16 bit dicom file the input image on the left appears with the jet colormap. I changed line 103 to gray but now I just get a black image. Do i need to edit dislayrange somehow?

Charles Griffin

I'm just beginning to learn image segmentation for a Masters thesis. My thesis is on remote sensing and I was told that thresholding was the way to go on this thesis. Do you have any suggestions that could narrow my research in a more directed path?

Robert Bemis

Nathan and others, thanks for all the feedback. Some users have been tripped up by the requirement that the input must be an intensity image. RGB color is not supported. I will update the function to for this at the beginning and stop immediately with a more helpful error message.

Nathan Tomlin

I apologize for cluttering the comments - didn't read carefully (and wish I could edit previous posts...). Like Benjamin said, it doesn't work if you input an RGB image. Just add:

if ndims(im) == 3
im = rgb2gray(im);

Nathan Tomlin

didn't work for me - I got the same errors as listed above

Benjamin Benson

Neat tool and nice gui. For all guys having problems with assignment error: You should load a grayscale image:
x = rgb2gray(imread('img.png'));

veysel gökhan böcekçi

Naveen Kumar

x= imread('a.jpg');
lev = thresh_tool(x,[1.0 1.0 0])

??? Assignment has more non-singleton rhs dimensions than non-singleton

Error in ==> ind2rgb at 35
rout(:,:,1) = r;

Error in ==> thresh_tool>im2rgb at 118
rgb = ind2rgb(ind,full_map);

Error in ==> thresh_tool at 110
rgb = im2rgb(im,full_map);


M Rosenbluth

Nice gui.

Sudip Ghosh

This is an excellent tool! Thanks!

piyush jain

Carlos Oliveira

Very useful tool.

ivan scardanzan

>> thresh_tool(X);
??? Assignment has more non-singleton rhs dimensions than non-singleton

Error in ==> ind2rgb at 35
rout(:,:,1) = r;

Error in ==> subimage>parse_inputs at 150
cdata = im2uint8(ind2rgb(varargin{1},varargin{2}));

Error in ==> subimage at 43
[x,y,cdata] = parse_inputs(varargin{:});

Error in ==> thresh_tool at 49

>> X=X(:,:,1);
>> thresh_tool(X);

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!