People Counter 5 – Morphological Transformations

First of all, sorry for taking so long for this post, I´ve been really busy with work and couldn´t find the time to write this.

Let´s start! This fifth part of the tutorial will cover basic transformations.

I recommend reading this and  this first to get an overview of what a transformation is and what it might be used for (You´ll only need to read about Erosion and Dilation, Opening and Closing).

Esentially, we will use erosion and dialtion on binarized images (black and white). In a very general manner, erosion expands a black portion of the image into a white portion. Dilation, on the other hand, expands a white portion of the image into a black portion.

To do this operations you also need to specify a kernel or structuring element (strel). This is a matrix that is convoluted on the image of size n*n that defines the area to use when calculating the value of each pixel.

Let try them on this image:


Use this code:

import cv2
import numpy as np

img = cv2.imread("noise.png")
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

kernel = np.ones((3,3),np.uint8)

erosion = cv2.erode(img,kernel,iterations = 1)

dilation = cv2.dilate(img,kernel,iterations = 1)



We´re useing the threshold method to binarize the image. Convert it from color to only black and white, two values (not the same as grayscale). Look at how it works here.

Run it and look at the output images. Also, try changing the kernel´s size (5,5 or 9,9, for example) and see what happens.

You can also combine both operations, running one after the other.

Doing and erosion and then a dilation is called opening.

On the other hand, doing a dilation and then an erosion is called closing.

Let´s try them on this image:


Use the following code:

import cv2
import numpy as np

img = cv2.imread("letters.jpg")
ret,thresh1 = cv2.threshold(img,200,255,cv2.THRESH_BINARY)

kernel = np.ones((5,5),np.uint8)

opening = cv2.morphologyEx(thresh1, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(thresh1, cv2.MORPH_CLOSE, kernel)



Look at the output and try to understand what happened during closing and opening.

Now try incorporating this code to our people counter, right after background substraction, to take the shadows (gray color) out and make the video stream clear (take out any noise), to take it from this:

peoplecount filter2

To this:

peoplecount filter3

It´s right to even try combining opening and closing operations, even with different sizes of kernel. Experiment with them!

You can also use this operations on color images to get cool results, although that´s not useful for the counter.

(Click on the images to view them full size)

Original Opening (9,9) Closing (9,9)
fordGT fordGT_opening fordGT_closing

That´s it for now. If you have any questions leave them in the comments.

Next, we´ll look at finding contours from the clean clean image to later detect them as moving people.

134 thoughts on “People Counter 5 – Morphological Transformations”

  1. Hey bro, nice tutorial. I’m also from México and i’m working on a similar project. Look, i have already realized BS, Filtering, contours detection and a basic blob tracking system. However i still have some false-positives which i would like to reduce to 0. I’m interested on the “Defining a person” section you mention over your first post. Could you please give me a hand on this? i’d appreciate it.

  2. Hello Fede,
    Your guide is so nice, able to make a python script upto filtering, as we know, other steps are very important to do the counting (IN and OUT) in this project, so please help me on this.
    Can you please update the step 6,7,8 and 9 ??
    If you have very minimal script with simple documentation for rest of the steps, can you please send me an email ?
    Thanks for the help.

  3. Hi Fede,

    Awesome work I’m very much looking forward to the next tutorials. If you can’t get around to completing the tutorials soon please do send the code to I’ve built something very similar with matlab and I’m pretty sure that I can work it out.


  4. Pingback: Cialis kaufen
  5. Pingback: Viagra cost
  6. Pingback: Canadian viagra
  7. Pingback: Buy cialis
  8. Pingback: Cialis 20 mg
  9. Pingback: Online viagra
  10. Pingback: Cialis purchasing
  11. Pingback: sirius movs6907
  12. Pingback: newtube sirius871
  13. Pingback: comment284
  14. Pingback: comment412
  15. Pingback: comment852
  16. Pingback: comment829
  17. Pingback: comment986
  18. Pingback: comment267
  19. Pingback: comment538
  20. Pingback: comment357
  21. Pingback: comment638
  22. Pingback: comment720
  23. Pingback: comment616
  24. Pingback: comment455
  25. Pingback: comment5
  26. Pingback: comment294
  27. Pingback: comment470
  28. Pingback: comment104
  29. Pingback: comment958
  30. Pingback: comment203
  31. Pingback: comment729
  32. Pingback: comment905
  33. Pingback: comment758
  34. Pingback: comment383
  35. Pingback: comment397
  36. Pingback: comment227
  37. Pingback: comment899
  38. Pingback: comment375
  39. Pingback: comment108
  40. Pingback: comment51
  41. Pingback: comment585
  42. Pingback: comment109
  43. Pingback: comment21
  44. Pingback: comment304
  45. Pingback: comment251
  46. Pingback: comment439
  47. Pingback: comment540
  48. Pingback: comment125
  49. Pingback: comment667
  50. Pingback: comment833
  51. Pingback: comment859
  52. Pingback: comment755
  53. Pingback: comment237
  54. Pingback: comment54
  55. Pingback: comment889
  56. Pingback: comment146
  57. Pingback: comment103
  58. Pingback: comment602
  59. Pingback: comment698
  60. Pingback: comment683
  61. Pingback: comment484
  62. Pingback: comment922
  63. Pingback: comment521
  64. Pingback: comment862
  65. Pingback: comment325
  66. Pingback: dsbdiablos
  67. Pingback:
  68. Pingback: appnewrelease
  69. Pingback: click here
  70. Pingback: tube

Leave a Reply