Image Processing and Counting Using MATLAB

158,524

74

27

About: Oh, hi there! My name is Aditya Reddy. I am an Electronics Engineer, Software developer and a tech-blogger. You can see more of my works at my website www.ingeniumblog.net. I have a passion for developing s...

Intro: Image Processing and Counting Using MATLAB


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.

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

Step 7: The Code


img1=imread('Lines.jpg');
imshow(img1)

img1=rgb2gray(img1);
imshow(img1)

img2=im2bw(img1,graythresh(img1));
imshow(img2)

img2=~img2;
imshow(img2)

B = bwboundaries(img2);
imshow(img2)
text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
hold on

for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 0.2)
end

3 People Made This Project!

Recommendations

  • Metalworking Contest

    Metalworking Contest
  • Fix It! Contest

    Fix It! Contest
  • Audio Contest 2018

    Audio Contest 2018

27 Discussions

0
None
satishvalluri

Question 7 weeks ago on Introduction

Can we count images of same shape in an image consisting of several other shapes?

0
None
malamer89

1 year ago

Will this code work for my image? And also, if there were a number of objects that weren't included, can I include them on my own? What I'm trying to do is to count these objects, get a distribution of the size, and a distribution of their colors.

g2g 081017-0008.tif
0
None
AllanF27

1 year ago

Can you please tell me the software that you are using?? is it MATLAB r2017a or something else??

0
None
dhruvilmistry7

3 years ago

Hello!
How can i take one frame or image from live streaming video to process it?

0
None
mani.ranjh89

3 years ago on Introduction

hy guys

i need some help

i have an image of a scanned txt and i want to do kmean clustering....into 3 clusters of background, upground and dity areas...

test.jpg
0
None
eman_anum

4 years ago on Introduction

thank you.. a great help for me.. :) .. i only found this easy toturial to do that task after searching .. God bless u dear

0
None
dean88

5 years ago on Introduction

good job..
my question: how about vehicle...? how to counting vehicle?
here the picture: http://postimg.org/image/m1u3jyqlp/

0
None
dkz

5 years ago on Introduction

NIce work done. Like this can u tell how to detect objects in an image and find their locations. It will be very useful.

0
None
kmittal2

5 years ago on Introduction

It is possible that the objects appear overlapped in image. Is it still possible to count their numbers?

0
None
JungleMugsy

6 years ago on Introduction

My girlfriend had to do something like this for a class using Matlab. Too bad she didn't read this first!

0
None
irtiq7

8 years ago on Introduction

Aditya bhai this is slick......... I am loving it great work