Object Counting and Density Calculation Using Matlab
Object Counting and Density Calculation Using Matlab
USING MATLAB
Submitted by
ABSTRACT
Counting the number of objects is an integral part of image processing.
Knowing the number of objects present in the image can be useful for further
analysis in a wide range of applications. In this project we propose a simple
method for automatically determining the number of objects in an image . Once
the number of objects are determined the objects per unit area or the density can
also be estimated. Existing methods involve counting based on area of objects,
color of objects, applying edge detection techniques etc. There are also
applications that involve a large amount of hardware components for counting
that further adds to the cost and maintenance which is a tedious work. The
proposed system involves converting the input image into a format such that
the number of objects can be calculated based on the connected components
present in the enhanced image. This project work also aims at determining the
correct value of density by clearing the objects touching the borders of the
image. In this project three applications are taken into account and using Matlab
with image processing toolbox the count and density values are calculated for
each.
CHAPTER 1
1.INTRODUCTION
Images contain a wealth of objects and patterns which may convey
information about underlying mechanism and methods. Counting involves
estimating the number of objects in an image or a still video frame. Counting
arises in many real-time applications such as counting cells in microscopic
images, number of people, number of vehicles on road etc.
This goal is to accurately estimate the count, but however we evade the
hard task of localizing or drawing boundary around the objects. Object detection
is a very hard problem to be solved, especially in case of overlapping objects.
Once the count is known , the density of objects or the number of objects
per unit area can be estimated.
As mentioned earlier, the existing works under counting involve a large
amount of hardware which also adds to the cost. Now with a simple image
processing software such as matlab many parameters can be estimated from
images. In this paper we have dealt with 3 such applications namely Cell counting
People counting based on faces
Vehicle counting
1.1 Cell counting:
COUNTING cells is often a necessary but tedious step for in vitro cell
culture.
and accuracy. Cell counts are important for monitoring cell health and
proliferation rate, assessing immortalization or transformation, seeding cells for
subsequent experiments, transfect ion or infection, and
Preparing for cell-based assays.
3
We propose a system for counting cells in image by using matlab and image
processing toolbox. It can be used for accurate calculation of object and density
of image
1.2 Counting people based on the faces detected:
NO OF OBJECTS
TOTAL SIZE OF IMAGE
CHAPTER 2
2. LITERATURE SURVEY:
2.1 CELL COUNT:
Victor Lempitsky and Andrew Zisserman-Learning To Count
Objects in Images deals with the counting problem is the estimation of
the number of objects in a still image or video frame. It arises in many
real-world applications including cell counting in microscopic images,
monitoring crowds in surveillance systems, and performing wildlife
census or counting the number of trees in an aerial image of a forest. We
take a supervised learning approach to this problem, and so require a set
of training images with annotation.
Gary Chern, Paul Gurney, and Jared Starman discussed about Face
detection using Color based masking. They developed an algorithm
capable of locating each face in a color image of the class. They were
given seven training images along with the corresponding ground truth
data to develop and train algorithms on .
CHAPTER 3
3. BLOCK DIAGRAM:
CONVERT COLOR TO
BINARY
INPUT IMAGE
COUNTING REMAINING
OBJECTS & CALCULATING
DENSITY
REMOVING BORDER
TOUCHING OBJECTS
CONVERSION FROM
RGB TO LAB
INPUT IMAGE
DISPLAY NO. OF
PEOPLE AND DENSITY
CONVERT TO BINARY
EXPLANATION:
First the input image is fed into matlab. The image is
converted from rgb to lab. Then the gray threshold is applied to the
converted image and brightness equalization is done. Then the equalized
image is converted to binary. The blobs in the image are opened when the
blobs area is greater than 1500. The number of people and density are
displayed.
CONVERTING FROM
RGB TO GRAY
DISPLAY COUNT
AND DENSITY
FIND BLOBS OF
AREA >4000
CONVERT TO
BINARY
12
CHAPTER 4
4. ALGORITHM:
4.1 CELL COUNT:
Step 1)Start the program
Step 2)Get the input image using imread ()
Step 3)Convert color to gray if it is a color image.
Step 4)Use clear borders command if any object is touching the
border.
Step 5)Choose an appropriate threshold value using graythresh ()
command
Step 6)Fill blobs that occur with holes
Step 7)Apply morphological operation by using a filter of ones
Step 8)Display all blobs that have an area>100
Step 9)Count them using conncomp () command
Step 10) Calculate the density
Step 11) Stop the program
13
14
using double
15
CHAPTER 5
5. FLOWCHART:
5.1CELL COUNT:
START
STOP
Fig 5.1 Flow chart of the cell count
16
If patches
are present
NO
Setting threshold value
Determining number of
objects present
Output display
End
17
YES
CONVERT TO GRAYSCALE
FORMAT
DETERMINATION OF WIDTH AND
HEIGHT OF IMAGE
SET THRESHOLD VALUE=11
FINDING DIFFERENCE BETWEEN
FRAMES BASED ON THRESHOLD
ASSIGN 0 IF NO
DIFFERENCE IS
FOUND
NO
IF FRAME
DIFF>11
YES
ASSIGN A
VARIABLE TO
THAT IMAGE
INCREASING CONTRAST OF
OUTPUT IMAGE USING imadjust()
FINDING GRAYTHRESHOLD
VALUE USING graythresh()
CONVERTING TO BINARY
IMAGE
FILLING HOLES
DISPLAYING THE
OUTPUT IMAGE
END
Fig 5.3 Flow chart of the vehicle count
19
CHAPTER 6
6. MODULE DESCRIPTION:
6.1 CELL COUNT:
6.1.1 Image acquisition:
Imread() -Getting the input imagereads a grayscale or color
image from the file specified by the string filename. If the file is
not in the current folder, or in a folder on the MATLAB path,
specify the full pathname.
imshow(I,[low high])-
specifying the display range for I in [low high]. The value low (and
any value less than low) displays as black; the value high (and any
value greater than high) displays as white. Values in between are
displayed as intermediate shades of gray, using the default number
of gray levels.
6.1.2Color to gray conversion:
21
22
23
24
input image one is the image with vehicles and the other is a background
image without vehicles in it.
6.3.2 Converting color image to gray:
Rgb2gray() converts both the images to gray images. Convert both
the images into gray level by using double precision.
6.3.3 Foreground detection:
Set threshold value=11, find the difference between the two images
by using abs() [absolute], abs can help find the absolute between the two
images. If the difference between the two images is greater than the
threshold value of 11 then those will be displayed as blobs at the output.
6.3.4 Morphological operation:
imageadjust(imadjust()) used to adjust the image intensity values to
the color map.
graythresh() used to set a suitable gray threshold value for the
output image.
Add Gaussian noise to the output image and filter it using wiener
filter.
Convert image to binary and fill holes if necessary. Open blobs of
area greater than 5000, this will help detect vehicles.
6.3.5 Counting and density calculation:
Count the no of blobs by using bwconncomp, the number of blobs
gives the number of vehicles present in the image. From the count the
density of traffic can be estimated
25
CHAPTER 7
7. SIMULATION RESULTS:
RESULT
7.1 RICE COUNT:
7.1.1 Input Image:
Image
`
Fig 7.1.2 Binary image of the count
26
OUTPUT:
existing
Cc =
Connectivity: 8
ImageSize: [256 256]
NumObjects: 95
PixelIdxList: {1x95 cell}
Objects_per_unit_area =
0.0014
27
0.0013
28
30
31
32
33
50
100
150
200
50
100
150
OUTPUT:
cc =
Connectivity: 8
ImageSize: [225 300]
NumObjects: 7
PixelIdxList: {1x7 cell}
density = 1.0370e-004
numberOfPeople =7
34
200
250
300
100
200
300
400
200
400
600
7.4.4 Foreground:
200
400
600
OUTPUT:
cc =
Connectivity: 8
ImageSize: [480 640]
NumObjects: 2
PixelIdxList: {[39316x1 double] [9899x1 double]}
6.5104e-006
35
CHAPTER 8
8. CONCLUSION:
In this project work, a simple way of counting objects in the images and
determining the correct value of density has been implemented. The number of
objects and the density values are determined and verified for cell, people and
vehicle counting applications. In cell count, the problem of counting many cells
automatically as well as determining the accurate value of density has been
solved. In people counting based on faces, even if too much skin is shown by a
person in the image the algorithm can detect faces correctly. This provides an
accurate count and density. Finally in vehicle counting, the algorithm used can
detect the number of vehicles accurately including two wheelers without use of
any hardware components.
In future, the same algorithm can be extended too many other
applications other than the three applications covered in this project. Also, the
present mat lab code used in cell count can be extended to find the correct count
even if cells tend to overlap in the image. In people counting, if the two faces
touch each other then inaccurate counts can be obtained. Hence methods to
segment such images can be developed. In vehicle count, the same method can
be extended to classify vehicles on the road such as car, truck, bike etc. Finally,
the same counting applications can be shown in a simpler manner using other
image processing software such as Python, ImageJ etc.
36
REFERENCES
Cell count:
[1] http://www.robots.ox.ac.uk/%7Evgg/research/counting/index.html.
[2] S.-Y. Cho, T. W. S. Chow, and C.-T.Leung.A neural-based crowd
estimation by hybrid global learningalgorithm. IEEE Transactions on Systems,
Man, and Cybernetics, Part B, 29(4):535541, 1999.
[3] C. Desai, D. Ramanan, and C. Fowlkes. Discriminative models for multiclass object layout. ICCV, 2009.
[4] X. Descombes, R. Minlos, and E. Zhizhina. Object extraction using a
stochastic birth-and-death dynamics incontinuum. Jthisnal of Mathematical
Imaging and Vision, 33(3):347359, 2009.
[5] L. Dong, V. Parameswaran, V. Ramesh, and I. Zoghlami. Fast crowd
segmentation using shape indexing.ICCV, pp. 18, 2007.
[6] M. Everingham, L. Van Gool, C. K. I. Williams, J. Winn, and A. Zisserman.
[7] T. Joachims, T. Finley, and C.-N. J. Yu. Cutting-plane training of structural
svms. Machine Learning, 77(1):2759, 2009.
[8] N. Ahuja and S. Todorovic.Extracting texels in 2.1d natural textures.ICCV,
pp. 18, 2007.
[9] S. An, P. Peursum, W. Liu, and S. Venkatesh. Efficient algorithms for
subwindow search in object detectionand localization. CVPR, pp. 264271,
2009.
[10] D. Anoraganingrum. Cell segmentation with median filter and
mathematical morphology operation.ImageAnalysis and Processing,
International Conference on, 0:1043, 1999.
[11] O. Barinova, V. Lempitsky, and P. Kohli. On the detection of multiple
object instances using Hough transforms.CVPR, 2010.
37
People count:
L. H. Liang, A. H. Zhou, G. Y. Xu, B. Zhang and L. H. Zhao, "A survey
of human face detection," Chinese Jthisnal of Computers, Vol. 25, No 5,
pp. 450-458, 2002.
1. P. Viola and M. Jones, "Rapid object detection using a boosted cacade of
simple features," Proceedings of IEEE Conference on Computer Vision
and Pattern Recognition, Vol. 1, pp. 511-518, 2001.
2. L. M. Luo, Z. Y. Peng and G. Y. Xu, "The feature of skin color," Jthisnal
of Software, Vol. 12, No 7, pp. 1032-1040, 2001.
3. R. L. Hsu, M. Abdel-Mottaleb and A. K. Jain, "Face detection in color
images," IEEE Transactions on Pattern Analysis and Machine
Inteligence, Vol. 24, pp. 696-706, 2002.
4. X. Tian, "Color space-based skin color subspace," Jthisnal of Xi'an
University of Science and Technology, Vol. 12, No 4, pp. 369-371,
2001.
5. X. S. Gao, X. Z. Zhang and C. Y. Ji, "Face detection in color images,"
Techniques of Automation and Applications, Vol. 24, No. 10, pp. 54-56,
2005.
6. Y. Freund, "Boosting a weak learning algorithm by majority,"
Information and Computation, Vol. 121, No 2, pp. 256-285, 1995.
38
Vehicle count:
39
APPENDIX I
MATLAB CODING
40
I = imread('dna.jpeg');%call image
bw0=imclearborder(I);
imshow(bw0),title('img 1');
bw1 = im2bw(bw0, graythresh(bw0));%apply threshold
figure;imshow(bw1),title('img 2');
bw2 = imfill(bw1,'holes');%fill hole in the image
figure;imshow(bw2),title('img 3');
bw3 = imopen(bw2, ones(5,5));%perform morphological ops in image
figure;imshow(bw3),title('img 4');
bw4 = bwareaopen(bw3, 100);%removes from a binary image all connected
comp of area less than 100 pixels
figure;imshow(bw4),title('img 5');
cc=bwconncomp(bw4)%count
objects_per_unit_area=cc.NumObjects/(size(bw4,1)*size(bw4,2))%Density
41
hold on;
title('Original with bounding boxes');
for k = 1 : numberOfPeople % Loop through all blobs.
% Find the mean of each blob.
% directly into regionprops.
thisBlobsBox = blobMeasurements(k).BoundingBox; % Get list of pixels in
current blob.
x1 = thisBlobsBox(1);%1st side
y1 = thisBlobsBox(2);%2nd side
x2 = x1 + thisBlobsBox(3);%3rd side
y2 = y1 + thisBlobsBox(4);%4th side
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
%subplot(3,4,2);
plot(x, y, 'LineWidth', 2);
end
43
clc;
clear all;
MV = imread('cars1.png'); %To read image
MV1 = imread('backgnd.png');
A = double(rgb2gray(MV));%convert to gray
B= double(rgb2gray(MV1));%convert 2nd image to gray
[height width] = size(A); %image size?
h1 = figure(1);
%Foreground Detection
thresh=11;
fr_diff = abs(A-B);
for j = 1:width
for k = 1:height
if (fr_diff(k,j)>thresh)
fg(k,j) = A(k,j);
else
fg(k,j) = 0;
end
end
end
subplot(2,2,1) , imagesc(MV), title (['Orignal Frame']);
subplot(2,2,2) , imshow(mat2gray(A)), title ('converted Frame');
subplot(2,2,3) , imshow(mat2gray(B)), title ('BACKGND Frame ');
sd=imadjust(fg);% adjust the image intensity values to the color map
level=graythresh(sd);
m=imnoise(sd,'gaussian',0,0.025);% apply Gaussian noise
44
45
46