0% found this document useful (0 votes)
75 views27 pages

P6 - Computer Vision

OpenCV is an open source library used for computer vision and machine learning. It provides a common infrastructure for computer vision applications and accelerates machine perception in commercial products. OpenCV allows users to process images and videos in real-time, including tasks like object detection, face recognition, and optical character recognition.

Uploaded by

Raul Marian
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)
75 views27 pages

P6 - Computer Vision

OpenCV is an open source library used for computer vision and machine learning. It provides a common infrastructure for computer vision applications and accelerates machine perception in commercial products. OpenCV allows users to process images and videos in real-time, including tasks like object detection, face recognition, and optical character recognition.

Uploaded by

Raul Marian
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
You are on page 1/ 27

11/4/22

OpenCV (Open Source Computer Vision


Library) is an open source computer
vision and machine learning software
library.
OpenCV was built to provide a common
infrastructure for computer vision
applications and to accelerate the use of
machine perception in the commercial
products.

Requirements

pip install opencv-python

Try if your installation is fine:

Import cv2

1
11/4/22

Real-time image processing

+ We will use OpenCV for real-time image processing.


+ The process consists of collecting one image from
camera and apply several image processing techniques
as soon as possible. Then, we collect other image and do
the same process.
+ Real-time means collecting images each X milliseconds.

import cv2 # import the opencv library


vid = cv2.VideoCapture(0) # define a video capture object
while(True):
ret, frame = vid.read() # Capture the video frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release() # After the loop release the cap object
cv2.destroyAllWindows() # Destroy all the windows

2
11/4/22

Grayscale and colour images

+ The shape of gray and colour images is not the same.


+ For each pixel you will have three dimensions to
represent the number of channels in a colour image.
+ Grayscale images are often used to reduce the
computational complexity.

Convert image to grayscale or HSV

+ We can convert an image to a grayscale easily using:


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+ Moreover, to convert na image to HSV we can use:
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
+ OpenCV works with BGR not RGB.

3
11/4/22

Exercise

Convert video to grayscale and HSV.

Resizing Images using OpenCV

+ Resizing images is an important task in image processing.


+ For example, for building models we need to collect data. The
data collected is not always in the same size. Therefore, it is
necessary to resize data.
+ We can use the resize function from OpenCV.

resized = cv2.resize(frame, (200,200), interpolation= cv2.INTER_LINEAR)

4
11/4/22

Exercise

Resize the image to (200,200).

import cv2 # import the opencv library


vid = cv2.VideoCapture(0) # define a video capture object
while(True):
ret, frame = vid.read() # Capture the video frame
rotated = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('frame', rotated)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release() # After the loop release the cap object
cv2.destroyAllWindows() # Destroy all the windows

10

5
11/4/22

Flip Images Horizontally and


Vertically
+ We can flip an image both horizontally and vertically.
+ This creates a mirror image along the horizontal/vertical
axis.
+ flip code: A flag to specify how to flip the array; 0 means
flipping around the x-axis and positive value (for example,
1) means flipping around y-axis. Negative value (for
example, -1) means flipping around both axes.

11

import cv2 # import the opencv library


vid = cv2.VideoCapture(0) # define a video capture object
while(True):
ret, frame = vid.read() # Capture the video frame
fliped = cv2.flip(frame, 1)
cv2.imshow('frame', fliped)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release() # After the loop release the cap object
cv2.destroyAllWindows() # Destroy all the windows

12

6
11/4/22

Exercise

Crop the image x = 80 to 280 and y = 150 to 350

13

Image filtering

+ Filtering is a common image operation in image


processing.
+ Filtering can be used to remove noise or to enhance
features.
+ The filtered image could be the desired result or just a
pre-processing step.

14

7
11/4/22

Gaussian Filter

+ The OpenCV Gaussian filtering provides


the cv2.GaussianBlur() method to blur an image by using a
Gaussian Kernel.
+ Each pixel in an image gets multiplied by a Gaussian Kernel.
+ It means, a Gaussian Kernel is a square array of pixels.

filtered = cv2.GaussianBlur(img,(5,5),cv2.BORDER_DEFAULT)

15

OpenCV - putText

+ In several applications is interesting to write text on


images for debugging!
+ We can easily do this in OpenCV by using putText
function.

16

8
11/4/22

OpenCV - putText

+ font = cv2.FONT_HERSHEY_SIMPLEX
+ pos = (0, 50)
+ fontScale = 1
+ color = (255, 0, 0)
+ thickness = 2
image = cv2.putText(image, 'OpenCV’, pos, font, fontScale,
color, thickness, cv2.LINE_AA)

17

Utilities for real world projects

+ OpenCV has a lot of libraries to support real world


projects please run the following script to install them:

+ pip install opencv-python-headless

18

9
11/4/22

Barcode
Reader
Barcodes are a major
technique to identify
commodities in real life.

19

Example

+ import cv2
+ bardet = cv2.barcode_BarcodeDetector()
+ img = cv2.imread("your file path")
+ ok, decoded_info, decoded_type, corners =
bardet.detectAndDecode(img)

20

10
11/4/22

Exercise

+ Create a real-time application that readers barcodes in


real-time.
+ This application must show the barcodes using putText
function.

21

QRcode
Reader
QRcodes are a major
technique to identify
commodities in real life.

22

11
11/4/22

Example

import cv2
detector = cv2. QRCodeDetector()
img = cv2.imread("your file path")
data, vertices_array, binary_qrcode =
detector.detectAndDecode(frame)

23

Exercise

+ Create a real-time application that readers Qrcodes in


real-time.
+ This application must show the barcodes using putText
function.

24

12
11/4/22

Optical Character
Recognition (OCR)

+ An optical character
recognition (OCR) tool
can recognize and
“read” the text
embedded in images.

25

pytesseract 0.3.10

+ Python-tesseract is a wrapper for Google’s Tesseract-OCR


Engine.
+ It is also useful as a stand-alone invocation script to tesseract,
as it can read image types such as jpeg, png, gif, bmp, tiff,
and others.
pip install pytesseract
Install Tesseract windows installer
https://github.com/UB-Mannheim/tesseract/wiki

26

13
11/4/22

Python code

+ import pytesseract
+ pytesseract.pytesseract.tesseract_cmd =
"C:/ProgramFiles/Tesseract-OCR/tesseract.exe"
+ import cv2
+ frame = cv2.imread(r'/<path_to_image>/digits.png')
+ text = pytesseract.image_to_string(frame, lang='eng’)
+ print(text)

27

Exercise

+ Create a real-time application that provides OCR in real-


time.
+ This application must show the collected information
using putText function.
+ Note: you should collect and analyze images every
second (1s).

28

14
11/4/22

Edge detection in OpenCV

+ Edge detection is an image processing technique for


finding the boundaries of objects within images.
+ It mainly works by detecting discontinuities in brightness.
+ One of the most popular and widely used algorithm is
Canny edge detector.

29

Edge detection

30

15
11/4/22

import cv2 # import the opencv library


vid = cv2.VideoCapture(0) # define a video capture object
while(True):
ret, frame = vid.read() # Capture the video frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, threshold1=30, threshold2=100)
cv2.imshow('edges', edges)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release() # After the loop release the cap object
cv2.destroyAllWindows() # Destroy all the windows

31

Line and circle detection

+ The Hough transform is a technique which can be used to


isolate features of a particular shape within an image.
+ The classical Hough transform is most commonly used for the
detection of regular curves such as lines, circles, ellipses, etc.
+ The main advantage of the Hough transform technique is that
it is tolerant of gaps in feature boundary descriptions and is
relatively unaffected by image noise.

32

16
11/4/22

Hough Line Transform

+ cv2.HoughLinesP() function finds line segments in a


binary image using the probabilistic Hough transform.
+ cv2. HoughLines() function finds lines in a binary image
using the standard Hough transform.
+ You should check documentation to understand the
parameters.

33

Line detection

lines = cv2.HoughLinesP(edges, 1, np.pi/180, 60, np.array([]), 50, 5)


if lines is not None:
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(frame, (x1, y1), (x2, y2), (20, 220, 20), 3)
cv2.imshow('lines', frame)

34

17
11/4/22

Exercise

+ Create a real-time application that detects lines in real-


time.

35

Hough Circle Transform

+ HoughCircles() function Finds circles in a grayscale


image using the Hough transform.

You should check documentation to understand the


parameters.

36

18
11/4/22

Circle detection

circles = cv2.HoughCircles(image=img, method=cv2.HOUGH_GRADIENT,


dp=0.9, minDist=80, param1=110, param2=39, maxRadius=70)
if circles is not None:
circles = np.uint16(np.around(circles))
for co, i in enumerate(circles[0, :], start=1):
# draw the outer circle in green
cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle in red
cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3)

37

Exercise

+ Create a real-time application that detects circles in real-


time.
+ Print the number of circles detected in your terminal.
+ You can use coins for the experiments.

38

19
11/4/22

Contours

+ Contours can be explained as a curve joining all the continuous


points, having same color or intensity. The contours are a useful
tool for shape analysis and object detection and recognition.
+ For better accuracy it is recommended to use binary images. So
before finding contours, apply threshold or canny edge detection.
+ In OpenCV, finding contours is like finding white object from black
background. Consequently, the object to be found should be white
and background should be black.

39

Contours

findContours() - Finds contours in a binary image.


drawContours() - Draws contours outlines or filled contours.
The function draws contour outlines in the image if
𝚝𝚑𝚒𝚌𝚔𝚗𝚎𝚜𝚜≥0 or fills the area bounded by the contours if
𝚝𝚑𝚒𝚌𝚔𝚗𝚎𝚜𝚜<0 .

40

20
11/4/22

41

Contours example

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
_, binary = cv2.threshold(gray, 225, 255,
cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
image = cv2.drawContours(image, contours, -1, (0, 255, 0), 2)

42

21
11/4/22

Tips on contours

+ Sometimes we can find a lot of different contours.


+ Therefore, to get the contour with max area we can use
the following :
c = max(contours, key = cv2.contourArea)

43

Tips on contours

+ Sometimes, we may need to approximate a contour area


to a rectangle.
+ Therefore, we can use the following :

x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(output,(x,y),(x+w,y+h),(0,255,0),2)

44

22
11/4/22

Tips on contours

+ Sometimes, we may need to get the center of a contour.


+ Therefore, we can use the following :

M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

45

Tips on contours
+ Sometimes, we may need to approximate the contour .
+ Therefore, we can use the following :

peri = cv2.arcLength(c, True)


approx = cv2.approxPolyDP(c, 0.001*peri, True)
output = cv2.drawContours(img, approx, -1, (0, 0, 255), 3)

46

23
11/4/22

Detect Objects of Similar Color

+ Color detection is a technique of detecting a particular


color in a given range of HSV color space.
+ Image segmentation is the process of partitioning digital
image and labeling every pixel, where each pixel having
the same label shares certain characteristics.

47

Detect the color from the input


image and create a mask
+ OpenCV reads the frame as BGR colorspace.
+ However, to detect any color, first, we must convert the
frame to HSV color space using cv2.cvtColor function.
+ Then, we must define a range for color detection using a
lower and upper limits
+ inRange() function returns a binary mask of the frame
where the green color is present.

48

24
11/4/22

Example code

+ # convert to hsv colorspace


+ hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
+ # lower bound and upper bound for Green color
+ lower_bound = np.array([50, 20, 20])
+ upper_bound = np.array([100, 255, 255])
+ # find the colors within the boundaries
+ mask = cv2.inRange(hsv, lower_bound, upper_bound)

49

Removing unnecessary noise from


masks
+ In the mask, we can see that there is lots of unnecessary noise.
Therefore, we must remove the noise to get a better result.
+ np.ones((7,7),np.uint8) create a 5×5 8-bit integer matrix.
+ cv2.MORPH_CLOSE removes unnecessary black noises from
the white region.
+ cv2.MORPH_OPEN removes white noise from the black
region of the mask

50

25
11/4/22

Example code

+ #define kernel size


+ kernel = np.ones((5,5),np.uint8)
+ # Remove unnecessary noise from mask
+ mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
+ mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

51

Mask application

segmented_img = cv2.bitwise_and(img, img, mask=mask)


bitwise_and(source1, source2, destination_array, mask)
+ cv2.bitwise_and() applies mask on frame in only that
region where the mask is true means white.

52

26
11/4/22

Draw a Boundary of the detected


objects
contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
output = cv2.drawContours(segmented_img, contours, -1, (0, 0, 255), 3)

+ cv2.findcontour finds all the continuous points along the


boundary
+ cv2.drawContours draws all the contour points

53

Exercise

+ Create a real-time application that detects green objects


in real-time.
+ Print draw the boundaries of the objects.
+ Advanced: detect only circles!
+ Expert: detect only the one with higher area.

54

27

You might also like