Instructables
Picture of Image Processing and Counting using MATLAB
process1_02.jpg
process1_03.jpg
process1_04.jpg
process1_05.jpg
process1_06.jpg

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!

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.
 
Remove these adsRemove these ads by Signing Up

Step 1: Read the Image

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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
aeksaekhow8 days ago

Thanks you :)

eman_anum10 months ago
thank you.. a great help for me.. :) .. i only found this easy toturial to do that task after searching .. God bless u dear
dean881 year ago
good job..
my question: how about vehicle...? how to counting vehicle?
here the picture: http://postimg.org/image/m1u3jyqlp/
dkz1 year ago
NIce work done. Like this can u tell how to detect objects in an image and find their locations. It will be very useful.
kmittal21 year ago
It is possible that the objects appear overlapped in image. Is it still possible to count their numbers?
JungleMugsy2 years ago
My girlfriend had to do something like this for a class using Matlab. Too bad she didn't read this first!
arzoo1053 years ago
thx bro
naziabhi4 years ago
nice work
irtiq74 years ago
Aditya bhai this is slick......... I am loving it great work
adityareddy (author)  irtiq74 years ago
thanx!!
fuego3164 years ago
Great work, I think everyone should learn the basics like this! You could also use the watershed filter to pick our objects not so clearly defined. It could be useful to show something like that as an next step for people who want to try it with any blob structures that may touch.
adityareddy (author)  fuego3164 years ago
maybe im planning 2 fill those up in my next instructable. Thanx anyway!!
tanmanknex4 years ago
When I first looked at that first image, I thought it looked familiar but I couldn't place it. When I closed out of the browser twenty minutes later, i realized that my desktop has that as the background. That's basically my life for you.
adityareddy (author)  tanmanknex4 years ago
Ya, i could'nt think of anything when i have that on my desktop.
bishopdante4 years ago
Matlab rules. Nice one.
Culturespy4 years ago
More people need to know this stuff!