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) :
box-1

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 :
box-1-result

2. Code details

Load the libraries :

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

Blur the picture :

box-1-blurred

Convert to gray scale :

box-1-gray

Calculate the threshold level and we apply it :

box-1-bin

Detect contours :

If we draw all the contours :

The result is :
box-1-contours

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

And then we draw it :

box-1-biggest-contour

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 :

box-1-result

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 *