# 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 :

# Code details

``````import numpy as np
import cv2``````

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

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

Blur the picture :

``blurred = cv2.GaussianBlur(img, (5, 5), 0)``

Convert to gray scale :

``gray = cv2.cvtColor(blurred,cv2.COLOR_BGR2GRAY)``

Calculate the threshold level and we apply it :

``````threshold = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
retval, bin = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)``````

Detect contours :

``bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)``

If we draw all the contours :

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

The result is :

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

``````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``````

And then we draw it :

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

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 :

``````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)``````

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

``````cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()``````
``````cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()``````

# Code overview

``````import numpy as np
import cv2

#Gaussian blur
blurred = cv2.GaussianBlur(img, (5, 5), 0)
#Convert to graysscale
gray = cv2.cvtColor(blurred,cv2.COLOR_BGR2GRAY)
#Autocalculate the thresholding level
#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()``````
Built with Hugo
Theme Stack designed by Jimmy