-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheatsheet.txt
172 lines (122 loc) · 5.64 KB
/
cheatsheet.txt
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
### CheatSheet - OpenCV ###
---images load/write,.. ---
cv2.imread(example.jpg, 1) -1 cv2.IMREAD_COLOR
0 cv2.IMREAD_GRAYSCALE
1 cv2.IMREAD_UNCHANGED
cv2.namedWindow('title of window', cv2.WINDOW_AUTOSIZE) cv2.WINDOW_AUTOSIZE (default)
cv2.WINDOW_NORMAL allows to resize
cv2.waitKey(0) default (for 32bit)
cv2.waitKey(0) & 0xFF 64bit
cv2.VideoCapture(0) number of camera (0 -> webcam)
cap.release Don't forget to release
out.release Don't forget to release
cv2.VideoWriter_fourcc(*'XVID') DIVX, XVID, MJPG, X264, WMV1, WMV2
---drawing---
Coordinate system in OpenCV:
0----------10---> [x]
| (1|1)
| (10|2)
|
| (7|4)
5
|
[y]
cv2.line(img, (pStartX, pStartY), (pEndX, pEndY), (color), (thickness))
example: cv2.line(img, (128, 384), (384, 384), (255, 255, 255), 2)
cv2.rectangle(img, (point top-left), (point bottom-right), (color), thickness)
example: cv2.rectangle(img, (136, 350), (376, 370), (0, 0, 255), 1)
cv2.circle(img, (center), radius, (color), thickness) # thickness (-1) -> filled
example: cv2.circle(img, (312, 175), 20, (0, 0, 0), -1)
cv2.ellipse(img, (center), (|up-wards|, |side-wards|), rotation*, startAngle, stopAngle, (color), thickness)
1*rotation of ellipse in anti-clockwise direction
2*startAngle and endAngle denotes the starting and ending of ellipse arc measured in clockwise direction from major axis
example: cv2.ellipse(img, (256, 246), (60, 30), 0, 0, 180, (0, 0, 0), -1)
points = np.array([[Px1, Py1], [Px2, Py2], [Px3, Py3], ..., ], np.int32)
example: cv2.polylines(img, [points], True, (255, 255, 255)) #255.255.255 = white
#font = cv2.FONT_HERSHEY_DUPLEX -> num[0:7] -> font = 0; 1 .., 7
cv2.putText(img, 'text', (center), font, Size, (color), thickness, cv2.LINE_AA)
example: cv2.putText(img, 'OpenCV', (128, 124), 5, 2.2, (0, 0, 0), 2, cv2.LINE_AA)
---ROI---
objekt = img[h1:h2, w1:w2] # h = height | w = width (1-2 => start - end) ..from the object in the img (that you want to copy/define)
img[hB:hE, wB:wE] = object # here you give the coordinates which (where) pixels [from img] shall be overwriten with pixels [from object]
example:
bubble = img[440:1240, 635:1435] # define bubble
img[0:800, 0:800] = bubble # overwrite img at the given coordinates with 'bubble'
an example at:
/basics/splitMerg.py
/basics/doubleBubble.jpg
0-------------> [width == y]
|
|
|
|
|
[height == x]
So if you are thinking in (x|y) instead of (h|w) because you are used to that x is width and y is height just flip it:
objekt = img[y1:y2, x1:x2] # y = height | x = width (1-2 => start - end) ..from the object in the img (that you want to copy/define)
img[yB:yE, xB:xE] = object # here you give the coordinates which pixels [from img] shall be overwriten with pixels [from object]
# B => beginning, E = ending
---frames / boarder---
name = cv2.copyMakeBorder(src, top, bottom, left, right, borderType)
example:
wrap = cv2.copyMakeBorder(img, 100, 100, 100, 100, cv2.BORDER_WRAP)
#cv2.imshow('wrap', wrap)
#borderTypes:
cv2.BORDER_DEFAULT or cv2.BORDER_REFLECT
img border|img |border
1234 21|1234|43
5678 65|5678|87
cv2.BORDER_REPLICATE
img border|img|border
1234 11|1234|44
5678 55|5678|88
cv2.BORDER_CONSTANT, value=[255, 255, 255] #value, def color 255, 255, 255 [= white]
img border|img |border
1234 color*|1234|color* #color* -> this case: white [255, 255, 255]
5678 color*|5678|color* #color* -> this case: white [255, 255, 255]
cv2.BORDER_ISOLATED
img border|img |border
1234 black|1234|black
5678 black|5678|black
cv2.BORDER_WRAP
img border|img|border
5678
------
1234 34|1234|12
5678 78|5678|56
------
1234
..for better understanding compare:
(git: MrCode97)
OpenCV/basics/border_wrap.jpg
OpenCV/basics/chess1.jpg
---mask---
# convert
convertedFrame = cv2.cvtColor(src/input, code)
example: hsv = cv2.cvtColor(frame, cv2.COLOR)
hls = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)
lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
luv = cv2.cvtColor(frame, cv2.COLOR_BGR2LUV)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
YCbCr = cv2.cvtColor(frame, cv2.COLOR_BGR2YCR_CB)
...
# define (color-)range
lower_color = np.array([hue, saturation, value]) # it's in 'HSV'-format
upper_color = np.array([hue, saturation, value])
example: lower_green = np.array([40,30,30])
upper_green = np.array([70,120,125])
# get the HSV from BGR: # for RGB just use: cv2.COLOR_RGB2HSV
>>> blue = np.uint8([[[255, 0, 0 ]]])
>>> hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
>>> print hsv_blue
[[[120 255 255]]]
# Note that there are a lot of diffrent blue-colors, so you got to know which color-code your color is or have a look with a graphic-program.
# For the range you need to define a range:
=> [H-10, 100, 100] -> [H+10, 100, 100]
H (hue) -> +/- 10
S (saturation) -> lower_color = ~100, upper_color = ~255
V (value) -> lower_color = ~100, upper_color = ~255
# this case:
HSV-lower_blue = [110, 100, 100]
HSV-upper_blue = [130, 255, 255]
*src: http://docs.opencv.org/master/df/d9d/tutorial_py_colorspaces.htmls