Count number of Object using Python-OpenCV Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will use image processing to count the number of Objects using OpenCV in Python.Google Colab link: https://colab.research.google.com/drive/10lVjcFhdy5LVJxtSoz18WywM92FQAOSV?usp=sharingModule neededOpenCv: OpenCv is an open-source library that is useful for computer vision applications such as image processing, video processing, facial recognition, and detection, etc.Numpy: Numpy is a python package for scientific computing. It is a popular math library for Machine Learning. The main Object of Numpy is a multidimensional array.Matplotlib: Matplotlib is a Python library used for data visualization and graphical plotting of the data.Image Used:.Stepwise implementationStep 1: Import required libraries. Python # Import libraries import cv2 import numpy as np import matplotlib.pyplot as plt Step 2: We will read the image by using "cv2.imread(image-name)" command & then convert this image into grayscale image using "cv2.cvtColor(image-name, cv2.COLOR_BGR2GRAY)" command. Python image = cv2.imread('coins.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) plt.imshow(gray, cmap='gray') Output:Step 3: For counting, we have to detect the edges but before detecting the edges we have to make the image blur to avoid the noises. Use "cv2.GaussianBlur(image-name, Kernal size, std. deviation)". Python blur = cv2.GaussianBlur(gray, (11, 11), 0) plt.imshow(blur, cmap='gray') Output: Step 4: Now we will detect edges using a canny algorithm, 2nd & 3rd parameters in cv2.canny() function are threshold values. a value between 30 & 150 are consider as an edge for this image. Python canny = cv2.Canny(blur, 30, 150, 3) plt.imshow(canny, cmap='gray') Output:Step 5: We can see that edges are not connected. We need to connect the edges, have to make more thiker & visible. Python dilated = cv2.dilate(canny, (1, 1), iterations=0) plt.imshow(dilated, cmap='gray') Output:Step 6: Now we have to calculate the contour in the image & convert the image into RGB from BGR & then draw the contours. Python (cnt, hierarchy) = cv2.findContours( dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.drawContours(rgb, cnt, -1, (0, 255, 0), 2) plt.imshow(rgb) Output:Step 7: Printing the result Python print("coins in the image : ", len(cnt)) Output:coins in the image: 5Below is the complete implementation: Python # Import libraries import cv2 import numpy as np import matplotlib.pyplot as plt image = cv2.imread('coins.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (11, 11), 0) canny = cv2.Canny(blur, 30, 150, 3) dilated = cv2.dilate(canny, (1, 1), iterations=0) (cnt, hierarchy) = cv2.findContours( dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.drawContours(rgb, cnt, -1, (0, 255, 0), 2) print("coins in the image : ", len(cnt)) Output: coins in the image : 5Get the complete notebook link: click here. Count number of Object using Python-OpenCV Comment A asmitapatidar2023 Follow Improve A asmitapatidar2023 Follow Improve Article Tags : Computer Vision AI-ML-DS OpenCV Explore Introduction to Computer VisionComputer Vision - Introduction 4 min read A Quick Overview to Computer Vision 3 min read Applications of Computer Vision 6 min read Fundamentals of Image Formation 7 min read Satellite Image Processing 2 min read Image Formats 5 min read Image Processing & TransformationDigital Image Processing Basics 7 min read Difference Between RGB, CMYK, HSV, and YIQ Color Models 3 min read Image Enhancement Techniques using OpenCV - Python 15+ min read Image Transformations using OpenCV in Python 5 min read How to find the Fourier Transform of an image using OpenCV Python? 5 min read Python | Intensity Transformation Operations on Images 5 min read Histogram Equalization in Digital Image Processing 5 min read Python - Color Inversion using Pillow 4 min read Image Sharpening using Laplacian, High Boost Filtering in MATLAB 3 min read Wand sharpen() function - Python 2 min read Python OpenCV - Smoothing and Blurring 7 min read Python PIL | GaussianBlur() method 1 min read Apply a Gauss filter to an image with Python 2 min read Spatial Filtering and its Types 3 min read Python PIL | MedianFilter() and ModeFilter() method 1 min read Python | Bilateral Filtering 2 min read Python OpenCV - Morphological Operations 5 min read Erosion and Dilation of images using OpenCV in Python 3 min read Introduction to Resampling methods 8 min read Python | Image Registration using OpenCV 3 min read Feature Extraction and DescriptionFeature Extraction Techniques - NLP 10 min read SIFT Interest Point Detector Using Python - OpenCV 4 min read Feature Matching using Brute Force in OpenCV 13 min read Feature detection and matching with OpenCV-Python 5 min read Feature matching using ORB algorithm in Python-OpenCV 3 min read Mahotas - Speeded-Up Robust Features 2 min read Create Local Binary Pattern of an image using OpenCV-Python 5 min read Deep Learning for Computer VisionImage Classification using CNN 5 min read What is Transfer Learning? 8 min read Top 5 PreTrained Models in Natural Language Processing (NLP) 7 min read ML | Introduction to Strided Convolutions 2 min read Dilated Convolution 5 min read Continuous Kernel Convolution 6 min read CNN | Introduction to Pooling Layer 5 min read CNN | Introduction to Padding 5 min read What is the difference between 'SAME' and 'VALID' padding in tf.nn.max_pool of tensorflow? 14 min read Convolutional Neural Network (CNN) Architectures 11 min read Deep Transfer Learning - Introduction 8 min read Introduction to Residual Networks 4 min read Residual Networks (ResNet) - Deep Learning 9 min read ML | Inception Network V1 4 min read Understanding GoogLeNet Model - CNN Architecture 3 min read Image Recognition with Mobilenet 4 min read VGG-16 | CNN model 6 min read Autoencoders in Machine Learning 8 min read How Autoencoders works ? 6 min read Difference Between Encoder and Decoder 9 min read Implementing an Autoencoder in PyTorch 4 min read Generative Adversarial Network (GAN) 12 min read Deep Convolutional GAN with Keras 9 min read StyleGAN - Style Generative Adversarial Networks 5 min read Object Detection and RecognitionDetect an object with OpenCV-Python 4 min read Haar Cascades for Object Detection - Python 3 min read R-CNN - Region-Based Convolutional Neural Networks 8 min read YOLO v2 - Object Detection 7 min read Face recognition using Artificial Intelligence 15+ min read Deep Face Recognition 8 min read ML | Face Recognition Using Eigenfaces (PCA Algorithm) 4 min read Emojify using Face Recognition with Machine Learning 7 min read Object Detection with Detection Transformer (DETR) by Facebook 7 min read Image SegmentationImage Segmentation Using TensorFlow 5 min read Thresholding-Based Image Segmentation 7 min read Region and Edge Based Segmentation 4 min read Image Segmentation with Watershed Algorithm - OpenCV Python 9 min read Mask R-CNN | ML 9 min read 3D ReconstructionPython OpenCV - Depth map from Stereo Images 2 min read Top 7 Modern-Day Applications of Augmented Reality (AR) 10 min read Virtual Reality, Augmented Reality, and Mixed Reality 3 min read Camera Calibration with Python - OpenCV 4 min read Python OpenCV - Pose Estimation 7 min read 40+ Top Computer Vision Projects [2025 Updated] 4 min read Like