How to count objects on an image with Python

In this tutorial, you will learn how you can count the number of objects on an image with Python using CV2.

This is our test image:

Let’s jump to the code:

First we need to import our dependencies:

import cv2
import numpy as np

First we …


This content originally appeared on DEV Community and was authored by Stokry

In this tutorial, you will learn how you can count the number of objects on an image with Python using CV2.

This is our test image:

enter image description here

Let's jump to the code:

First we need to import our dependencies:

import cv2
import numpy as np

First we need to read our image:

img = cv2.imread('test.jpg')

then we will be converting it into grayscale

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

after that, we doing thresholding on image

_, thresh = cv2.threshold(img, 225, 255, cv2.THRESH_BINARY_INV)
kernal = np.ones((2, 2), np.uint8)

then we are doing dilation process, removing black distortion:

dilation = cv2.dilate(thresh, kernal, iterations=2)

next step is finding contour shapes:

contours, hierarchy = cv2.findContours(
    dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Then we are getting number of contours (objects found):

objects = str(len(contours))

We can now print number of objects on an image

text = "Obj:"+str(objects)
cv2.putText(dilation, text, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX,
            0.4, (240, 0, 159), 1)

For the lasr step we can show, original, threshold and dilation image:

cv2.imshow('Original', img)
cv2.imshow('Thresh', thresh)
cv2.imshow('Dilation', dilation)

cv2.waitKey(0)
cv2.destroyAllWindows()

This is our final result:

enter image description here

Thank you all.


This content originally appeared on DEV Community and was authored by Stokry


Print Share Comment Cite Upload Translate Updates
APA

Stokry | Sciencx (2021-05-03T12:31:17+00:00) How to count objects on an image with Python. Retrieved from https://www.scien.cx/2021/05/03/how-to-count-objects-on-an-image-with-python/

MLA
" » How to count objects on an image with Python." Stokry | Sciencx - Monday May 3, 2021, https://www.scien.cx/2021/05/03/how-to-count-objects-on-an-image-with-python/
HARVARD
Stokry | Sciencx Monday May 3, 2021 » How to count objects on an image with Python., viewed ,<https://www.scien.cx/2021/05/03/how-to-count-objects-on-an-image-with-python/>
VANCOUVER
Stokry | Sciencx - » How to count objects on an image with Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/03/how-to-count-objects-on-an-image-with-python/
CHICAGO
" » How to count objects on an image with Python." Stokry | Sciencx - Accessed . https://www.scien.cx/2021/05/03/how-to-count-objects-on-an-image-with-python/
IEEE
" » How to count objects on an image with Python." Stokry | Sciencx [Online]. Available: https://www.scien.cx/2021/05/03/how-to-count-objects-on-an-image-with-python/. [Accessed: ]
rf:citation
» How to count objects on an image with Python | Stokry | Sciencx | https://www.scien.cx/2021/05/03/how-to-count-objects-on-an-image-with-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.