P6 - Computer Vision
P6 - Computer Vision
Requirements
Import cv2
1
11/4/22
2
11/4/22
3
11/4/22
Exercise
4
11/4/22
Exercise
10
5
11/4/22
11
12
6
11/4/22
Exercise
13
Image filtering
14
7
11/4/22
Gaussian Filter
filtered = cv2.GaussianBlur(img,(5,5),cv2.BORDER_DEFAULT)
15
OpenCV - putText
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
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
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
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
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
28
14
11/4/22
29
Edge detection
30
15
11/4/22
31
32
16
11/4/22
33
Line detection
34
17
11/4/22
Exercise
35
36
18
11/4/22
Circle detection
37
Exercise
38
19
11/4/22
Contours
39
Contours
40
20
11/4/22
41
Contours example
42
21
11/4/22
Tips on contours
43
Tips on contours
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
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 :
46
23
11/4/22
47
48
24
11/4/22
Example code
49
50
25
11/4/22
Example code
51
Mask application
52
26
11/4/22
53
Exercise
54
27