Introduction: 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

Comments

author
Akira456 made it! (author)2017-09-18

How can i count the objects in these images..... Plzzz help

Count the various objects in image....

book1.jpglayyy.jpg
author
malamer89 (author)2017-08-23

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
author
AllanF27 (author)2017-04-07

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

author
min15 made it! (author)2017-01-11

how finding object in picture?

how much fruit in this picture?

ttnx

6.jpg
author
Su MonA (author)min152017-01-29

Dear min15,

I have developed one code for your image.Although it is not perfect, I hope you can get something from this.Keep in touch! Enjoy your strawberries !

Here is the code for you

clc;
clearvars;
close all;
imtool close all; % Close all imtool figures.
workspace;
format longg;
format compact;
fontSize = 20;
tic;
% Read image
original = imread('F04U2MPIXQG3WQS.LARGE.jpeg');
figure,imshow(original)
title('Original Image','FontSize',fontSize);

% RGB component
R=original(:,:,1);
G=original(:,:,2);
B=original(:,:,3);
figure,imshow(R)
title('Red Component Image','FontSize',fontSize);
figure,imshow(G)
title('Green Component Image','FontSize',fontSize);
figure,imshow(B)
title('Blue Component Image','FontSize',fontSize);

% Thresholding
% Select Red compoent
level=graythresh(R);
binary=im2bw(R,level);
figure,imshow(binary)
title('Thresholed Image','FontSize',fontSize);

% Use Morphological Operations
erosion=imerode(binary,strel('disk',2));
figure,imshow(erosion)
title('Eroded Image','FontSize',fontSize);

% Use Watershed segmentation
D = bwdist(erosion);
figure
imshow(D,[],'InitialMagnification','fit')
title('Distance transform of ~bw')
D = -D;
D(~erosion) = -Inf;
L = watershed(D);
Lrgb = label2rgb(L,'jet',[.5 .5 .5]);
figure
imshow(Lrgb,'InitialMagnification','fit')
title('Watershed transform of D')
figure,imshow(imfuse(erosion,Lrgb))
axis([])
bw2 = ~bwareaopen(~erosion, 10);
imshow(bw2)
D = -bwdist(~erosion);
figure,imshow(D,[])
Ld = watershed(~D);
figure,imshow(label2rgb(Ld))
bw2 = erosion;
bw2(Ld == 0) = 0;
mask = imextendedmin(D,2);
D2 = imimposemin(D,mask);
Ld2 = watershed(D2);
bw3 = erosion;
bw3(Ld2 == 0) = 0;
figure,imshow(erosion);
title('Watershed Segmentation','FontSize',fontSize)

% labelling
figure,imshow(original);
title('Labelling','FontSize',fontSize)
s=regionprops(logical(bw3),'Centroid','Area','Eccentricity','Perimeter');
hold on
for k=1:numel(s)
c=s(k).Centroid;
text(c(1),c(2),sprintf('%d',k),...
'HorizontalAlignment','center',...
'VerticalAlignment','middle');
end
hold off

Num = numel(s);
message= sprintf('You have counted %i Strawberries',Num);
h=msgbox(message);

author
Su MonA (author)min152017-01-29

Dear min15,

I developed one code for you.Although it is not perfect,I hope you can get something from this.Enjoy it! Please your knowledge and develope it more

author
mariyayeldhos made it! (author)2016-08-30

I tried it in Matlab. But, the number of objects its detecting is false.

could u explain a way where I can obtain correct number of detected objects

output.JPG
author
JoshuaN6 (author)2015-06-27

author
حمدىش (author)2015-05-02

thank you

author
dhruvilmistry7 (author)2015-04-04

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

author
khushi123 (author)2015-03-14

author
sampath ch (author)2015-02-26

author
mani.ranjh89 (author)2014-10-19

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
author
akshaydhone (author)2014-10-14

thnx buddy

author
akshaydhone (author)2014-10-14

thnx buddy

author
aeksaekhow (author)2014-09-13

Thanks you :)

author
eman_anum (author)2013-10-31

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

author
dean88 (author)2013-07-12

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

author
dkz (author)2012-11-23

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

author
kmittal2 (author)2012-11-04

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

author
JungleMugsy (author)2011-12-30

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

author
arzoo105 (author)2010-10-04

thx bro

author
naziabhi (author)2010-07-05

nice work

author
irtiq7 (author)2010-06-12

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

author
adityareddy (author)irtiq72010-06-14

thanx!!

author
LuisiJ (author)2010-06-11

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.

author
adityareddy (author)LuisiJ2010-06-12

maybe im planning 2 fill those up in my next instructable. Thanx anyway!!

author
tanmanknex (author)2010-06-11

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.

author
adityareddy (author)tanmanknex2010-06-12

Ya, i could'nt think of anything when i have that on my desktop.

author
bishopdante (author)2010-06-11

Matlab rules. Nice one.

author
Culturespy (author)2010-06-11

More people need to know this stuff!

About This Instructable

150,291views

75favorites

License:

Bio: 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 ... More »
More by adityareddy:Image Processing and Counting using MATLAB
Add instructable to: