Identify Objects and Compute Area in an Image using MATLAB

This article shows how to do preprocessing on an image to identify objects from an image. We convert an image in grey with image inversion and convert the image into a binary image to make it easy to identify foreground objects (individually all objects). You can then analyze the objects, such as finding the area of each object, centroid of each object or number of objects.

Analysis and Steps

The image which I am going to use is given below.

I took this image from google search

Preprocessing of the Image

If there are noise and unwanted objects in the image we require to remove them.

There are different ways to remove noises from images like designing an image filter or you can use some kind of morphology for the same. In MATLAB there are some functions which you can try like imfilter, imopen, imclose etc. Same way you can remove unwanted objects as well. Just remember that your objects can be in the background or foreground of the image. Again there are certain methods to detect the foreground and background from images, like segmentation based on the image colour thresholding or morphology.

In this example we are going to use bwareaopen function to remove small objects from the image, which are not required.

Before applying function bwareaopen, there are unwanted white spots
After applying bwareaopen, noise and unwanted objects are removed

Image Inversion

A good way to remove the background from the image is image inversion in the case of a white background. Image inversion just subtracts all the pixel intensity from highest pixel value that is 255 in unsign integer8 format. We are going to use an inbuilt command of MATLAB – imcomplement – for image inversion.

White background before inversion
After inversion removed noise

If your background is not white, then you can go for morphology as mentioned above, or you can use Gaussian mixture algorithms as well to detect the foreground if you have a video.

Image Conversion

Before detection of objects, better to convert the image in binary form that is black & white. Otherwise it is not possible to detect objects precisely from colour or grey image.

Functions which we use are rgb2gray (from colour to grey) and imbinarize (from grey to binary).

Identify and Count Objects

In this case we will use an inbuilt function of MATLAB – bwconncomp. It returns the connected components found in the binary image. bwconncomp uses a default connectivity of 8 for two dimensions, 26 for three dimensions, and  for higher dimensions conndef(ndims(Binary Image),’maximal’). 8 means if any object is connected with 8 pixels or more then it will be counted as one object, as well as it will give index of every object. For more detail you can refer to the MATLAB documentation.

Labelling all objects

In this case we label every object with respect to the colormap. First we use labelmatrix to create a label matrix from the output of bwconncomp. Then we use label2rgb to choose the colormap. Labelling objects is very important to identify similar kinds of objects or similar areas of objects. Similar kinds of objects will have same colour.

Labelled objects based on the colormap

Compute Area of Every Object

In this case I use an inbuilt function – regionprops. It returns measurements of connected objects in the binary image. You can use regionprops on contiguous (where objects share a common border) regions and discontiguous (separate borders) regions. To get measurements of a 3D volumetric image, use regionprops3 instead of regionprops.

Code Explanation

Read an image into the workspace

clear,clc
im = imread('Objects2.jpg'); % read image
imshow(im) % display image

Convet Image in Gray Format

I=rgb2gray(im); % convert image in grey 
imshow(I) % display image
As you can see there are some light shadows in above image and white background

Image Inversion

I=imcomplement(I); % complement of image
imshow(I)
Removed background

Image Conversion and Background Noise Removal

Create a binary version of the processed image. Use the imbinarize function to convert the grayscale image into a binary image. Remove the background noise from the image with the bwareaopen function. During the conversion value is .04 with function imbinarize . It means all pixels with the intensity below .04 will be converted to black. It takes normalized intensity values between (0,1).

bw = imbinarize(I,.04);
imshow(bw)
Binary image with noise

Remove all the connected objects that have fewer than 100 pixels from the binary image.

bww = bwareaopen(bw,100); 
imshow(bww)
After removed noise

Identify Objects in the Image

I have created a binary of the original image. Now I can perform the analysis of objects in the image.
Find all the connected objects in the binary image. The accuracy of your results depends on the the connectivity parameter (4, 8,16 28), and whether or not any objects are touching.

cc = bwconncomp(bww,26) % cc is a structure, where counts and index of every object are saved.
Structured output
cc.NumObjects % extract counts from the cc

View the object that is labeled 40 in the image.

objects = false(size(bw));
objects(cc.PixelIdxList{40}) = true;
imshow(objects)
Object at index 40

Labeling of Objects

Visualize all the connected components in the image by creating a label matrix.
Use labelmatrix to create a label matrix from the output of bwconncomp. labelmatrix stores the label matrix in the smallest numeric class.

labeled = labelmatrix(cc);
whos labeled
Outout of whos command

I used label2rgb to choose the colormap,

RGBLabel = label2rgb(labeled,'spring','c','shuffle');
imshow(RGBLabel)
Labeled image

Compute Area

Compute the area of each object in the image using regionprops. It will be saved in objectsdata

objectsdata = regionprops(cc,'basic')
Preview of area, centroid and bounding box

Create a new vector objects_areas, which holds the area measurement for each object.

objects_areas = [objectsdata.Area]; % Column vector of all area

Find the biggest area.

[max_area, idx] = max(objects_areas) % idx is the index of biggest area and max_area is the area at index idx

Display an Object with the Biggest Area

object = false(size(bw));
object(cc.PixelIdxList{idx}) = true;
imshow(object)
Object of biggest area

Display the biggest object in the original image by using insertshape command.

originalimag=insertShape(im,'Rectangle',objectsdata(idx).BoundingBox,'Color','black','LineWidth',2);
imshow(originalimag)
Detected biggest object

Further you can use histogram command to get the number objects on y-axis and area on x-axis.

histogram(objects_areas)
title('Histogram of Object Area')
value is number of objects

For the basics of image processing you can check my previous post Basics of Image Processing to Live Edge Detection

Further you can watch a recorded webinar and subscribe to the channel for more similar webinars. Click here Learn to count objects, measure area, centroid and bounding box of every object in an image

Leave a Reply

Your email address will not be published.