记录一下相关的API, 方便查找

basic

img

1
2
3
4
5
6
import cv2

img = cv2.imread('Path')
cv2.imshow('Cat', img)

cv2.waitKey(0)  #等待特定延迟

video

1
2
3
4
5
6
7
8
9
capture = cv2.VideoCapture('Path')
# 摄像头 capture = cv2.VideoCapture(0) 第1个摄像头
while True:
	isTrue, frame = capture.read()
	cv2.imshow('Video', frame)
    if cv.waitKey(20) & 0xFF == ord('d'):
        break
capture.relase()
cv2.destoryAllWindows()

resize & rescale

method 1 适用于图像和视频

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import cv2 

def rescaleFrame(frame, scale=0.75):
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)
    dimensions = (width, height)
    return cv2.resize(frame, dimensions, interpolation=cv2.INTER_AREA)

capture = cv2.VideoCapture('dde-introduction.mp4')

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

    fream_resized = rescaleFrame(frame)
    cv2.imshow('Video', frame)
    cv2.imshow('Video Resized', fream_resized)
    if cv2.waitKey(20) & 0xFF == ord('d'):
        break
       
capture.release()
cv2.destroyAllWindows()

method 2 仅使用与视频

1
2
3
def changeRes(width, height):  #更改视频图像分辨率
    capture.set(3, width)
    capture.set(4, height)

Drawing Shapes & Putting Text

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 1. Paing the Image a certain colour
blank[200:300, 100:200] = 0,255,255
cv2.imshow('Green', blank)
# 2. Draw a Rectangle
cv2.rectangle(blank, (0,0), (250,250), (0,255,0), thickness=cv2.FILLED)
cv2.imshow('Rectangle', blank)
# 3. Draw a circle 
cv2.circle(blank, (40,40), 40, (0,0,255), thickness=3)
cv2.imshow('Circle', blank)
# 4. Draw a line
cv2.line(blank, (0,0), (40, 40), (255,0,0), thickness=3)
cv2.imshow('line', blank)
# 5. Write text
cv2.putText(blank, "Hello, World!", (255,255), cv2.FONT_HERSHEY_TRIPLEX, 1.0, (0,255,255), 2)
cv2.imshow('text', blank)

Essential Functions

 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
33
34
35
36
import cv2

img = cv2.imread('1.jpg')
cv2.imshow('BGR', img)

# Converting to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray', gray)

# Blur -> 轻微的模糊,减少噪音
blur = cv2.GaussianBlur(img, (7,7), cv2.BORDER_DEFAULT)
cv2.imshow('Blur', blur)

#  Edge Cascade -> 应用模糊, 消除无用边缘
canny = cv2.Canny(blur, 125, 175)
cv2.imshow('Canny Edge', canny)

# Dilating the image  -> 扩展图像, 边缘增强
dilated = cv2.dilate(canny, (7,7), iterations=3)
cv2.imshow('Dilated', dilated)

# Frading # 图像腐蚀 消除Dialte
eroded = cv2.erode(dilated, (7,7), iterations=3)
cv2.imshow('Eroded', eroded)

# Resize
resized = cv2.resize(img, (500, 500), interpolation=cv2.INTER_CUBIC)
cv2.imshow('Resized', resized)

# Cropping
cropped = img[50:200, 200:400]
cv2.imshow('Croped', cropped)


cv2.waitKey(0)

Image Transformations

 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
33
34
35
36
37
38
39
import cv2
import numpy as np

img = cv2.imread('1.jpg')
cv2. imshow('Img', img)

# Translation 平移 
def translate(img, x, y):
    transMat = np.float32([[1,0,x], [0,1,y]])
    dimensions = (img.shape[1], img.shape[0])
    return cv2.warpAffine(img, transMat, dimensions)

translated = translate(img, -100, -100)
cv2.imshow('Translated', translated)

# Rotation 旋转
def rotate(img, angle, rotPoint=None):
    (height, width) = img.shape[:2]
    if rotPoint is None:
        rotPoint = (width // 2, height // 2)
    rotMat = cv2.getRotationMatrix2D(rotPoint, angle, 1.0)
    dimensions = (width, height)
    return cv2.warpAffine(img, rotMat, dimensions)
rotated = rotate(img, -45)
cv2.imshow('Rotated', rotated)

# Resizing
resized = cv2.resize(img, (500,500), interpolation=cv2.INTER_CUBIC)
cv2.imshow('Resized', resized)

# Flipping 翻转
flip = cv2.flip(img, 1) # 0: 垂直翻转 1:水平翻转 -1:垂直和水平翻转
cv2.imshow('Flip', flip)

# Cropping
cropped = img[200:400, 300:400]
cv2.imshow('Cropped', cropped)

cv2.waitKey(0)

Contour Detection

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import cv2
import numpy as np

img = cv2.imread('1.jpg')

cv2.imshow('Img', img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray', gray)

blur = cv2.GaussianBlur(gray, (5,5), cv2.BORDER_DEFAULT)
cv2.imshow('Blur',  blur)

# method1: cv2.Canny
canny = cv2.Canny(blur, 125, 175)
cv2.imshow('Canny', canny)
contours, hierarchies = cv2.findContours(canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
print(len(contours))

cv2.waitKey(0)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# method2: 二值化图像
blank = np.zeros(img.shape, dtype='uint8')
cv2.imshow('Blank', blank)

ret, thresh = cv2.threshold(gray, 125, 255, cv2.THRESH_BINARY)
cv2.imshow('Thresh', thresh)

contours, hierarchies = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
print(len(contours))

cv2.drawContours(blank, contours, -1, (0,255,255), thickness=1)
cv2.imshow("Contours Drawn", blank)

advanced

Color Spaces

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# BGR to Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray', gray)

# BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow('HSV', hsv)

# BGR to lab
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
cv2.imshow('LAB', lab)

# BGR to RGB
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow('RGB', rgb)

cv2.imshow('Img', img)  # bgr
plt.imshow(img)   # rgb
plt.show()

Color Channels

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
blank = np.zeros(img.shape[:2], dtype='uint8')

# Split Channels
b, g, r = cv2.split(img)

blue = cv2.merge([b, blank, blank])
green = cv2.merge([blank, g, blank])
red = cv2.merge([blank, blank, r])

cv2.imshow('Blue', blue)
cv2.imshow('Green', green)
cv2.imshow('Red', red)
'''
cv2.imshow('Blue', b)
cv2.imshow('Greed', g)
cv2.imshow('Red', r)
'''
print(img.shape)
print(b.shape)
print(g.shape)
print(r.shape)
# Merge
merged =cv2.merge([b,g,r])
cv2.imshow('Merged', merged)

Blurring Techniques 平滑、模糊

kernel or window :

  • size : called kernel size

sigmoid:对其他块的影响力

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Average Blur
average = cv2.blur(img, (3, 3))
cv2.imshow('Average Blur', average)

# Gaussian Blur
gauss =cv2.GaussianBlur(img, (3,3), 0)
cv2.imshow('Gaussian Blur', gauss)

# Median BLur
median = cv2.medianBlur(img, 3)
cv2.imshow('Median Blur', median)

# Bilateral  应用模糊, 但会保留图像中的边缘
bilateral = cv2.bilateralFilter(img, 10, 35, 35)
cv2.imshow('Bilateral', bilateral)

Bitwise_Operations 按位运算

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
blank = np.zeros((400, 400), dtype='uint8')

rectangle = cv2.rectangle(blank.copy(), (30, 30), (370, 370), 255, -1)
circle = cv2.circle(blank.copy(), (200, 200), 200, 255, -1)

cv2.imshow('Rectangle', rectangle)
cv2.imshow('Circle', circle)

# bitwise AND --> itersecting regions
bitwise_and = cv2.bitwise_and(rectangle, circle)
cv2.imshow('Bitwise AND', bitwise_and)

# bitwise OR  --> non-intersecting and intersecting regions
bitwise_or = cv2.bitwise_or(rectangle, circle)
cv2.imshow('Bitwise OR', bitwise_or)

# bitwise XOR  --> non-intersecting regions
bitwise_xor = cv2.bitwise_xor(rectangle, circle)
cv2.imshow('Bitwise XOR', bitwise_xor)

# bitwise NOT
bitwise_not = cv2.bitwise_not(rectangle)
cv2.imshow('Bitwise NOT', bitwise_not)

Masking

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
blank = np.zeros(img.shape[:2], dtype='uint8')
#cv2.imshow('Blank', blank)

circle = cv2.circle(blank.copy(), (img.shape[1]//2 - 135, img.shape[0]//2 - 200), 100, 255, -1)
#cv2.imshow('Circle', mask)
rectangle = cv2.rectangle(blank.copy(), (30, 30), (370, 370), 255, -1)
weird_shape =cv2.bitwise_and(circle, rectangle)

masked = cv2.bitwise_and(img, img, mask=weird_shape)
cv2.imshow('Masked Image', masked)

Computing Histograms