4.55

4.5 | 22 ratings Rate this file 655 Downloads (last 30 days) File Size: 5.68 KB File ID: #25157
image thumbnail

Image Segmentation Tutorial ("BlobsDemo")

by Image Analyst

 

27 Aug 2009 (Updated 16 Mar 2012)

Image Processing Tutorial to demonstrate the basic concepts to beginner users.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

Perfect for the beginner, this demo illustrates simple object detection (segmentation, feature extraction), measurement, and filtering. Requires the Image Processing Toolbox (IPT) because it demonstrates some functions supplied by that toolbox, plus it uses the "coins" demo image supplied with that toolbox. If you have the IPT (you can check by typing ver on the command line), you should be able to run this demo code simply by copying and pasting this code into a new editor window, and then clicking the green "run" triangle on the toolbar.

First finds all the objects, then filters results to pick out objects of certain sizes. The basic concepts of thresholding, labeling, and regionprops are demonstrated with a simple example.

It's a good tutorial for those users new to MATLAB's image processing capabilities to learn on, before they go on to more sophisticated algorithms.

Tested with R2008b through R2011b.

Acknowledgements
This submission has inspired the following:
Simple color detection by hue
Required Products Image Processing Toolbox
MATLAB release MATLAB 7.7 (R2008b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (59)
29 Aug 2009 us

an excellent and exceptionally well commented image processing demo by a seasoned ML user and senior CSSM citizen...

a few changes i'd make:
- put it into a function
  -> no need for the bad CLEAR ALL...
- make the figure size smaller -and-
- put an ECHO ON/OFF at the beginning/end of the demo
  -> for users to be able to enjoy the comments during run-time...

just a few thoughts
us

29 Nov 2009 ThomasKUL Knaepen

I got :

MATLAB Version 7.1.0.246 (R14) Service Pack 3
and the IPT

29 Nov 2009 ThomasKUL Knaepen

Not working for me:

Running BlobsDemo.m...
??? Undefined command/function 'sprint'.

Error in ==> regionprops>ParseInputs at 878
        msg = sprint('This measurement is not a string: "%d".', list{k});

Error in ==> regionprops at 114
[L, requestedStats] = ParseInputs(officialStats, varargin{:});

05 Dec 2009 Image Analyst

Thomas, this is not an error in my code. sprint() is not a function - ParseInputs() should have called sprintf() instead. regionprops() and ParseInputs() are Mathworks code, not mine. I searched the R2008b version of ParseInputs and it no longer has sprint() in it, so they've fixed that bug.

Sorry that I can't test this with every prior version of MATLAB. I know it definitely works with R2008a and later. Since you are several years behind I suggest you get the most modern version of MATLAB.

17 Jan 2010 Jimmy

Great Work Image Analyst, very useful indeed. I want to ask one question: is there any chance that it can be applied to more than one image simultaniously? And if there is how can it be done. Thank you

22 Jan 2010 Image Analyst

Jimmy, sure - just copy the code into your own program. One way to do it might be to use this in conjunction with MAGIC (http://www.mathworks.com/matlabcentral/fileexchange/24224) which does batch processing. Just take this code and plug it into his AnalyzeSingleImage() function. Then you'll be able to select several images from your folder, and process them. Of course, my code has several things that are special for the coins image and you'd have to modify several things for your images, such as the thresholds, etc.

23 Jan 2010 Jimmy

Thank you for your reply Image Analyst. Yes, I know that I need to make changes according to the pictures regarding threshold etc.. What I don't understand is how to plug that code into AnalyzeSingleImage(). Is it possible for you to send me the example on testbest1@yahoo.com. Thank you

24 Jan 2010 Image Analyst

Jimmy, just follow these easy step by step instructions.
1. Open both his MAGIC code and my BlobsDemo code in MATLAB.
2. Copy all of my BlobsDemo code into the clipboard.
3. Go to AnalyzeSingleImage in his code, currently line 1075, and simply delete all of that function, which means delete lines 1076 - 1164. This is just like it says to do in the comments on line 1070 and 1071 of his code.
4. Now simply paste in my BlobsDemo code.
Now it should work. Adapt my/your code in that function as you see fit.

25 Jan 2010 Jimmy

Ok I did as you said Image Analyst.I got some errors. Now do I only use 1076 - 1164 code or the whole Magic code which is huge? And another thing how do I select images from a folder? Do I need to put that into the code? Thank you

27 Jan 2010 Image Analyst

Jimmy, just paste whatever code you want into line 1076 of that app after you've deleted lines 1076-1164. Then just run it with the green triangle. You need all the rest of the code because that is the GUI. To select images, you simply click on their names in the listbox like you'd do with any app. No code is needed from you to handle clicking on a listbox - that's already done for you. Just click on the images you want to process and click the analyze button and it will analyze only those that you've clicked on.

28 Jan 2010 Husam Aldahiyat

Excellent!!!

30 Jan 2010 Jimmy

Ok let's forget about GUI, I am not even sure if I need to use it. The reason I asked if I can get more than one image read is than after I modified the code I got my the centroids of all objects in the images. The results are in vector form [x y]. For example
[29 134]. I want to make a loop going through all images and objects in them, reading those measurements. Can you give me an advice? Thank you

05 Feb 2010 Image Analyst

Jimmy - ask in the newsgroup. Let's take this conversation there.

07 Feb 2010 Nitin Kumar  
12 Feb 2010 Yuvaraj

great work..I have one question for U. using my web cam i will capture a picture which will contain a ball in a room. now how can i separate the ball (circular shaped) from the background and i want to find the area of that ball(circle). how can i edit your code for this particular task.

09 Mar 2010 Chaitu

Very useful...great work...

12 Mar 2010 David JL

I just realized I use Matalb R2007b, not R2008. I guess that is the problem. Sorry about that.

25 Mar 2010 John Brathwaite

What if I don't have the IPT??

25 Mar 2010 Image Analyst

Like other submissions that are meant to demonstrate certain toolboxes, you need to have the toolbox. If you don't have it, you can buy it, or write the necessary toolbox functions yourself. Generally they are the functions starting with the letters "im" or "bw".

14 Apr 2010 aravind prajan  
17 May 2010 Farie

Hi, I've tried your demo but it's not working for 3D images. Is there any way to solve this problem? Thanks

06 Jun 2010 Ben

hello every body, can you give me a code for building extraction using mathematical morphologie ?? and thanks a lot

01 Jul 2010 Nitin Kumar  
01 Jul 2010 Christopher

Interesting demonstration ImageAnalyst. Fortunately the 'coins' sample image has high contrasting content which allows for easy selection of the threshold. Plus the coins allow for the 'imfill' operation to perform the majority of the work.

It would be interesting to see results on an image with a much smoother histogram but this is where image segmentation gets complicated I guess...

All in all, a nice demonstration of the capabilities that both the IPT and Mathematical Morphology can provide in image processing applications.

01 Jul 2010 trypsin

i am just trying it

15 Aug 2010 Walaa Gouda

Hi I am working in my master about breast cancer mass detection the first step is to determine region of interest which is the breast contour and i need help in this step as i don't know how to do this so please if there is anyone who can help me
thanks

22 Sep 2010 Image Analyst

Christopher. Yes there are lots of more complicated images that could be used that would be more challenging. I work with many of those kinds of images. However I know from my newsgroup postings that even code as simple as this blows a lot of people away. So thanks for the suggestion but I think I'll keep it simple. I do have some color demos posted that are slightly more challenging.

24 Sep 2010 J.D

hello ~ i juz want to ask you what should i do if i want to recognize color of human skin~ i want to get its hsv value for every pixel of the human face ~ and then exlude the eye and mouth part~ i want to get the value as my input to train in neural network~ can u help me~ urgent.. tq~

15 Oct 2010 Image Analyst

J.D, you need to get some model of skin color. I know that it's a boomerang- or banana-shaped cluster in HSV color space. You need to define parameters to model that, and then segment it, possibly with a 3D lookup table instead of a mathematical model, or with the mathematical model if you can come up with the formulas. But I know you can't simply do high and low thresholding in any color space to carve out a box that selects the skin color pixels.

02 Nov 2010 Vivek Maik  
12 Nov 2010 Anthony

An excellent demonstration of simple image segmentation. Also a nice introduction to the image toolbox in Matlab. The functions for dealing with connected components (centroid, mean, etc) should be extremely useful.

03 Dec 2010 Naresh Pai

Useful and well-commented code that demonstrates image thresholding. Connected components, including real world objects, in an image is of interest to many and this code provides the starting point for such projects. Thanks IA.

12 Jan 2011 Manhal

and one more thing, I tried to change the image that is used in the BlobsDemo, by adding 2 more coins, but still show that we already have 10 objects...
is there something should I change to make it works??

05 Feb 2011 Image Analyst

Vivek, what is it about this that made you think it was a 1 star? Is there anything I could improve with it to help beginners understand it better?

05 Feb 2011 Image Analyst

Manhal, I used Photoshop to create an image with 12 coins and it worked just fine. You must have done something wrong.

21 Feb 2011 Nicolas  
22 Feb 2011 Nicolas

Very cool code.

01 Mar 2011 shantha

Hello!

I'm pretty new to matlab.Can you offer some help regarding my issue?
I need a code to extract objects from a single image and write the extracted objects to separate images.As an example if an image consists of say an elephant and tree , I want to extract these two objects and write as separate images..

01 Mar 2011 Pierre

Please can somebody send me the matlab code for gray scale image segmentation plzzzz...?I will be very greatful

10 Mar 2011 Jason  
24 Apr 2011 akbarul

thank you

10 May 2011 K

I should read code comments more thoroughly. No need to answer. Thanks!

10 May 2011 K

Thanks!

22 May 2011 German Espinal

Hi guys !!! I need to capture a primary color card with my webcam, then I need to determine what color is it ??? Can someone of you Help me please with the MatLab code ???? TKS A lot !!!

24 May 2011 German Espinal

Please I need some help !!! I need to capture a basic color card with my webcam, then I need to determine what color is it. Matlab code analize and gives the answer. Example: The card is yellow. Can someone of you Help me please with the MatLab code ???? TKS A lot !!!

05 Jun 2011 Image Analyst

German:
You ask this kind of question on the newsgroup or on Answers, not as a comment on people's File Exchange submissions. I'd be happy to answer it there.

22 Jul 2011 Lim

Hi I appreciate this demo very much. I modify your code to work along with watershed segmentation for my image (blood cell), and it turns out good. However, how do I filter the background after I plot the boundaries of the selected cell? (so that the resulting image only shows the blobs without the background) The cropping works very well too but I don't want to involve the background. Thank you.

13 Aug 2011 sivan saminathen

can any wan help me...why i got error in the code when i try to run it..in line 41-[pixelCount grayLevels] = imhist(originalImage);...this code..the error said

("??? Error using ==> iptcheckinput
Function IMHIST expected its first input, I or X, to be two-dimensional.")

why it said so...i just keep on follow the code given but suddenly it give error

24 Aug 2011 Nicolas  
05 Nov 2011 Image Analyst

If anyone wants to ask me a question, click my name above, then click the "Contact Author" link.

06 Dec 2011 Fer  
19 Jan 2012 Syahrul Niezam

is it possible to do segmentation into several areas, not binarization before labeling?

20 Jan 2012 Image Analyst

sivan, you must have a color image for some reason. You probably changed the filename to one of your images. Coins.png that my demo uses is a monochrome (gray scale) image.

20 Jan 2012 Image Analyst

Syahrul, it does do segmentation into several areas - each coin is a different area. How can you label something without having a binary image? You can't, but why would you want to avoid labeling even if you could?

19 Feb 2012 Rose  
22 Feb 2012 Ryan  
31 Mar 2012 zaidan

it's nice info for beginner thank's

30 May 2012 Aliyu Abdu

I'm finding your Demo quite very useful in my work. Thank you.

02 Jun 2012 khalid  
Please login to add a comment or rating.
Updates
25 Mar 2010

Added label numbers to each blob. Extracted each blob to a separate image.

26 Mar 2010

Updated description and made change to how labeled image is displayed.

10 Apr 2010

Spelling correction in the description.

16 Mar 2012

Renamed main title and altered description slightly.

Tag Activity for this File
Tag Applied By Date/Time
image processing Image Analyst 27 Aug 2009 14:34:47
demo Image Analyst 27 Aug 2009 14:34:47
potw Shari Freedman 05 Nov 2009 10:33:00
demo Abdulhakeem Othman 15 Jan 2010 09:57:38
segmentation Image Analyst 26 Mar 2010 13:02:45
image analysis Image Analyst 26 Mar 2010 13:02:45
image processing Besita 22 Sep 2010 14:08:09
demo Besita 22 Sep 2010 14:08:11
image analysis Besita 22 Sep 2010 14:08:13
image analysis Zubia 19 Nov 2010 14:31:46
image processing Zubia 19 Nov 2010 14:31:52
demo Zubia 20 Nov 2010 04:04:18
pick of the week Jiro Doke 11 Feb 2011 20:06:58
demo Ahmad 06 May 2011 18:30:09
image analysis Ahmad 06 May 2011 18:30:15
image processing Ahmad 06 May 2011 18:30:19
image analysis Mohamed 03 Aug 2011 08:14:34
demo subhan pradana 16 Oct 2011 05:14:06
demo Sumit Kar 12 Dec 2011 19:35:41
segmentation Enea Poletti 28 Mar 2012 05:49:15

Contact us at files@mathworks.com