Azus

Never be a sad song with nothing to say.

0%

OpenCV

第一次接触计算机视觉,决定在Python下使用Opencv库,学习图像处理的同时熟悉Python语法。

Python@3.8 PyCharm with Opencv and numpy

1. 获取视频和播放视频

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2
import time
print("package imported")

cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture("Resource/out.mp3")
cap.set(3, 640)
cap.set(4,480)
while True:
success, img = cap.read()
# time.sleep(1)
cv2.imshow("Video", img)
if cv2.waitKey(1) & 0xFF ==ord('q'):
break
  • 打开视频接口cv2.Videocapture()创造一个Videiocapture变量, 参数可以是设备索引cv2.VideoCapture(0)或视频文件cv2.VideoCapture("path")
  • 以帧为单位读取显示视频
img
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

对于已获取的视频可以使用 ```Videocapture.set(propID, value) ``` 对属性进行修改:

> args中propID:
3-视频流中帧的宽度
4-视频流中帧的高度
5-帧率
6-编解码器的4字符代码
10-图像的亮度--仅适用于相机
11-对比度--仅用于相机
12-饱和度--仅用于相机


[OpencvProp属性](https://www.cnblogs.com/StarZhai/p/11905818.html)


## Opencv中的长宽坐标:

—— ——X
|
|
V y

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
在Opencv中,坐标表示为(width, height)
## 图像预处理函数
图像转换、高斯模糊、边缘检测、边缘增强,削弱
```python
import cv2
import time
import numpy as np
print("package imported")

# all the values in the matrix is '1'
kernel = np.ones((5,5), np.uint8)
# Grayscale:
img = cv2.imread("Resources/lena.png")
# # Grayscale convert
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# blur (7,7)(ksize) must be odd numbers
imgBlur = cv2.GaussianBlur(imgGray, (19, 19), 0)
# # Edge detector
imgCanny = cv2.Canny(img, 100, 100)
# enlarge the thickness (src, mtx, iterations
imgDialation = cv2.dilate(imgCanny, kernel, iterations=1)
# decrease thickness
imgEroded = cv2.erode(imgDialation, kernel, iterations=1)

#
# #output
cv2.imshow("cvtColor", imgGray)
cv2.imshow("BlurImg", imgBlur)
cv2.imshow("GrayImg", imgCanny)
cv2.imshow("Dialation Img", imgDialation)
cv2.imshow("Eroded Img", imgEroded)
cv2.waitKey(0)

Crop and Resize

图像放缩(任意比例)、图像裁切

1
2
3
4
5
6
7
8
9
10
11
12
13
img = cv2.imread("Resources/lambo.png")
print(img.shape)
# (hight, width, number for channels(BGR))
# resize function
#* note that in Opencv (width, height),
imgResized = cv2.resize(img, (300, 300))
# cropping imgs (height, width)
# keep height from 0-200, width 200-500
imgCropped = img[0:200, 200:500]

cv2.imshow("lambo", img)
cv2.imshow("lambo but smaller", imgResized)
cv2.imshow("lambo but cropped", imgCropped)

画图(线,点,圆,文字)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# SHAPES AND TEXTS
img = np.zeros((512,512, 3), np.uint8)
print(img.shape)
# img[:] = 255,0,0
# : states the limits of the img (height, width)
# [:] means the whole img(the whole matrix)
# drawing lines note that img is a matrix and img.shape[0] represents the first factor in img.shape(the height)
cv2.line(img, (0,0), (img.shape[1], img.shape[0]), (0, 255, 0), 3)
cv2.rectangle(img,(0, 0), (250, 250), (0, 0, 255), cv2.FILLED)
# cv2.FILLED in the thickness parameter fills the
cv2.circle(img, (400, 50), 30, (255, 255, 0), 5)
# src, Text, font, fontScale, color thickness
cv2.putText(img, "opencv", (300, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 150, 0), 1)

cv2.imshow("Image", img)

透视变换(四组点)

仿射变换(三组) 线性变换
相关线性代数方程原理参EssenceofLinearAlgbra

1
2
3
4
5
6
7
8
9
10
11
12
13
# WRAP PERSPECTIVE
width, height = 250, 350
img = cv2.imread("Resources/cards.jpg")
pts1 = np.float32([[111, 219], [287, 188], [154, 482], [352, 440]])
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
# transform matrix
matrix = cv2.getPerspectiveTransform(pts1, pts2)
imgOutPut = cv2.warpPerspective(img, matrix, (width, height))

cv2.imshow("output", imgOutPut)
cv2.imshow("original", img)

cv2.waitKey(0)

Calculation
Calculation