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

TP02 - Image Processing Using 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
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
164 views3 pages

TP02 - Image Processing Using 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
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

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 = cv.cvtColor(im,cv.COLOR_RGB2HSV)
import cv2 as cv hls_img = cv.cvtColor(im,cv.COLOR_RGB2HLS)
#Reading and writing images: xyz_img = cv.cvtColor(im,cv.COLOR_RGB2XYZ)
#--------- yuv_img = cv.cvtColor(im,cv.COLOR_RGB2YUV)
# read image #BGR_img = cv.cvtColor(im,cv.COLOR_BGR2RGB)
img = cv.imread(’image5.jpg’) #-----------
print (img.shape) #Displaying images and results:
h,w = img.shape[:2] #-----
print (h,w) cv.imshow("Gray Scale Image", gray_img)
#displaying image cv.waitKey(0)
#------------------ cv.destroyAllWindows()
cv.imshow('Original Image', img) cv.imshow("HSV", hsv_img)
cv.waitKey(0) cv.waitKey(0)
##cv.destroyAllWindows() cv.destroyAllWindows()
# saving image cv.imshow("HLS", hls_img)
#------------ cv.waitKey(0)
cv.imwrite(’result.png’,img) cv.destroyAllWindows()
#Color spaces: cv.imshow("XYZ", xyz_img)
#----------------- cv.waitKey(0)
img = cv.imread(’image15.jpg’) cv.destroyAllWindows()
# create a grayscale version cv.imshow("YUV", yuv_img)
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY) cv.waitKey(0)
#Convert rgb image to grayscale, cv.destroyAllWindows()
gray_img = cv.cvtColor(im, #-----------
cv.COLOR_RGB2GRAY) #Displaying image using matplotlib
#convert RGB to color spaces #----------
1
import matplotlib.pyplot as plt #----------------------------------------
img= cv.imread('image9.jpg') reflect =
plt.imshow(img),plt.axis(‘off’) cv.copyMakeBorder(img2,10,10,10,10,cv.BORDER
plt.show() _REFLECT)#Border will be mirror reflection
of the border elements
Handling 02: Drawing Functions in OpenCV #----------------------------------------
# Create images
reflect101 =
import numpy as np
cv.copyMakeBorder(img2,10,10,10,10,cv.BORDER
image1 = np.zeros((512,512,3), np.uint8)
_REFLECT_101)
image2 = np.ones((512,512,3), np.uint8)*255
wrap =
image3 = np.ones((512,512,3), np.uint8)*128
cv.copyMakeBorder(img2,10,10,10,10,cv.BORDER
rand_=np.random.randint(0,255,(512,512,3))
_WRAP)
imshow(image1)
#----------------------------------------
imshow(image2)
imshow(image3)
constant=
imshow(rand_)
cv.copyMakeBorder(img2,10,10,10,10,cv.BORDER
# Draw a line, rectangle, circle …
_CONSTANT,value=0)#Adds a constant colored
img = np.zeros((512,512,3), np.uint8)
border
cv.line(img,(0,0),(511,511),(255,0,0),5)
#----------------------------------------
#----------
cv.imshow('Original Image', img2)
cv.waitKey(0)
cv.rectangle(img,(384,0),(510,128),(0,255,0)
cv.imshow('REPLICATE', replicate)
,3)
cv.waitKey(0)
#-------
cv.imshow('REFLECT', reflect)
cv.circle(img,(255,255), 100, (0,0,255), 3)
cv.waitKey(0)
#try -1
cv.imshow('REFLECT_101', reflect101)
#--------
cv.waitKey(0)
cv.imshow('WRAP', wrap)
cv.ellipse(img,(256,256),(100,50),0,0,180,(0
cv.waitKey(0)
,0,255),-1)
cv.imshow('CONSTANT', constant)
#--------
cv.waitKey(0)
font = cv.FONT_HERSHEY_SIMPLEX
cv.destroyAllWindows()
cv.putText(img,'TP_Vision',(10,50), font,
print (img2.shape, replicate.shape)
4,(255,255,255),2)
Handling 04: Morphological operations
Handling 03: Basic Operations on Images
img = cv.imread('j.png',0)
# Access to an image pixel value ret,img = cv.threshold(image,127,
255,cv.THRESH_BINARY )
img = cv.imread(’image12.jpg’)
kernel 3= np.ones((3,3),np.uint8)
px = img[100,100]
kernel 5= np.ones((5,5),np.uint8)
print(px)
kernel 7= np.ones((7,7),np.uint8)
# accessing to a specific color pixel(RGB
#Erosion
image)
erosion = cv.erode(img,kernel3,iterations =
Red_px=img[50,10,0]
1)
Green_px=img[50,10,1]
#Dilation
Blue_px=img[50,10,2]
dilation = cv.dilate(img,kernel3,iterations
#modify the pixel value
= 1)
Img[100,100]=[255,255,255]
#Closing
#-------------------
closing = cv.morphologyEx(img,
img.itemset((10,10,2),100)
cv2.MORPH_CLOSE, kernel3)
img.item(10,10,2)
#opening
#Splitting and Merging Image Channels
opening = cv.morphologyEx(img,
r,g,b = cv.split(img)
cv2.MORPH_OPEN, kernel3)
img = cv.merge((r,g,b))
Handling 05: Geometrical transformations
#try
# Scaling
r = img[:,:,0]
Img=imreac(‘images12.jpg’)
g = img[:,:,1]
height, width = img.shape[:2]
b = img[:,:,2]
res = cv.resize(img,(2*width, 2*height),
#Making Borders for Images (Padding)
interpolation = cv.INTER_CUBIC)
img2 = cv.imread('./TP_pictures/image5.jpg')
print(img.shape,res.shape)
#----------------------------------------
# Translation
replicate =
rows,cols = img.shape[:2]
cv.copyMakeBorder(img2,10,10,10,10,cv.BORDER
M = np.float32([[1,0,100],[0,1,50]])
_REPLICATE) #Last element is replicated
dst = cv2.warpAffine(img,M,(cols,rows))
throughout
2
cv2.imshow('img',dst) Handling 06:Arithmetic Operations on Images
cv2.waitKey(0) #Image Blending (𝑑𝑠𝑡 = 𝛼 · 𝑖𝑚𝑔1 + 𝛽 · 𝑖𝑚𝑔2 +
cv2.destroyAllWindows() 𝛾)
#Rotation img1 = cv.imread(image1.jpg')
img = cv2.imread('messi5.jpg',0) img2 = cv.imread('image11.jpg')
rows,cols = img.shape dst = cv.addWeighted(img1,0.7,img2,0.3,0)
M = cv.imshow('dst',dst)
cv2.getRotationMatrix2D((cols/2,rows/2),90,1 cv.waitKey(0)
) cv.destroyAllWindows()
dst = cv2.warpAffine(img,M,(cols,rows)) # Bitwise Operations
# Affine Transformation img1 = cv.imread('images5.jpg')
img = cv2.imread('drawing.png') img2 = cv.imread(‘image10.png')
rows,cols,ch = img.shape 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 = cv2.getAffineTransform(pts1,pts2) # Put logo in ROI and modify the main image
dst = cv2.warpAffine(img,M,(cols,rows)) dst = cv.add(img1_bg,img2_fg)
plt.subplot(121),plt.imshow(img),plt.title(' img1[0:rows, 0:cols ] = dst
Input') cv.imshow('res',img1)
plt.subplot(122),plt.imshow(dst),plt.title(' cv.waitKey(0)
Output') cv.destroyAllWindows()
plt.show()

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

You might also like