JimYuan's Blog

Sharing the things I learned

0%

OpenCV_Course_Note

Preface

I have absolutely zero idea of the openCV library before watching this tutorial. I hope I can take away some useful knowledge by noting down some code here.

Reference

youtube
github

Reading Image and Video

Read Image

1
2
3
4
5
6
import cv2 as cv

img = cv.imread('../Resources/Photos/cats.jpg')
cv.imshow('Cats', img)

cv.waitKey(0)

Read Video

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
capture = cv.VideoCapture('../Resources/Videos/dog.mp4')

while True:
isTrue, frame = capture.read()

# if cv.waitKey(20) & 0xFF==ord('d'):
# This is the preferred way - if `isTrue` is false (the frame could
# not be read, or we're at the end of the video), we immediately
# break from the loop.
if isTrue:
cv.imshow('Video', frame)
if cv.waitKey(20) & 0xFF==ord('d'):
break
else:
break

capture.release()
cv.destroyAllWindows()

Resizing and Rescaling

We often resize and rescale the image to relieve the computational strain.

Drawing Shapes and Putting Texts

5 Essential Function in OpenCV

  • Converting to grayScale
  • Blur
  • Edge Cascade
  • Dilating the image
  • Eroding
  • Resize

Converting to grayScale

1
gray = cv.cvtColor(img, cv.COLOR2GRAY)

Blur

1
blur = cv.GussianBlur(img, (7,7), cv.BORDER_DEFAULT)

Edge Cascade

1
2
canny = cv.Canny(img, 125, 175,)
canny = cv.Canny(cv.GussianBlur(img, (7,7), cv.BORDER_DEFAULT, 125,175) # passing blur image

Dilating the image

1
dilated = cv.dilated(canny, (3,3), iteration=1)

Eroding

1
eroded = cv.erode(dilated, (3,3), iteration=3)

Resize

1
resized = cv.resized(img, (500, 500), interpolation=cv.INTER_CUBIC)

Cropping

1
cropped = img[50:200, 200:400]

Image Transformation

  • Translation
  • Rotation
  • Resizing
  • Flipping
  • Cropping

Contour detection