Eight Essential OpenCV Examples for Image Processing

This article introduces eight fundamental OpenCV examples—including image reading, display, grayscale conversion, edge detection, resizing, Gaussian blur, and face detection—providing concise Python code snippets and explanations to help readers quickly apply these common computer‑vision techniques.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Eight Essential OpenCV Examples for Image Processing

OpenCV is a widely used library for computer‑vision tasks, offering many algorithms for image and video processing. Below are eight common examples that demonstrate basic operations.

01. Read Image

Read an image from disk using cv2.imread():

import cv2
image = cv2.imread('image.jpg')

02. Display Image

Show the loaded image in a window and wait for a key press:

import cv2
image = cv2.imread('image.jpg')
cv2.imshow('Image', image)
cv2.waitKey(0)

03. Grayscale Conversion

Convert the image to grayscale before displaying:

import cv2
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Image', gray)
cv2.waitKey(0)

04. Edge Detection

Detect edges using the Canny algorithm:

import cv2
image = cv2.imread('image.jpg')
edges = cv2.Canny(image, 100, 200)
cv2.imshow('Edges', edges)
cv2.waitKey(0)

05. Resize Image

Resize the image to a fixed size (500×500) with area interpolation:

import cv2
image = cv2.imread('image.jpg')
resized = cv2.resize(image, (500, 500), interpolation=cv2.INTER_AREA)
cv2.imshow('Resized Image', resized)
cv2.waitKey(0)

06. Gaussian Blur

Apply a Gaussian blur to smooth the image:

import cv2
image = cv2.imread('image.jpg')
blur = cv2.GaussianBlur(image, (5,5), 0)
cv2.imshow('Blurred Image', blur)
cv2.waitKey(0)

07. Face Detection

Detect faces using a Haar cascade classifier and draw rectangles around them:

import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('Faces', image)
cv2.waitKey(0)

08. Conclusion

OpenCV is a powerful tool for a wide range of image and video processing tasks. The eight examples above illustrate just a fraction of its capabilities; they can be combined and extended to meet specific project requirements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Computer VisionPythonImage ProcessingCode ExamplesTutorialOpenCV
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.