FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
opencv_tutorials/python/code_046/opencv_046.py at master · HZHCoder1990/opencv_tutorials · GitHub
HZHCoder1990
/
opencv_tutorials
Public
forked from
JimmyHHua/opencv_tutorials
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
opencv_tutorials
/
python
/
code_046
/
opencv_046.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (34 loc) · 1.13 KB
Breadcrumbs
opencv_tutorials
/
python
/
code_046
/
opencv_046.py
Copy path
File metadata and controls
43 lines (34 loc) · 1.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
cv2
as
cv
import
numpy
as
np
def
connected_components_demo
(
src
):
src
=
cv
.
GaussianBlur
(
src
, (
3
,
3
),
0
)
gray
=
cv
.
cvtColor
(
src
,
cv
.
COLOR_BGR2GRAY
)
ret
,
binary
=
cv
.
threshold
(
gray
,
0
,
255
,
cv
.
THRESH_BINARY
|
cv
.
THRESH_OTSU
)
cv
.
imshow
(
"binary"
,
binary
)
cv
.
imwrite
(
'binary.png'
,
binary
)
output
=
cv
.
connectedComponents
(
binary
,
connectivity
=
8
,
ltype
=
cv
.
CV_32S
)
num_labels
=
output
[
0
]
print
(
num_labels
)
# output: 5
labels
=
output
[
1
]
# 构造颜色
colors
=
[]
for
i
in
range
(
num_labels
):
b
=
np
.
random
.
randint
(
0
,
256
)
g
=
np
.
random
.
randint
(
0
,
256
)
r
=
np
.
random
.
randint
(
0
,
256
)
colors
.
append
((
b
,
g
,
r
))
colors
[
0
]
=
(
0
,
0
,
0
)
# 画出连通图
h
,
w
=
gray
.
shape
image
=
np
.
zeros
((
h
,
w
,
3
),
dtype
=
np
.
uint8
)
for
row
in
range
(
h
):
for
col
in
range
(
w
):
image
[
row
,
col
]
=
colors
[
labels
[
row
,
col
]]
cv
.
imshow
(
"colored labels"
,
image
)
cv
.
imwrite
(
"labels.png"
,
image
)
print
(
"total componets : "
,
num_labels
-
1
)
src
=
cv
.
imread
(
"pill.png"
)
h
,
w
=
src
.
shape
[:
2
]
connected_components_demo
(
src
)
cv
.
waitKey
(
0
)
cv
.
destroyAllWindows
()
Back
|
FazBrowse Home
|
New Git URL