In this video, we will see how to calculate the dimensions of a fixed camera object on a flat surface. For example, if we want to measure the sizes of the nuts we can use the diameter.

measure size of the nuts final result

Measuring the size of an object can be applied to anything, no matter what its outline, color, or background, and in this tutorial, we will see which technology to use and how best to do it.

1. Measure the size of objects, but first identifies the object and its mask

To measure the size of an object, it is necessary to identify its position in the image in order to detach it from the background. This procedure must be done for each individual object and a segmentation algorithm must be used to get the precise shape, in this case, we use Mask R-CNN.

I will not go into the details of this operation because I have covered the subject several times but it is necessary to search as many images as possible of the object to be measured, make the annotations with specific software (makesense.ai) and train a custom model.

annotation with mask r-cnn

Train a custom model

To simplify the training of a custom model for Mask R-CNN I created a notebook with Google Colab that allows you to simplify the steps to set up the training: You can Train Mask R-CNN to detect any custom object, easily and quickly.

mask r-cnn training course

Using the file, the result of the training, with Mask R-CNN it can be seen how the algorithm perfectly recognizes the objects and their shape.

2. Convert Mask into cm

Thanks to Mask R-CNN we obtained the mask of the object and consequently its position in space but to measure the object it is not enough, we have to convert pixel to cm to calculate the diameter.

I extract the object’s position and proceed with the calculations

       for i in range(object_count):
            # 1. Rectangle
            y1, x1, y2, x2 = r["rois"][i]
            cv2.rectangle(img,(x1, y1), (x2, y2), (25, 15, 220), 3)

            # Width
            width = x2 - x1
            #cv2.putText(img, str(width), (x1 , y1 - 10), cv2.FONT_HERSHEY_PLAIN, 1, (25, 15, 220), 2)

            # 1.4 CM = 153
            # 14 MM = 153
            # Ratio Pixels to MM
            ratio_px_mm = 153 / 14
            mm = width / ratio_px_mm
            cm = mm / 10

and adding the text, I get this result

pixel to cm ratio

we got the first result but the figure is not perfectly visible, with some corrections to the code

            cv2.rectangle(img, (x1, y1 - 60), (x1 + 270, y1 - 5), (25, 15, 220), -1)
            cv2.putText(img, "{} CM".format(round(cm, 2)), (x1, y1 - 10), cv2.FONT_HERSHEY_PLAIN, 4, (255, 255, 255), 3)

everything becomes clearer

computer vision measure nuts