Computer vision : A box on a industrial conveyor

1. Introduction

To start exploring OpenCV with Python, we’ll start by a simple exercise : Detect a cardboard box on a industrial conveyor (from a average picture took with a smartphone) :

As as first step, we’ll identity the contour of the box and define a “perfect” rotated rectangle of the minimum area enclosing our box. The result will be this :

2. Code details

Load the libraries :

Read the picture (here the picture “box-1.jpg is in the same directory) :

Blur the picture :


Convert to gray scale :


Calculate the threshold level and we apply it :


Detect contours :

If we draw all the contours :

The result is :

That’s why, to do it quick, we sort out the biggest and convex (close) contour:

And then we draw it :


Now we just have the contour that we wanted, but it’s not a rectangle !

Let’s draw a rotated rectangle of the minimum area enclosing our box, in red :


And to finish, just the code to show a picture in a good size windows in Python :

3. Code overview

Leave a Reply

Your email address will not be published. Required fields are marked *