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 :

img = cv2.imread('box-1.jpg')

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 :

cv2.drawContours(img, contours, -1, (0, 255, 0), 3 )

The result is :
box-1-contours

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

cv2.drawContours(img, contours, max_index, (0, 255, 0), 3 )

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 :


import numpy as np
import cv2

#Read image
img = cv2.imread('box-1.jpg')
#Gaussian blur
blurred = cv2.GaussianBlur(img, (5, 5), 0)
#Convert to graysscale
gray = cv2.cvtColor(blurred,cv2.COLOR_BGR2GRAY)
#Autocalculate the thresholding level
threshold = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
#Threshold
retval, bin = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)
#Find contours
bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
#Sort out the biggest contour (biggest area)
max_area = 0
max_index = -1
index = -1
for i in contours:
    area = cv2.contourArea(i)
    index=index+1
    if area > max_area :
        max_area = area
        max_index = index

#Draw the raw contours
cv2.drawContours(img, contours, max_index, (0, 255, 0), 3 )
cv2.imwrite("box-1-biggest-contour.png", img)

#Draw a rotated rectangle of the minimum area enclosing our box (red)
cnt=contours[max_index]
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
img = cv2.drawContours(img,[box],0,(0,0,255),2)

#Show original picture with contour
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Leave a Reply

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