Counting and measuring plastic from an image

Counting and measuring plastic from an image

Feb 2, 2018

So as a result of getting lots of samples of plastics from our lovely colleagues in Greece Dave asked if there was any way around automating the process of counting and measuring the hundreds (if not thousands (if not millions(if not millions))) of pieces of plastic collected from select beaches in Greece.

Using a combination of Office Lens to take the pic and flatten it from my phone, run it through the OpenCV library I managed to get a good idea of the sizes as well as the count of each object on the image.

Preparing for the photo: I used a white square envelope (with good straight 90 degree corners) on a distinguishable background to allow for the app to pick up and readjust the photo to “flatten” the image.

From your phone: Download and run Office Lens or other lens distortion correction software and align the edge of the white envelope to allow the photo to properly project correct relative distances whether on the side of the image or dead-center.

Prepare the scripts: I used script-posts found here and here to make the following:

# python --image images/example_01.png --width 0.955
# python --image images/example_02.png --width 0.955
# python --image images/example_03.png --width 3.5

# import the necessary packages
from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
from colorlabeler import ColorLabeler
import numpy as np
import argparse
import imutils
import cv2

cl = ColorLabeler()

def midpoint(ptA, ptB):
  return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
  help="path to the input image")
ap.add_argument("-w", "--width", type=float, required=True,
  help="width of the left-most object in the image (in inches)")
args = vars(ap.parse_args())

# load the image, convert it to grayscale, and blur it slightly
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)

# perform edge detection, then perform a dilation + erosion to
# close gaps in between object edges
edged = cv2.Canny(gray, 50, 100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)

# find contours in the edge map
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

# sort the contours from left-to-right and initialize the
# 'pixels per metric' calibration variable
(cnts, _) = contours.sort_contours(cnts)
pixelsPerMetric = None

count = 0;
# loop over the contours individually
for c in cnts:
  # if the contour is not sufficiently large, ignore it
  if cv2.contourArea(c) < 100:

  # compute the rotated bounding box of the contour
  orig = image.copy()
  box = cv2.minAreaRect(c)
  box = if imutils.is_cv2() else cv2.boxPoints(box)
  box = np.array(box, dtype="int")

  # order the points in the contour such that they appear
  # in top-left, top-right, bottom-right, and bottom-left
  # order, then draw the outline of the rotated bounding
  # box
  box = perspective.order_points(box)
  cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)

  cv2.drawContours(orig, c, 0, (0,255,0), 3)
  # loop over the original points and draw them
  for (x, y) in box:, (int(x), int(y)), 5, (0, 0, 255), -1)

  # unpack the ordered bounding box, then compute the midpoint
  # between the top-left and top-right coordinates, followed by
  # the midpoint between bottom-left and bottom-right coordinates
  (tl, tr, br, bl) = box
  (tltrX, tltrY) = midpoint(tl, tr)
  (blbrX, blbrY) = midpoint(bl, br)

  # compute the midpoint between the top-left and top-right points,
  # followed by the midpoint between the top-righ and bottom-right
  (tlblX, tlblY) = midpoint(tl, bl)
  (trbrX, trbrY) = midpoint(tr, br)

  # draw the midpoints on the image, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1), (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1), (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1), (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)

  # draw lines between the midpoints
  cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
    (255, 0, 255), 2)
  cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
    (255, 0, 255), 2)

  # compute the Euclidean distance between the midpoints
  dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
  dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))

  # if the pixels per metric has not been initialized, then
  # compute it as the ratio of pixels to supplied metric
  # (in this case, inches)
  if pixelsPerMetric is None:
    pixelsPerMetric = dB / args["width"]

  # compute the size of the object
  dimA = dA / pixelsPerMetric
  dimB = dB / pixelsPerMetric

  # draw the object sizes on the image
  cv2.putText(orig, "{:.1f}mm".format(dimA),
    (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,
    0.65, (255, 255, 255), 2)
  cv2.putText(orig, "{:.1f}mm".format(dimB),
    (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
    0.65, (255, 255, 255), 2)
  count += 1
  colour = cl.label(lab, c)
  print dimA,",",dimB,",",cv2.contourArea(c)/pixelsPerMetric/pixelsPerMetric,",",colour

  # show the output image
  cv2.imshow("Image", orig)
print("total count:",count)

# import the necessary packages
from scipy.spatial import distance as dist
from collections import OrderedDict
import numpy as np
import cv2

class ColorLabeler:
  def __init__(self):
    # initialize the colors dictionary, containing the color
    # name as the key and the RGB tuple as the value
    colors = OrderedDict({
      "red": (255, 0, 0),
      "green": (0, 255, 0),
      "blue": (0, 0, 255)})

    # allocate memory for the L*a*b* image, then initialize
    # the color names list
    self.lab = np.zeros((len(colors), 1, 3), dtype="uint8")
    self.colorNames = []

    # loop over the colors dictionary
    for (i, (name, rgb)) in enumerate(colors.items()):
      # update the L*a*b* array and the color names list
      self.lab[i] = rgb

    # convert the L*a*b* array from the RGB color space
    # to L*a*b*
    self.lab = cv2.cvtColor(self.lab, cv2.COLOR_RGB2LAB)

  def label(self, image, c):
    # construct a mask for the contour, then compute the
    # average L*a*b* value for the masked region
    mask = np.zeros(image.shape[:2], dtype="uint8")
    cv2.drawContours(mask, [c], -1, 255, -1)
    mask = cv2.erode(mask, None, iterations=2)
    mean = cv2.mean(image, mask=mask)[:3]

    # initialize the minimum distance found thus far
    minDist = (np.inf, None)

    # loop over the known L*a*b* color values
    for (i, row) in enumerate(self.lab):
      # compute the distance between the current L*a*b*
      # color value and the mean of the image
      d = dist.euclidean(row[0], mean)

      # if the distance is smaller than the current distance,
      # then update the bookkeeping variable
      if d < minDist[0]:
        minDist = (d, i)

    # return the name of the color with the smallest distance
    return self.colorNames[minDist[1]]

From the command-line: for these two scripts to be useable you need to install some python packages so run

pip install opencv-python imutils numpy scipy

Then to run it:

python --image 010.jpg --width 21.2

where 010.jpg should be replaced with your image name in the same folder as the scripts and the 21.2 should be replaced with the reference object width (the width of the object the furthest left of the screen). Your output should show csv output like this:

21.4837708449 , 21.25 , 358.13935634 , red
21.5861672381 , 21.290466317 , 359.746048603 , red
19.9598121722 , 19.5162607906 , 305.29338973 , red
26.7609333568 , 26.9087838173 , 565.337078992 , red
22.1637654081 , 22.7910417876 , 396.109757168 , red
('total count:', 5)

where the first number is width, second height, third area in mm^2 and the fourth is experimental and a colour-tag followed at the bottom of the list by a final count of all objects


  • properly correct lens distortion using grid-map convex filter in photoshop or gimp
  • allow x and y aligned rulers in image without them being counted in final object count
  • GUI for OSX/Win with drag + drop functionality, output window + option to download csv