Note

This notebook can be downloaded here: 05_Image_Processing_Tutorial.ipynb

Tutorial: Counting objects using labeling

Introduction

The goal of this tutorial is to learn basic image processing skills using a simple picture of coins on a table. Your task is to identify and count the coins.

You can use the following image (coins.jpg ) or take a picture of your own and work with it.

Setup

from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
from scipy import ndimage
import os

%matplotlib nbagg

1) Open the file

path = "coins.jpg"
files = os.listdir("./")
if path in files:
    print("Ok, the file is in {0}".format(files))
else:
    print("The file is not in {0} , retry !".format(files))
Ok, the file is in ['coins.jpg', '_data', '02_Image_Processing.ipynb', '06_Practical_Work', '.ipynb_checkpoints', '00_Basics.ipynb', '04_Examples', 'dices.tif', '05_Image_Processing_Tutorial_2.ipynb', '02_Advanced_Examples']
im = Image.open(path)
plt.figure()
plt.imshow(im)
plt.show()
<IPython.core.display.Javascript object>

2) Plot the histogram

Plot the histogram of the image.

3) Thresholding

Use thresholding to convert the image to a binary format.

Erosion / Dilation

Use erosion and dilation to clean the image if needed to isolate each coin.

Labeling

Use the labeling (scipy.ndimage.measurements.label) algorithm to isolate each coin.