In this tutorial, we will see how to Calculate the Area of an object with Mask R-CNN. We will get the value in square centimeters and this is very useful in various sectors, for example when we have to check the defects of an object, if the surface is different it means that it has a problem. Not only the surface of an entire object can be identified but also parts of it, such as the logo in a car wheel (as in the image below).

For simplicity and for educational use we will do all this with common lego bricks. We will calculate the area in square centimeters and show the value on the screen. We will be using Google Colab for the entire process, so there will be no tedious installation required.

## Goals to be achieved to calculate the Area of an object

There are two specific requirements to complete the project:

1. You have to be able to Detect and segment the object, in this case a lego brick
2. Define a ratio to be able to correctly convert the measurements into square centimeters. Because the same object if close to the camera has many more pixels than a farther object.

## Write the code on the Google Colab notebook

### Installation

As soon as you open the notebook you will need to install Mask R-CNN, just click on the “start” button to install. As in the image below

### Load images and detect legos for Calculate the Area of an object

To make the job faster I have already done the training with Mask R-CNN and you just run the Colab notebook to get started. The result you get should look like this in the image below.

The first step is to identify the object and draw bounding boxes. To facilitate the operations I have already written the code to use Mask R-CNN for this purpose, we only have to use OpenCV to extract the coordinates and do the other processing

```import cv2

for class_id, box, object_contours in zip(class_ids, boxes, masks):
# Box
y1, x1, y2, x2 = box
cv2.rectangle(img, (x1, y1), (x2, y2), colors[class_id], 15)```

By applying the code to the image we obtain this result, it is the first step but to calculate the area is not enough.

By offering all the coordinates we can also draw the mask with OpenCV to see if everything works correctly. Just add these two lines of code.

```    cv2.polylines(img, [object_contours], True, colors[class_id], 10)

and this is the result

## Calculate the Area of an object

Calculating the area is very simple, just pass the points to the OpenCV function cv2.contourArea(object_contours) and we get the area. The only problem is that the unit of measurement is the pixel, so we have to do a little conversion calculation.

Using a tool to calculate pixels such as GIMP and a reference image, in this case, a ruler, we can understand how many pixels correspond to a centimeter, and based on this we can do a conversion ratio operation.

As you can see from the image, we now know that 78 pixels are 1cm. Based on this data, here is the code to convert pixels to square centimeters.

```RATIO_PIXEL_TO_CM = 78 # 78 pixels are 1cm
RATIO_PIXEL_TO_SQUARE_CM = 78 * 78

...

# 2. Calculate area
area_px = cv2.contourArea(object_contours)
area_cm = round(area_px / RATIO_PIXEL_TO_SQUARE_CM, 2)

cv2.putText(img, "A: {}cm^2".format(area_cm), (x1, y1), cv2.FONT_HERSHEY_PLAIN, 3, colors[class_id], 5)```

By adding the text to each lego in the image we can see the area of each individual object.