This is fun project based on image processing with MATLAB, here we will come accross learing how to count the number of distinguishable objects in an image. Sounds fun ,dosen’t it …So lets get started!
Note: The following tutorial is performed with the help of MATLAB. MATLAB stands for “MATrix LABoratory” and is a numerical computing environment and fourth-generation programming language, developed by MathWorks. Using MATLAB, we can solve technical computing problems faster than with traditional programming languages, such as C, C++, and Fortran. The tutorial can be performed by executing the commands in each step sequentially in the command window of MATLAB.
Step 1: Read the Image

An image is choosen so that it has considerable contrast with its background so that the objects can be identified. The function imread reads the image from a given location and stores it in the matrix img1. The imshow function displays the image.
img1=imread('Lines.jpg');
imshow(img1)
Step 2: Convert the Image to Grayscale

This step removes any color information in the image to make it easier to identify objects. The rgb2gray converts the color image into a grayscale image and stores it into the matrix img2.
img1=rgb2gray(img1); imshow(img1)
Step 3: Threshold the image

This step thresholds the image by converting the grayscale image into an image that contains only two colors. The function im2bw() assigns black color to all the pixels that have luminosity than a threshold level and the others as white. the function graythresh() approximately calculates the threshold of the image.
img2=im2bw(img1,graythresh(img1)); imshow(img2)
Step 4: Complement the image

In this step we complement the image by using the ~ operator. By this we convert the white patches into black and vice versa. We perform this step as we wanted the areas of concern(objects) to be colored white.
img2=~img2; imshow(img2)
Step 5: Find the Boundaries of the Objects

This step finds the boundaries of each object that it finds and stores it in B. The text function prints the number of objects that are found by bwboundaries.
B = bwboundaries(img2);
imshow(img2)
text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
hold on
Step 6: Draw the Boundaries

This is a fun step in which we mark the boundaries of all the objects identified by bwboundaries function. This step can be eliminated if it seems complicated
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 0.2)
end
Thats it!!
Thank you for following this tutorial, I hope you have followed all the parts of this tutorial. If you did’nt you can feel free to post your doubts in the comments section. You can also download the source files and run the simulation in Simulink, then you can experiment with the different blocks until you get a hold of it. All the Best!
Download the Source code
Similar topics you might like…
MatLab Analysis: Twenty20 Worldcup
Serial Communcation between microcontroller and PC
Interfacing a CD-Rom stepper motor
DTMF Decoder
it was very interesting to read.
I want to quote your post in my blog. It can?
And you et an account on Twitter?
So much info in so few words. Tosloty could learn a lot.
i am doing my final year project on traffic congestion mechanism throung real time video processing using matlab, i required a matlab code which count the # of tag car in road(video) please help me and send this code on my email add.
This code is really helpful, but since I have to image process my confocal or fluorescence microscope images of cancer cells at region of interest, sometimes this code gives erroneous results
thanks alot i need this code but i need also to compute the number of object plz if you can help me its important for my master
Yo, that’s what’s up trthufluly.
thanks alot but ineed also thenumber of object pleez its important
Amiable post and this enter helped me alot in my college assignement. Thank you as your information.
Possibly the top page I have read this year
Jesus
I have to hear what Lorie can do about this
Dollie
Maybe the greatest page I have read ever =D
This is the most amazing read I have read today!?!
It’s time to bring these babies back from obscurity!
last week our class held a similar talk on this topic and you show something we have not covered yet, appreciate that.
- Laura
excellent points and the details are more precise than elsewhere, thanks.
- Thomas
you are really a wonderfu webmaster, you have done a good job on this topic!
- Ayla COCKRELL
Great Article…
hello,i have to complete my final year project on traffic control system using image processing.is there any friends to help me
I can not figure out how do I subscribe to your blog
You can subscribe from the first block in my sidebar
(or)
http://www.ingeniumblog.net/?feed=rss2
thanks for ur idea sir.
can u give me some more idea of identifying a particular object from an object
I have done counting in one frame but for the whole video i have problem that one car can be counted multiple times …how to solve this problem. any comments?
You’re truly a excellent webmaster. The website loading pace is amazing. It sort of feels that you’re doing any unique trick. Moreover, The contents are masterwork. you have performed a wonderful activity in this matter!
I’m a long time watcher and I just believed I’d drop by and say hello there for your very first time.
I am totally delighted with strong your blog greatly that helped me! Thank you “Money won’t create success. The freedom to make it will.” – Nelson Mandela
I am totally delighted with incredibly blog greatly that saved me. God bless you “Failure is simply the opportunity to begin again, this time more intelligently.” – Henry Ford