Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pt2d
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
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
QIM
Tools
pt2d
Commits
b50c1a85
Commit
b50c1a85
authored
3 months ago
by
Christian
Browse files
Options
Downloads
Patches
Plain Diff
Cleaned up in functions
parent
409c3002
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
live_wire.py
+17
-80
17 additions, 80 deletions
live_wire.py
with
17 additions
and
80 deletions
live_wire.py
+
17
−
80
View file @
b50c1a85
import
time
import
cv2
import
cv2
import
numpy
as
np
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
...
@@ -8,62 +7,40 @@ from skimage.feature import canny
...
@@ -8,62 +7,40 @@ from skimage.feature import canny
from
skimage.graph
import
route_through_array
from
skimage.graph
import
route_through_array
from
scipy.signal
import
convolve2d
from
scipy.signal
import
convolve2d
'''
### Disk live wire cost image
### Canny Edge cost image
def compute_cost_image(path, sigma=3):
### Load image
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
# Apply histogram equalization
image_contrasted = exposure.equalize_adapthist(image, clip_limit=0.01)
# Apply smoothing
def
compute_disk_size
(
user_radius
,
upscale_factor
=
1.2
):
smoothed_img = gaussian(image_contrasted, sigma=sigma
)
return
int
(
np
.
ceil
(
upscale_factor
*
2
*
user_radius
+
1
)
//
2
*
2
+
1
)
# Apply Canny edge detection
canny_img = canny(smoothed_img)
# Create cost image
def
load_image
(
path
):
cost_img = 1.0 / (canny_img + 1e-5) # Invert edges: higher cost where edges are stronger
return
cv2
.
imread
(
path
,
cv2
.
IMREAD_GRAYSCALE
)
return cost_img
def
preprocess_image
(
image
,
sigma
=
3
,
clip_limit
=
0.01
):
# Apply histogram equalization
image_contrasted
=
exposure
.
equalize_adapthist
(
image
,
clip_limit
=
clip_limit
)
def find_path(cost_image, points):
# Apply smoothing
smoothed_img
=
gaussian
(
image_contrasted
,
sigma
=
sigma
)
if len(points) != 2:
return
smoothed_img
raise ValueError(
"
Points should be a list of 2 points: seed and target.
"
)
seed_rc, target_rc = points
path_rc, cost = route_through_array(
cost_image,
start=seed_rc,
end=target_rc,
fully_connected=True
)
return path_rc
def
compute_cost_image
(
path
,
user_radius
,
sigma
=
3
,
clip_limit
=
0.01
):
'''
### Disk live wire cost image
disk_size
=
compute_disk_size
(
user_radius
)
def
compute_cost_image
(
path
,
sigma
=
3
,
disk_size
=
15
):
### Load image
### Load image
image
=
cv2
.
imread
(
path
,
cv2
.
IMREAD_GRAYSCALE
)
image
=
load_image
(
path
)
# Apply histogram equalization
image_contrasted
=
exposure
.
equalize_adapthist
(
image
,
clip_limit
=
0.01
)
# Apply smoothing
# Apply smoothing
smoothed_img
=
gaussian
(
image_contrasted
,
sigma
=
sigma
)
smoothed_img
=
preprocess_image
(
image
,
sigma
=
sigma
,
clip_limit
=
clip_limit
)
# Apply Canny edge detection
# Apply Canny edge detection
canny_img
=
canny
(
smoothed_img
)
canny_img
=
canny
(
smoothed_img
)
# Do disk thing
# Do disk thing
binary_img
=
canny_img
binary_img
=
canny_img
k_size
=
17
kernel
=
circle_edge_kernel
(
k_size
=
disk_size
)
kernel
=
circle_edge_kernel
(
k_size
=
disk_size
)
convolved
=
convolve2d
(
binary_img
,
kernel
,
mode
=
'
same
'
,
boundary
=
'
fill
'
)
convolved
=
convolve2d
(
binary_img
,
kernel
,
mode
=
'
same
'
,
boundary
=
'
fill
'
)
...
@@ -128,12 +105,7 @@ def circle_edge_kernel(k_size=5, radius=None):
...
@@ -128,12 +105,7 @@ def circle_edge_kernel(k_size=5, radius=None):
return
kernel
return
kernel
# Other functions (to be implemented?)
# Other functions
def
downscale
(
img
,
points
,
scale_percent
):
def
downscale
(
img
,
points
,
scale_percent
):
"""
"""
Downsample `img` to `scale_percent` size and scale the given points accordingly.
Downsample `img` to `scale_percent` size and scale the given points accordingly.
...
@@ -160,39 +132,4 @@ def downscale(img, points, scale_percent):
...
@@ -160,39 +132,4 @@ def downscale(img, points, scale_percent):
scaled_seed_xy
=
(
int
(
seed_xy
[
0
]
*
scale_x
),
int
(
seed_xy
[
1
]
*
scale_y
))
scaled_seed_xy
=
(
int
(
seed_xy
[
0
]
*
scale_x
),
int
(
seed_xy
[
1
]
*
scale_y
))
scaled_target_xy
=
(
int
(
target_xy
[
0
]
*
scale_x
),
int
(
target_xy
[
1
]
*
scale_y
))
scaled_target_xy
=
(
int
(
target_xy
[
0
]
*
scale_x
),
int
(
target_xy
[
1
]
*
scale_y
))
return
downsampled_img
,
(
scaled_seed_xy
,
scaled_target_xy
)
return
downsampled_img
,
(
scaled_seed_xy
,
scaled_target_xy
)
\ No newline at end of file
def
compute_cost
(
image
,
sigma
=
3.0
,
epsilon
=
1e-5
):
"""
Smooth the image, run Canny edge detection, then invert the edge map into a cost image.
"""
# Apply histogram equalization
image_contrasted
=
exposure
.
equalize_adapthist
(
image
,
clip_limit
=
0.01
)
# Apply smoothing
smoothed_img
=
gaussian
(
image_contrasted
,
sigma
=
sigma
)
# Apply Canny edge detection
canny_img
=
canny
(
smoothed_img
)
# Create cost image
cost_img
=
1.0
/
(
canny_img
+
epsilon
)
# Invert edges: higher cost where edges are stronger
return
cost_img
,
canny_img
def
backtrack_pixels_on_image
(
img_color
,
path_coords
,
bgr_color
=
(
0
,
0
,
255
)):
"""
Color the path on the (already converted BGR) image in the specified color.
`path_coords` should be a list of (row, col) or (y, x).
"""
for
(
row
,
col
)
in
path_coords
:
img_color
[
row
,
col
]
=
bgr_color
return
img_color
def
export_path
(
path_coords
,
path_name
):
"""
Export the path to a np array.
"""
np
.
save
(
path_name
,
path_coords
)
return
None
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