Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Ortner, Joshua
ai4hdr_backend
Commits
7840e5a4
Commit
7840e5a4
authored
Jan 20, 2021
by
josh
Browse files
mean shift function implemented
parent
cb7abc06
Changes
11
Expand all
Hide whitespace changes
Inline
Side-by-side
.ipynb_checkpoints/meanShiftSeg-checkpoint.ipynb
0 → 100644
View file @
7840e5a4
This diff is collapsed.
Click to expand it.
README.md
View file @
7840e5a4
...
@@ -14,21 +14,21 @@ Note: if the slice size doesn't fit evenly into the size of
...
@@ -14,21 +14,21 @@ Note: if the slice size doesn't fit evenly into the size of
## Example run
## Example run
```
bash
```
bash
python sliceImage.py ./
example_i
mage
s
/GeoEye_After011310.jpg GeoEye_Slice 512 512 ./
example_i
mage
s
/
python sliceImage.py ./
sliceI
mage
_ex
/GeoEye_After011310.jpg GeoEye_Slice 512 512 ./
sliceI
mage
_ex
/
```
```
## Example run result
## Example run result
## Original Image: 1180x1180
## Original Image: 1180x1180


## Slice 1: 512x512
## Slice 1: 512x512


## Slice 2: 512x512
## Slice 2: 512x512


## Slice 3: 512x512
## Slice 3: 512x512


## Slice 4: 512x512
## Slice 4: 512x512


\ No newline at end of file
\ No newline at end of file
ai4hdr_utils.py
View file @
7840e5a4
import
cv2
import
cv2
import
pathlib
import
pathlib
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
matplotlib.colors
as
mcolors
from
matplotlib.colors
import
to_rgb
from
sklearn.cluster
import
MeanShift
,
estimate_bandwidth
from
skimage
import
color
def
sliceImage
(
imagePath
:
str
,
newFileBase
:
str
,
newSize
:
tuple
,
newPath
:
str
)
->
dict
:
def
meanshiftSegmentation
(
image
):
pass
def
sliceImage
(
imagePath
:
str
,
newFileBase
:
str
,
newSize
:
tuple
,
newPath
:
str
)
->
int
:
'''
'''
Creates slices of image
s given
Creates slices of
an
image
.
imagePath : path to image used for slicing
Parameters:
newFileBase: used to rename each slice
- imagePath : path to image used for slicing
newSize : width and height of new slices
- newFileBase: used to rename each slice
newPath : where to save new slicest
- newSize : width and height of new slices
- newPath : where to save new slicest
returns: dictionary containing the following information
returns: dictionary containing the following information
{
{
...
@@ -25,6 +26,7 @@ def sliceImage( imagePath: str, newFileBase: str, newSize: tuple, newPath: str )
...
@@ -25,6 +26,7 @@ def sliceImage( imagePath: str, newFileBase: str, newSize: tuple, newPath: str )
}
}
OR empty dictionary if slicing was unable to be completed
OR empty dictionary if slicing was unable to be completed
'''
'''
fileExt
=
pathlib
.
Path
(
imagePath
).
name
.
split
(
"."
)[
-
1
]
newPath
=
pathlib
.
Path
(
newPath
)
newPath
=
pathlib
.
Path
(
newPath
)
image
=
cv2
.
imread
(
imagePath
)
image
=
cv2
.
imread
(
imagePath
)
...
@@ -50,15 +52,99 @@ def sliceImage( imagePath: str, newFileBase: str, newSize: tuple, newPath: str )
...
@@ -50,15 +52,99 @@ def sliceImage( imagePath: str, newFileBase: str, newSize: tuple, newPath: str )
# process image
# process image
for
i
in
range
(
0
,
maxWidth
,
newWidth
):
for
i
in
range
(
0
,
maxWidth
,
newWidth
):
for
j
in
range
(
0
,
maxHeight
,
newHeight
):
for
j
in
range
(
0
,
maxHeight
,
newHeight
):
slicePath
=
newPath
.
joinpath
(
"{}-{}-{}.
jpg
"
.
format
(
newFileBase
,
i
,
j
)
)
slicePath
=
newPath
.
joinpath
(
"{}-{}-{}.
{}
"
.
format
(
newFileBase
,
i
,
j
,
fileExt
)
)
newSliceCount
+=
1
newSliceCount
+=
1
newPaths
.
append
(
slicePath
)
newPaths
.
append
(
slicePath
)
cv2
.
imwrite
(
str
(
slicePath
),
cv2
.
imwrite
(
str
(
slicePath
),
image
[
i
:
i
+
newWidth
,
image
[
i
:
i
+
newWidth
,
j
:
j
+
newHeight
,
:]
)
j
:
j
+
newHeight
,
:]
)
return
{
return
{
"pixelsLost"
:
pixelsLost
,
"pixelsLost"
:
pixelsLost
,
"sliceCount"
:
newSliceCount
,
"sliceCount"
:
newSliceCount
,
"slicePaths"
:
newPaths
"slicePaths"
:
newPaths
}
}
\ No newline at end of file
def
meanshiftSegmentation
(
imagePath
:
str
,
outPath
:
str
,
classColors
:
list
=
None
,
quantile
=
0.2
,
samples
=
500
)
->
dict
:
'''
SOURCE: https://www.efavdb.com/mean-shift
Creates and saves a segmented version of image using the mean shift algorithm to determine pixel classes.
Supports 3 channel images, each color channel is used as a feature.
Parameters:
- imagePath : path to image used
- outPath : path to save segmented image ( image type extension automatically added )
- classColors : custom list of colors to use for classes, default: [ "darkgreen", "indigo", "gold" ]
Note: list index = class label
- quantile : used for estimate_bandwidth function, should be between [0, 1], default: 0.2
- samples : used for estimate_bandwidth function, number of samples to use, defualt: 500
Returns: a dictionary mapping the class integer to a tuple containing a color name and rgb value
{
0: ( "darkgreen", ( 0.0, 0.3, 0.0 ) ),
1: ( "indigo", ( 0.2, 0.0, 0.5 ) ),
2: ( "gold", ( 1.0, 0.8, 0.0 ) )
}
Quantile and samples are used for sklearn estimate_bandwidth function.
For more info: https://scikit-learn.org/stable/modules/generated/sklearn.cluster.estimate_bandwidth.html
'''
if
classColors
is
None
:
classColors
=
[
"darkgreen"
,
"indigo"
,
"gold"
]
# create mapping for color name to rgb
color1
=
to_rgb
(
classColors
[
0
]
)
color2
=
to_rgb
(
classColors
[
1
]
)
color3
=
to_rgb
(
classColors
[
2
]
)
# cv2 uses BGR order, flip for RGB
colorToBGR
=
{
classColors
[
0
]:
(
color1
[
2
],
color1
[
1
],
color1
[
0
]
),
classColors
[
1
]:
(
color2
[
2
],
color2
[
1
],
color2
[
0
]
),
classColors
[
2
]:
(
color3
[
2
],
color3
[
1
],
color3
[
0
]
),
}
fileExt
=
pathlib
.
Path
(
imagePath
).
name
.
split
(
"."
)[
-
1
]
# read image and generate feature arrays
img
=
cv2
.
imread
(
imagePath
)
imgArr
=
np
.
array
(
img
)
flatArr
=
np
.
reshape
(
imgArr
,
[
-
1
,
3
]
)
# run mean shift
bandwidth
=
estimate_bandwidth
(
X
=
flatArr
,
quantile
=
quantile
,
n_samples
=
samples
)
meanShift
=
MeanShift
(
bandwidth
,
bin_seeding
=
True
,
n_jobs
=-
1
)
# n_jobs=-1 uses all processors
meanShift
.
fit
(
flatArr
)
# use labels to construct a class array with classes: 0, 1, 2
labels
=
meanShift
.
labels_
classArr
=
np
.
reshape
(
labels
,
[
img
.
shape
[
0
],
img
.
shape
[
1
]]
)
# use classColors to generate a 3 channel image from class array
rgbValues
=
[]
for
i
in
range
(
classArr
.
shape
[
0
]
):
for
j
in
range
(
classArr
.
shape
[
1
]
):
color
=
classColors
[
classArr
[
i
][
j
]]
rgbValues
.
append
(
colorToBGR
[
color
]
)
finalArr
=
np
.
asarray
(
rgbValues
)
finalArr
=
np
.
reshape
(
finalArr
,
imgArr
.
shape
)
# scale values to range [0, 255] to save with opencv
finalArr
*=
255.0
cv2
.
imwrite
(
"{}.{}"
.
format
(
outPath
,
fileExt
),
finalArr
)
return
{
0
:
(
classColors
[
0
],
color1
),
1
:
(
classColors
[
1
],
color2
),
2
:
(
classColors
[
2
],
color3
)
}
if
__name__
==
"__main__"
:
cwd
=
pathlib
.
Path
.
cwd
()
imagePath
=
cwd
.
joinpath
(
"meanShiftSeg_ex/GeoEye_MS_Original.jpg"
)
outPath
=
cwd
.
joinpath
(
"meanShiftSeg_ex/GeoEye_MS_Segmented"
)
result
=
meanshiftSegmentation
(
str
(
imagePath
),
str
(
outPath
)
)
\ No newline at end of file
meanShiftSeg.ipynb
0 → 100644
View file @
7840e5a4
This diff is collapsed.
Click to expand it.
example_images/GeoEye_Slice-0-0
.jpg
→
meanShiftSeg_ex/GeoEye_MS_Original
.jpg
View file @
7840e5a4
File moved
meanShiftSeg_ex/GeoEye_MS_Segmented.jpg
0 → 100644
View file @
7840e5a4
306 KB
example_i
mage
s
/GeoEye_After011310.jpg
→
sliceI
mage
_ex
/GeoEye_After011310.jpg
View file @
7840e5a4
File moved
sliceImage_ex/GeoEye_Slice-0-0.jpg
0 → 100644
View file @
7840e5a4
233 KB
example_i
mage
s
/GeoEye_Slice-0-512.jpg
→
sliceI
mage
_ex
/GeoEye_Slice-0-512.jpg
View file @
7840e5a4
File moved
example_i
mage
s
/GeoEye_Slice-512-0.jpg
→
sliceI
mage
_ex
/GeoEye_Slice-512-0.jpg
View file @
7840e5a4
File moved
example_i
mage
s
/GeoEye_Slice-512-512.jpg
→
sliceI
mage
_ex
/GeoEye_Slice-512-512.jpg
View file @
7840e5a4
File moved
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment