File Exchange

image thumbnail

Thresholding an image

version (263 KB) by Image Analyst
Interactively threshold an image. Make a binary image.


Updated 08 Feb 2018

View Version History

View License

This program takes a color or monochrome image and lets the user interactively/manually threshold the monochrome image, or a single color band of a color image, via sliders to set the maximum and minimum thresholds. The thresholded image is a binary image that can be used as a mask image for other images. The pixels in the thresholded range are shown in the middle image as a binary image (black/white), and the original image pixels are shown masked in the left image (gray scale or color). Inputs are the low and high thresholds to start with and the image file name or image matrix. The program returns the threshold values and the last color band that was used to select the threshold. The image can be of type integer (uint8, uint16, etc.) or floating point (single, double).

Cite As

Image Analyst (2021). Thresholding an image (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (51)

Md Ruhul Amin

Image Analyst

avm, you can put the lines where you call it into your main program after you have read in your image. See the commets at the top of the threshold.m file to see some sample calls. Use one of those.


How do I use and adapt the code? I am not really sure where to start

Eline Vos

Hank Yang


Salim Amir

Hello Image Analyst, can you provide me with the part that detects the minimum and maximum threshold automatically?
Thank you.

alhaa alhas

hi Image Analyst, I delete the command line but i replaced it with values. when i run the coding, the result come out. is it possible ? thank you so much for helping me. may God bless you always.

Image Analyst

alhaa, when you call the function like you did, you need to make sure you assign something to the variables startingLowThreshold, startingHighThreshold, and imageToThreshold in advance. If you don't define them, MATLAB does not know what to pass my function. If you still have trouble, we can work it through in the Answers community forum of MATLAB Central (see drop down link above).

alhaa alhas

hi Image Analyst, I run the demo coding but the cmmnd line [lowThreshold, highThreshold] = threshold(startingLowThreshold, startingHighThreshold, imageToThreshold); , it has error . why ?

Image Analyst

Rida, you can do
binaryImage = grayScaleImage > someValue; % For example, someValue = 150 or whatever.

Rida Memon

I only need the part of code for manually setting the threshold and not GUI based. can you provide that to me ?


Hello Image Analyst,

This is a question related to thresholding. I have been using your example code for skull stripping an MRI and it works quite well - thank you! However, I am finding it very challenging to come up with a solution for the first few slices in the axial plane or the sagittal plane where the outer rim of the skull is preserved rather than the small section of brain tissue. Would you mind sharing some contact info, so I could send you my code or samples ( Do you have any recommendations for image processing functions or strategies I should try? Thanks!


asdsadasd asdadasd


thank you for sharing the codes. I work with Bruker and at the beginning, its a bit different, but i don't succeed and find how can i save the Mask(binary Image) created by the threshold function. B=dir('*.png'); this command is nt enough. i think i have missed a point.
Thank you


Jonggyu Lee

Are you a genius or something?

Irinios Yiannoukos

Hi and thank you very much for sharing this great code. How can I save the binary image?


Hi, can this be used to detect texture and extract it from an image?
example see this image. I am trying to segment the floor area where the cabinets are and keeping the borders

Guojin Feng

Thanks for this handy tool.

Angela Mu




med BT

Asif Ameer

Really awesome code. Image Analyst indeed you are great.....

Image Analyst

Tian Tian, you write code and you pass the image into the threshold function as a variable in the input argument list when you call the function from your main program.

Tian Tian

Thank you for sharing this excellent program. May I consult you a question? Should I open the window and import my images? How to import image to the window please?



First, congratulations on the excellent program. I would like to insert a zoom button, but when I did this with the Toolbar editor, it did not work, it would be interesting also a zoom sync of the 3 images.

Kent Yeap

Thank you, this code really helped! Keep up the good work!

Suchita Kanekar

how to change the path of image

aimi shaharuddin

Adnan Ibrahim

Dear Image Analyst,
This tool is very impressive. May I know which paper u followed for the implementation of this code. Thank you

Ali Sultan

Does this work with Matlab R2013a?

savita sharma

Adrin Johnson

Excellent work. Is it possible not to show the GUI and just returning the max and min values?

Adrin Johnson

Burce Atac Mogol

You should think of preparing a MOOC on image analysis. I will definitely buy it. Thanks..

Pradeep Bhaskar

I see your comments in questions posed by others and always they are very helpful. Voting up your answers.




It works really fine, very helpful. Thank you very much.

Ashfaq Ahmed

I have a question,

How can we can we perform accurate edge detection for grayscale images using adaptive detection schemes? I am not talking to use conventional edge thresholding schemes.

1. How it can perform thresholding to removes noise as well.
2. How it can adaptively threshold for 0-255 at each pixel without converting into binary image?
I want to check the performance on edges in images with lot of details.
Kindly elaborate with example.

helwan ghgs



1. Good layout.
2. GUI does exactly what it claims to do.
3. Almost unbelievable - here is an example of well-commented code. I find the lack of good commenting in code appalling these days. To all programmers: comment your code - it will help you or another user use it down the track, and will often help you structure your code logically when composing it in the first place!

Usama Farooq


Amit Nambiar

Thank you for this wonderful tutorial. It really helped me a lot to understand the concept of thresholding in detail and also the matlab codes are wonderful.

Thanks a lot Sir!


Sorry, Sean de, do you mind mentioning how to fix this issue. Which Set(..) in threshold.m should I turn off ?

Sean de

I have 2009b and the 'monitorpositions' option is available.

I found that in the demo_script leaving the set(..) the way you have it works fine and then turning off the set in threshold.m makes it a perfect size. It was this way before too, I could see the figure load set up and look good, then expand to be multiple times my screen width on that call to set().

Anyway, excellent demo! Definitely useful for beginners to understand thresholding.

Image Analyst

Sean, I got to admit I didn't test it on a system with two monitors. The MonitorsPosition seems to be new with R2010b (at least they call it "new" in the 2010b help) so if I put that in it won't work for anyone with older versions. I had considered the Mathworks recommended "maxfig" but I wanted this to be standalone and not require any other code to be downloaded. If you have a fix that will work with all versions, I'd like to see it, and may build it in.

Sean de

The call to:
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.

Blows the figure up on to the point of being way too big to do anything on dual monitors since the screen size is returned as the size of both. The root property 'MonitorPositions' has the individual sizes of the primary and secondary monitors.

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

Inspired: Parametric Mapping Scripts for MRI data

Community Treasure Hunt

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

Start Hunting!