0% found this document useful (0 votes)
102 views3 pages

Image Processing with Python & OpenCV

This document provides instructions for a practical work session on image processing using Python OpenCV. It introduces basic concepts of OpenCV including its use for image handling, color space conversion, drawing functions, morphological operations, and geometric transformations. The objective is for students to understand fundamental computer vision concepts through hands-on exercises with OpenCV in Python.

Uploaded by

Belkacem Hedadi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views3 pages

Image Processing with Python & OpenCV

This document provides instructions for a practical work session on image processing using Python OpenCV. It introduces basic concepts of OpenCV including its use for image handling, color space conversion, drawing functions, morphological operations, and geometric transformations. The objective is for students to understand fundamental computer vision concepts through hands-on exercises with OpenCV in Python.

Uploaded by

Belkacem Hedadi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIVERSITE KASDI MERBAH OUARGLA

Faculté de Technologie
Département D’électronique et télécommunication
Filière : Électronique des systèmes embarqué
Année universitaire : 2022/2023
Option : ESEM
Niveau : Master 2
Enseignant : MOKADEM Zakaria.
Module : TP Computer Vision (Vision Artificielle)

TP N° : 02
Image Processing using Python-OpenCV
The main objective of this set of practical work is that the student understand the basic classroom concepts of
computer vision using python IDE and OpenCV .
What you need to know?
• Basic programming experience: You need to know how to use an editor and run scripts.
• basic concepts of computer vision
Basic Concepts:
OpenCV (Open Source Computer Vision) :
OpenCV is a free cross-platform library for real-time image processing that has become a standard tool for all things
related to Computer Vision. The applications for OpenCV cover areas such as segmentation and recognition, 2D and
3D feature toolkits, object identification, facial recognition, motion tracking, gesture recognition, image stitching,
high dynamic range (HDR) imaging, augmented reality, and so on.

Basic OpenCV Handling


Handling 01: image handling hsv_img = [Link](im,cv.COLOR_RGB2HSV)
import cv2 as cv hls_img = [Link](im,cv.COLOR_RGB2HLS)
#Reading and writing images: xyz_img = [Link](im,cv.COLOR_RGB2XYZ)
#--------- yuv_img = [Link](im,cv.COLOR_RGB2YUV)
# read image #BGR_img = [Link](im,cv.COLOR_BGR2RGB)
img = [Link](’[Link]’) #-----------
print ([Link]) #Displaying images and results:
h,w = [Link][:2] #-----
print (h,w) [Link]("Gray Scale Image", gray_img)
#displaying image [Link](0)
#------------------ [Link]()
[Link]('Original Image', img) [Link]("HSV", hsv_img)
[Link](0) [Link](0)
##[Link]() [Link]()
# saving image [Link]("HLS", hls_img)
#------------ [Link](0)
[Link](’[Link]’,img) [Link]()
#Color spaces: [Link]("XYZ", xyz_img)
#----------------- [Link](0)
img = [Link](’[Link]’) [Link]()
# create a grayscale version [Link]("YUV", yuv_img)
gray = [Link](img,cv.COLOR_BGR2GRAY) [Link](0)
#Convert rgb image to grayscale, [Link]()
gray_img = [Link](im, #-----------
cv.COLOR_RGB2GRAY) #Displaying image using matplotlib
#convert RGB to color spaces #----------
1
import [Link] as plt #----------------------------------------
img= [Link]('[Link]') reflect =
[Link](img),[Link](‘off’) [Link](img2,10,10,10,10,[Link]
[Link]() _REFLECT)#Border will be mirror reflection
of the border elements
Handling 02: Drawing Functions in OpenCV #----------------------------------------
# Create images
reflect101 =
import numpy as np
[Link](img2,10,10,10,10,[Link]
image1 = [Link]((512,512,3), np.uint8)
_REFLECT_101)
image2 = [Link]((512,512,3), np.uint8)*255
wrap =
image3 = [Link]((512,512,3), np.uint8)*128
[Link](img2,10,10,10,10,[Link]
rand_=[Link](0,255,(512,512,3))
_WRAP)
imshow(image1)
#----------------------------------------
imshow(image2)
imshow(image3)
constant=
imshow(rand_)
[Link](img2,10,10,10,10,[Link]
# Draw a line, rectangle, circle …
_CONSTANT,value=0)#Adds a constant colored
img = [Link]((512,512,3), np.uint8)
border
[Link](img,(0,0),(511,511),(255,0,0),5)
#----------------------------------------
#----------
[Link]('Original Image', img2)
[Link](0)
[Link](img,(384,0),(510,128),(0,255,0)
[Link]('REPLICATE', replicate)
,3)
[Link](0)
#-------
[Link]('REFLECT', reflect)
[Link](img,(255,255), 100, (0,0,255), 3)
[Link](0)
#try -1
[Link]('REFLECT_101', reflect101)
#--------
[Link](0)
[Link]('WRAP', wrap)
[Link](img,(256,256),(100,50),0,0,180,(0
[Link](0)
,0,255),-1)
[Link]('CONSTANT', constant)
#--------
[Link](0)
font = cv.FONT_HERSHEY_SIMPLEX
[Link]()
[Link](img,'TP_Vision',(10,50), font,
print ([Link], [Link])
4,(255,255,255),2)
Handling 04: Morphological operations
Handling 03: Basic Operations on Images
img = [Link]('[Link]',0)
# Access to an image pixel value ret,img = [Link](image,127,
255,cv.THRESH_BINARY )
img = [Link](’[Link]’)
kernel 3= [Link]((3,3),np.uint8)
px = img[100,100]
kernel 5= [Link]((5,5),np.uint8)
print(px)
kernel 7= [Link]((7,7),np.uint8)
# accessing to a specific color pixel(RGB
#Erosion
image)
erosion = [Link](img,kernel3,iterations =
Red_px=img[50,10,0]
1)
Green_px=img[50,10,1]
#Dilation
Blue_px=img[50,10,2]
dilation = [Link](img,kernel3,iterations
#modify the pixel value
= 1)
Img[100,100]=[255,255,255]
#Closing
#-------------------
closing = [Link](img,
[Link]((10,10,2),100)
cv2.MORPH_CLOSE, kernel3)
[Link](10,10,2)
#opening
#Splitting and Merging Image Channels
opening = [Link](img,
r,g,b = [Link](img)
cv2.MORPH_OPEN, kernel3)
img = [Link]((r,g,b))
Handling 05: Geometrical transformations
#try
# Scaling
r = img[:,:,0]
Img=imreac(‘[Link]’)
g = img[:,:,1]
height, width = [Link][:2]
b = img[:,:,2]
res = [Link](img,(2*width, 2*height),
#Making Borders for Images (Padding)
interpolation = cv.INTER_CUBIC)
img2 = [Link]('./TP_pictures/[Link]')
print([Link],[Link])
#----------------------------------------
# Translation
replicate =
rows,cols = [Link][:2]
[Link](img2,10,10,10,10,[Link]
M = np.float32([[1,0,100],[0,1,50]])
_REPLICATE) #Last element is replicated
dst = [Link](img,M,(cols,rows))
throughout
2
[Link]('img',dst) Handling 06:Arithmetic Operations on Images
[Link](0) #Image Blending (𝑑𝑠𝑡 = 𝛼 · 𝑖𝑚𝑔1 + 𝛽 · 𝑖𝑚𝑔2 +
[Link]() 𝛾)
#Rotation img1 = [Link]([Link]')
img = [Link]('[Link]',0) img2 = [Link]('[Link]')
rows,cols = [Link] dst = [Link](img1,0.7,img2,0.3,0)
M = [Link]('dst',dst)
cv2.getRotationMatrix2D((cols/2,rows/2),90,1 [Link](0)
) [Link]()
dst = [Link](img,M,(cols,rows)) # Bitwise Operations
# Affine Transformation img1 = [Link]('[Link]')
img = [Link]('[Link]') img2 = [Link](‘[Link]')
rows,cols,ch = [Link] inv_ = cv.bitwise_not(b_img1)
pts1 = img1_bg = cv.bitwise_and(b_img1,bimg2)
np.float32([[50,50],[200,50],[50,200]]) img1_bg = cv.bitwise_or(b_img1,img2)
pts2 = img1_bg = cv.bitwise_xor(b_img1,img2)
np.float32([[10,100],[200,50],[100,250]]) # Take only region of logo from logo image.
M = [Link](pts1,pts2) # Put logo in ROI and modify the main image
dst = [Link](img,M,(cols,rows)) dst = [Link](img1_bg,img2_fg)
[Link](121),[Link](img),[Link](' img1[0:rows, 0:cols ] = dst
Input') [Link]('res',img1)
[Link](122),[Link](dst),[Link](' [Link](0)
Output') [Link]()
[Link]()

Assignment:
Write a python code, can concatenate (collect) multiple images into one.

Common questions

Powered by AI

Padding techniques, such as replicate, reflect, wrap, and constant, modify the edges of an image differently. These variations influence results in filtering and convolution operations by controlling how border pixels are handled, impacting edge detection accuracy and feature extraction dependence on border continuity .

In OpenCV, several border types are available, including BORDER_REPLICATE, BORDER_REFLECT, BORDER_REFLECT_101, BORDER_WRAP, and BORDER_CONSTANT. BORDER_REPLICATE extends the border by replicating the outermost values, whereas BORDER_REFLECT and BORDER_REFLECT_101 create a mirror reflection around the border. BORDER_WRAP repeats the image as is, and BORDER_CONSTANT adds a user-defined constant color. Each border type can impact image processing tasks by influencing how edge pixels are handled during convolution operations or morphological transformations .

OpenCV handles drawing functions such as lines, rectangles, circles, and ellipses using functions like `cv.line()`, `cv.rectangle()`, and `cv.circle()`. These functions allow annotation and visualization of key features on images, aiding in debugging and interpreting data by highlighting regions of interest or outlining detected objects .

OpenCV provides functionalities for real-time image processing, which include image segmentation and recognition, object identification, facial recognition, motion and gesture tracking, and augmented reality applications. Other applications include 2D and 3D feature toolkits, high dynamic range imaging, and image stitching .

OpenCV facilitates image color space conversions through functions such as `cv.cvtColor()`, allowing conversions between different color spaces like RGB, HSV, HLS, XYZ, and YUV formats. This is important because different tasks in computer vision, such as object detection and tracking, require different color models to be used effectively for analyzing specific image features .

Python, with its intuitive syntax and powerful libraries like OpenCV, provides a versatile platform for computer vision development. OpenCV offers extensive tools for image processing, feature detection, and object tracking. Combined with Python's libraries for machine learning, data processing, and visualization, developers can create robust applications in areas like autonomous vehicles, surveillance systems, and augmented reality .

Image blending in OpenCV is performed using `cv.addWeighted()`, which combines two images based on specified weights (alpha and beta) and an optional gamma value for brightness adjustment. Practical applications include creating composite images, generating transition effects, and developing augmented reality experiences where multiple image layers must be combined seamlessly .

Geometric transformations in OpenCV are carried out using functions like `cv.resize()`, `cv2.warpAffine()`, and `cv2.getRotationMatrix2D()`. Scaling, translation, rotation, and affine transformations modify the orientation, size, and positioning of images. These operations are significant for tasks like image registration, normalization, and enhancement where alignment or resizing to a specific format is required .

Basic operations include accessing and modifying pixel values, image channel splitting and merging, and creating image padding using various border types. These operations form the foundation for advanced tasks by allowing direct manipulation and examination of image data, facilitating preprocessing steps like noise reduction and image enhancements crucial for effective analysis and processing .

Morphological operations in OpenCV, like `cv.erode()`, `cv.dilate()`, `cv.morphologyEx()`, are pivotal for refining binary images. These operations help remove noise, fill holes, and separate connected objects. They enhance image features by manipulating image shapes, useful in preprocessing for better feature extraction and recognition tasks .

You might also like