Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CCV
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Marc Feger
CCV
Commits
51843207
Commit
51843207
authored
6 years ago
by
Marc Feger
Browse files
Options
Downloads
Patches
Plain Diff
Add A1
parent
d2f5ca43
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
A1/A1.pdf
+0
-0
0 additions, 0 deletions
A1/A1.pdf
A2/images/astronauts/Eileen-Collins.jpg
+0
-0
0 additions, 0 deletions
A2/images/astronauts/Eileen-Collins.jpg
foo.py
+0
-80
0 additions, 80 deletions
foo.py
test.py
+0
-162
0 additions, 162 deletions
test.py
with
0 additions
and
242 deletions
A1/A1.pdf
0 → 100644
+
0
−
0
View file @
51843207
File added
This diff is collapsed.
Click to expand it.
A2/images/astronauts/Eileen-Collins.jpg
0 → 100644
+
0
−
0
View file @
51843207
46.4 KiB
This diff is collapsed.
Click to expand it.
foo.py
deleted
100644 → 0
+
0
−
80
View file @
d2f5ca43
# Program to count islands in boolean 2D matrix
import
copy
import
string
import
numpy
as
np
class
Graph
:
def
__init__
(
self
,
row
,
col
,
g
):
self
.
ROW
=
row
self
.
COL
=
col
self
.
graph
=
g
self
.
islands
=
[[
None
for
_
in
range
(
col
)]
for
_
in
range
(
row
)]
def
isSafe
(
self
,
i
,
j
,
visited
,
g
):
return
0
<=
i
<
self
.
ROW
and
0
<=
j
<
self
.
COL
and
not
visited
[
i
][
j
]
and
g
[
i
][
j
]
def
DFS
(
self
,
i
,
j
,
visited
,
g
):
rowNbr
=
[
-
1
,
-
1
,
-
1
,
0
,
0
,
1
,
1
,
1
]
colNbr
=
[
-
1
,
0
,
1
,
-
1
,
1
,
-
1
,
0
,
1
]
visited
[
i
][
j
]
=
True
for
k
in
range
(
8
):
if
self
.
isSafe
(
i
+
rowNbr
[
k
],
j
+
colNbr
[
k
],
visited
,
g
):
self
.
DFS
(
i
+
rowNbr
[
k
],
j
+
colNbr
[
k
],
visited
,
g
)
def
countIslands
(
self
):
visited
=
[[
False
for
_
in
range
(
self
.
COL
)]
for
_
in
range
(
self
.
ROW
)]
count
=
0
for
numb
in
[
1
,
2
,
3
]:
g
=
self
.
highlight
(
numb
,
self
.
graph
,
self
.
ROW
,
self
.
COL
)
print
(
np
.
array
(
g
))
for
i
in
range
(
self
.
ROW
):
for
j
in
range
(
self
.
COL
):
if
visited
[
i
][
j
]
is
False
and
g
[
i
][
j
]
==
1
:
self
.
DFS
(
i
,
j
,
visited
,
g
)
for
x
in
range
(
self
.
ROW
):
for
y
in
range
(
self
.
COL
):
if
self
.
islands
[
x
][
y
]
is
None
and
visited
[
x
][
y
]
is
True
:
self
.
islands
[
x
][
y
]
=
(
list
(
string
.
ascii_lowercase
)[
count
],
numb
)
count
+=
1
print
(
np
.
array
(
self
.
islands
))
return
count
def
highlight
(
self
,
number
,
g
,
r
,
c
):
adjacency_matrix
=
copy
.
deepcopy
(
g
)
for
i
in
range
(
r
):
for
j
in
range
(
c
):
adjacency_matrix
[
i
][
j
]
=
1
if
adjacency_matrix
[
i
][
j
]
==
number
else
0
return
adjacency_matrix
def
bins
(
self
):
bins
=
{}
for
x
in
range
(
self
.
ROW
):
for
y
in
range
(
self
.
COL
):
if
self
.
islands
[
x
][
y
]
not
in
bins
:
bins
[
self
.
islands
[
x
][
y
]]
=
1
else
:
bins
[
self
.
islands
[
x
][
y
]]
+=
1
return
bins
if
__name__
==
'
__main__
'
:
graph
=
[[
2
,
1
,
2
,
2
,
1
,
1
],
[
2
,
2
,
1
,
2
,
1
,
1
],
[
2
,
1
,
3
,
2
,
1
,
1
],
[
2
,
2
,
2
,
1
,
1
,
2
],
[
2
,
2
,
1
,
1
,
2
,
2
],
[
2
,
2
,
1
,
1
,
2
,
2
]]
row
=
len
(
graph
)
col
=
len
(
graph
[
0
])
g
=
Graph
(
row
,
col
,
graph
)
print
(
"
Number of islands is:
"
)
g
.
countIslands
()
print
(
g
.
bins
())
This diff is collapsed.
Click to expand it.
test.py
deleted
100644 → 0
+
0
−
162
View file @
d2f5ca43
# Program to count islands in boolean 2D matrix
import
copy
import
string
import
sys
import
numpy
as
np
from
enum
import
Enum
from
skimage
import
io
from
matplotlib
import
pyplot
as
plt
class
KEYS
(
Enum
):
LABEL
=
'
label
'
COLOR
=
'
color
'
COUNT
=
'
count
'
ALPHA
=
'
alpha
'
BETA
=
'
beta
'
PIXELS
=
'
pixels
'
class
Graph
:
def
__init__
(
self
,
row
,
col
,
g
,
bins
,
threshold
):
self
.
ROW
=
row
self
.
COL
=
col
self
.
graph
=
g
self
.
connected_components
=
[[
None
for
_
in
range
(
col
)]
for
_
in
range
(
row
)]
self
.
bins
=
bins
self
.
threshold
=
threshold
def
isSafe
(
self
,
i
,
j
,
visited
,
g
):
return
0
<=
i
<
self
.
ROW
and
0
<=
j
<
self
.
COL
and
not
visited
[
i
][
j
]
and
g
[
i
][
j
]
def
DFS
(
self
,
i
,
j
,
visited
,
g
):
rowNbr
=
[
-
1
,
-
1
,
-
1
,
0
,
0
,
1
,
1
,
1
]
colNbr
=
[
-
1
,
0
,
1
,
-
1
,
1
,
-
1
,
0
,
1
]
visited
[
i
][
j
]
=
True
for
k
in
range
(
8
):
if
self
.
isSafe
(
i
+
rowNbr
[
k
],
j
+
colNbr
[
k
],
visited
,
g
):
self
.
DFS
(
i
+
rowNbr
[
k
],
j
+
colNbr
[
k
],
visited
,
g
)
def
label_connected_components
(
self
,
visited
,
label
,
count
):
for
x
in
range
(
self
.
ROW
):
for
y
in
range
(
self
.
COL
):
if
self
.
connected_components
[
x
][
y
]
is
None
and
visited
[
x
][
y
]
is
True
:
self
.
connected_components
[
x
][
y
]
=
{
KEYS
.
LABEL
.
name
:
count
,
# list(string.ascii_lowercase)[count],
KEYS
.
COLOR
.
name
:
label
}
def
detect_connected_components
(
self
):
visited
=
[[
False
for
_
in
range
(
self
.
COL
)]
for
_
in
range
(
self
.
ROW
)]
count
=
0
for
numb
in
range
(
1
,
self
.
bins
+
1
):
g
=
self
.
binary_matrix
(
numb
,
self
.
graph
,
self
.
ROW
,
self
.
COL
)
for
i
in
range
(
self
.
ROW
):
for
j
in
range
(
self
.
COL
):
if
visited
[
i
][
j
]
is
False
and
g
[
i
][
j
]
==
1
:
self
.
DFS
(
i
,
j
,
visited
,
g
)
self
.
label_connected_components
(
visited
,
numb
,
count
)
count
+=
1
print
(
np
.
array
(
visited
))
return
count
def
binary_matrix
(
self
,
number
,
g
,
r
,
c
):
adjacency_matrix
=
copy
.
deepcopy
(
g
)
for
i
in
range
(
r
):
for
j
in
range
(
c
):
adjacency_matrix
[
i
][
j
]
=
1
if
adjacency_matrix
[
i
][
j
]
==
number
else
0
return
adjacency_matrix
def
ccv
(
self
):
results
=
{}
for
x
in
range
(
self
.
ROW
):
for
y
in
range
(
self
.
COL
):
label
=
self
.
connected_components
[
x
][
y
][
KEYS
.
LABEL
.
name
]
color
=
self
.
connected_components
[
x
][
y
][
KEYS
.
COLOR
.
name
]
if
label
not
in
results
:
results
[
label
]
=
{
KEYS
.
COUNT
.
name
:
1
,
KEYS
.
COLOR
.
name
:
color
,
KEYS
.
PIXELS
.
name
:
[(
x
,
y
)]
}
else
:
results
[
label
][
KEYS
.
COUNT
.
name
]
+=
1
results
[
label
][
KEYS
.
PIXELS
.
name
]
+=
[(
x
,
y
)]
vector
=
{}
for
i
in
range
(
1
,
self
.
bins
+
1
):
vector
[
i
]
=
{
KEYS
.
ALPHA
.
name
:
0
,
KEYS
.
BETA
.
name
:
0
}
for
result
in
results
:
color
=
results
[
result
][
KEYS
.
COLOR
.
name
]
if
results
[
result
][
KEYS
.
COUNT
.
name
]
>=
self
.
threshold
:
vector
[
color
][
KEYS
.
ALPHA
.
name
]
+=
results
[
result
][
KEYS
.
COUNT
.
name
]
else
:
vector
[
color
][
KEYS
.
BETA
.
name
]
+=
results
[
result
][
KEYS
.
COUNT
.
name
]
return
results
,
vector
def
rgb2gray
(
rgb
):
return
np
.
dot
(
rgb
[...,
:
3
],
[
0.2989
,
0.5870
,
0.1140
])
def
visualize
(
img
,
results
,
threshold
,
alpha
):
rows
=
len
(
img
)
columns
=
len
(
img
[
0
,
0
:])
above
=
copy
.
deepcopy
(
img
)
# above[i, j, :] = [0, 255, 0]
coherent_pixels
=
[]
for
result
in
results
:
if
results
[
result
][
KEYS
.
COUNT
.
name
]
>=
threshold
:
coherent_pixels
+=
results
[
result
][
KEYS
.
PIXELS
.
name
]
print
(
len
(
coherent_pixels
))
for
x
,
y
in
coherent_pixels
:
above
[
x
,
y
,
:]
=
[
0
,
255
,
0
]
plt
.
imshow
(
img
)
plt
.
imshow
(
above
,
interpolation
=
'
none
'
,
alpha
=
alpha
)
plt
.
show
()
if
__name__
==
'
__main__
'
:
sys
.
setrecursionlimit
(
2000000000
)
print
(
sys
.
getrecursionlimit
())
graph
=
np
.
array
([[
2
,
1
,
2
,
2
,
1
,
1
],
[
2
,
2
,
1
,
2
,
1
,
1
],
[
2
,
1
,
3
,
2
,
1
,
1
],
[
2
,
2
,
2
,
1
,
1
,
2
],
[
2
,
2
,
1
,
1
,
2
,
2
],
[
2
,
2
,
1
,
1
,
2
,
2
]])
b
=
3
t
=
50
a
=
0.5
img
=
np
.
array
(
io
.
imread
(
'
./A2/images/astronauts/Eileen-Collins-2.jpg
'
))
# [:100, :100]
gray
=
np
.
array
(
np
.
round
(
np
.
divide
(
rgb2gray
(
img
),
256
/
(
b
-
1
))).
astype
(
np
.
int
))
gray
=
np
.
array
(
np
.
add
(
gray
,
1
))
print
(
type
(
gray
))
print
(
np
.
array
(
gray
))
row
=
len
(
gray
)
col
=
len
(
gray
[
0
])
g
=
Graph
(
row
,
col
,
gray
,
b
,
t
)
print
(
"
Connected components:
"
)
g
.
detect_connected_components
()
# print(g.connected_components)
res
,
vec
=
g
.
ccv
()
print
(
res
)
print
(
vec
)
visualize
(
img
,
res
,
t
,
a
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment