Learn Opencv - 10 Code Examples & CST Typing Practice Test
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning library that provides tools for real-time image and video processing across multiple platforms.
View all 10 Opencv code examples →
Learn OPENCV with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
OpenCV Simple Image Read and Display
import cv2
img = cv2.imread('example.jpg')
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
A minimal OpenCV example reading an image and displaying it in a window.
OpenCV Convert Image to Grayscale
import cv2
img = cv2.imread('example.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
Reads an image and converts it to grayscale.
OpenCV Resize Image
import cv2
img = cv2.imread('example.jpg')
resized = cv2.resize(img,(200,200))
cv2.imshow('Resized', resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
Resizes an image to a specific width and height.
OpenCV Draw Shapes on Image
import cv2
import numpy as np
img = np.zeros((300,300,3), dtype=np.uint8)
cv2.rectangle(img,(50,50),(250,250),(0,255,0),2)
cv2.circle(img,(150,150),50,(255,0,0),2)
cv2.line(img,(0,0),(300,300),(0,0,255),2)
cv2.imshow('Shapes', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Draws rectangle, circle, and line on an image.
OpenCV Image Thresholding
import cv2
img = cv2.imread('example.jpg',0)
_,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
cv2.imshow('Threshold', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
Applies binary thresholding to a grayscale image.
OpenCV Edge Detection
import cv2
img = cv2.imread('example.jpg',0)
edges = cv2.Canny(img,100,200)
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
Detects edges using the Canny algorithm.
OpenCV Video Capture
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('Webcam', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Captures video from webcam and displays it.
OpenCV Image Blurring
import cv2
img = cv2.imread('example.jpg')
blur = cv2.GaussianBlur(img,(5,5),0)
cv2.imshow('Blurred', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
Applies Gaussian blur to an image.
OpenCV Face Detection with Haar Cascades
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('face.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('Faces', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Detects faces in an image using Haar cascades.
OpenCV Video Frame Processing
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Video', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Processes video frame by frame and converts to grayscale.
Frequently Asked Questions about Opencv
What is Opencv?
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning library that provides tools for real-time image and video processing across multiple platforms.
What are the primary use cases for Opencv?
Image and video processing (filtering, transformations, enhancement). Object detection and recognition. Facial recognition and emotion detection. Motion tracking and optical flow analysis. Machine learning integration for vision-based applications
What are the strengths of Opencv?
Open-source with active community. Extensive documentation and tutorials. High performance for real-time applications. Wide range of algorithms for classical CV tasks. Cross-language support for developers
What are the limitations of Opencv?
Steeper learning curve for beginners. Limited high-level deep learning features compared to frameworks. Sometimes inconsistent API between C++ and Python. GPU support requires setup with CUDA or OpenCL. Not ideal for large-scale training from scratch
How can I practice Opencv typing speed?
CodeSpeedTest offers 10+ real Opencv code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.