Use of the getTickCount and getTickFrequency functions

This recipe explains what is the use of the getTickCount and getTickFrequency functions. The getTickCount function returns the count of clock signals and The getTickFrequency function returns the number of clock signals sent in a second.

Recipe Objective: What is the use of the getTickCount() and getTickFrequency() functions?

Let us understand how to calculate the performance of OpenCV using the cv2.getTickCount() and cv2.getTickFrequency() functions

Build a Chatbot in Python from Scratch!

Step 1: Import the library

import cv2

Step 2: Calculate the perform of OpenCV

The cv2.getTickCount() function returns us the count of clock signals that was sent from the reference event to the time cv2.getTickCount() function is called. The reference event may be anything such as the moment when the computer was turned on

The cv2.getTickFrequency() function returns the number of clock signals sent in a second, which can also be called as getTickFrequency

To calculate the time a block of code takes to execute, we can calculate the number of clock signals using cv2.getTickCount() at the beginning and the end of the block of code and divide its difference by the frequency, which can be obtained using the cv2.getTickFrequency() function.

In this example, let us try to perform some basic OpenCV manipulations such as converting the image to grayscale and drawing a rectangle and find the time taken for its execution.

c1 = cv2.getTickCount()
image1=cv2.imread('project.jpg')
image_converted = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
cv2.rectangle(image1,(0,0),(image1.shape[1],image1.shape[0]),255,5)
c2 = cv2.getTickCount()
time_taken = (c2 - c1)/ cv2.getTickFrequency()

Now let see how much time has the above chunk of code taken to execute

print(f'The time taken for execution is {time_taken}')

Output:

    The time taken for execution is 0.0071479

Download Materials

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.