Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
inovisao
pynovisao
Commits
f0c310ef
Commit
f0c310ef
authored
Oct 24, 2019
by
Fábio Prestes
Browse files
Consertado erro em criacao de XML quando houvesse segmentos nao originados da imagem
parent
fb022b34
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
41 deletions
+61
-41
src/extraction/feature_extraction.py
src/extraction/feature_extraction.py
+30
-13
src/extraction/image_moments.py
src/extraction/image_moments.py
+25
-24
src/pynovisao.py
src/pynovisao.py
+2
-1
src/segmentation/skimage_segmenter.py
src/segmentation/skimage_segmenter.py
+4
-3
No files found.
src/extraction/feature_extraction.py
View file @
f0c310ef
...
...
@@ -24,7 +24,7 @@ import cv2
from
.extractor
import
Extractor
from
tqdm
import
tqdm
import
sys
from
sys
import
version_info
if
not
sys
.
warnoptions
:
import
warnings
warnings
.
simplefilter
(
"ignore"
)
...
...
@@ -256,23 +256,40 @@ class FeatureExtractor(object):
output_file : string
Path to output file.
"""
if
version_info
[
0
]
>=
3
:
arff
=
open
(
output_file
,
'wb'
)
arff
.
write
(
bytes
(
str
(
"%s %s
\n\n
"
%
(
'@relation'
,
relation
)),
'utf-8'
))
for
label
,
t
in
zip
(
labels
,
types
):
arff
.
write
(
bytes
(
str
(
"%s %s %s
\n
"
%
(
'@attribute'
,
label
,
t
)),
'utf-8'
))
arff
.
write
(
bytes
(
str
(
"%s %s {%s}
\n\n
"
%
(
'@attribute'
,
'classe'
,
', '
.
join
(
classes
))),
'utf-8'
))
arff
.
write
(
bytes
(
str
(
'@data
\n\n
'
),
'utf-8'
))
for
instance
in
data
:
instance
=
map
(
str
,
instance
)
line
=
","
.
join
(
instance
)
arff
.
write
(
bytes
(
str
(
line
+
"
\n
"
),
'utf-8'
))
arff
.
close
()
else
:
arff
=
open
(
output_file
,
'wb'
)
arff
=
open
(
output_file
,
'wb'
)
arff
.
write
(
bytes
(
str
(
"%s %s
\n\n
"
%
(
'@relation'
,
relation
)),
'utf-8'
))
arff
.
write
(
"%s %s
\n\n
"
%
(
'@relation'
,
relation
))
for
label
,
t
in
zip
(
labels
,
types
):
arff
.
write
(
bytes
(
str
(
"%s %s %s
\n
"
%
(
'@attribute'
,
label
,
t
))
,
'utf-8'
))
arff
.
write
(
bytes
(
str
(
"%s %s {%s}
\n\n
"
%
(
'@attribute'
,
'classe'
,
', '
.
join
(
classes
)))
,
'utf-8'
))
for
label
,
t
in
zip
(
labels
,
types
):
arff
.
write
(
"%s %s %s
\n
"
%
(
'@attribute'
,
label
,
t
))
arff
.
write
(
"%s %s {%s}
\n\n
"
%
(
'@attribute'
,
'classe'
,
', '
.
join
(
classes
)))
arff
.
write
(
bytes
(
str
(
'@data
\n\n
'
)
,
'utf-8'
))
arff
.
write
(
'@data
\n\n
'
)
for
instance
in
data
:
instance
=
map
(
str
,
instance
)
line
=
","
.
join
(
instance
)
arff
.
write
(
bytes
(
str
(
line
+
"
\n
"
)
,
'utf-8'
))
for
instance
in
data
:
instance
=
map
(
str
,
instance
)
line
=
","
.
join
(
instance
)
arff
.
write
(
line
+
"
\n
"
)
arff
.
close
()
arff
.
close
()
#method to equalize size of images
def
equalize_size_image
(
self
,
image
):
...
...
src/extraction/image_moments.py
View file @
f0c310ef
...
...
@@ -18,6 +18,7 @@ from skimage.measure import regionprops, moments, moments_central
from
skimage.morphology
import
label
import
numpy
as
np
from
.extractor
import
Extractor
import
math
class
RawCentralMoments
(
Extractor
):
"""Calculate raw and central set of image moments."""
...
...
@@ -30,39 +31,42 @@ class RawCentralMoments(Extractor):
def
run
(
self
,
image
):
"""Calculate raw and central set of image moments of order 1 and 2.
Parameters
----------
image : opencv image
I
mage to be analyzed.
i
mage to be analyzed.
Returns
-------
features : tuple
Returns a tuple containing a list of labels, type and values for each feature extracted.
Returns a tuple containing a list of labels, type and values for each feature extracted.
"""
#raw_moments = moments(image)
image_binary
=
ImageUtils
.
image_binary
(
image
,
bgr
=
True
)
m
=
measure
.
moments
(
image_binary
)
m
=
np
.
nan_to_num
(
m
)
values_m
=
[
m
[
p
,
q
]
for
(
p
,
q
)
in
self
.
_moments_order
]
labels_m
=
[
M
+
str
(
p
)
+
str
(
q
)
for
M
,(
p
,
q
)
in
zip
([
'M_'
]
*
len
(
self
.
_moments_order
),
self
.
_moments_order
)]
row
=
m
[
0
,
1
]
/
m
[
0
,
0
]
col
=
m
[
1
,
0
]
/
m
[
0
,
0
]
mu
=
measure
.
moments_central
(
image_binary
,
center
=
(
row
,
col
),
order
=
3
)
mu
=
np
.
nan_to_num
(
mu
)
values_mu
=
[
mu
[
p
,
q
]
for
(
p
,
q
)
in
self
.
_moments_order
]
labels_mu
=
[
M
+
str
(
p
)
+
str
(
q
)
for
M
,(
p
,
q
)
in
zip
([
'Mu_'
]
*
len
(
self
.
_moments_order
),
self
.
_moments_order
)]
#print(values_mu)
labels
=
labels_m
+
labels_mu
types
=
[
Extractor
.
NUMERIC
]
*
len
(
labels
)
values
=
values_m
+
values_mu
return
labels
,
types
,
values
...
...
@@ -75,22 +79,20 @@ class HuMoments(Extractor):
def
run
(
self
,
image
):
"""Calculate Hu's set set of image moments.
Parameters
----------
image : opencv image
Image to be analyzed.
mage to be analyzed.
Returns
-------
features : tuple
Returns a tuple containing a list of labels, type and values for each feature extracted.
Returns a tuple containing a list of labels, type and values for each feature extracted.
"""
"""image_binary = ImageUtils.image_binary(image, bgr = True)
m = measure.moments(image_binary)
row = m[0, 1] / m[0, 0]
col = m[1, 0] / m[0, 0]
...
...
@@ -98,17 +100,16 @@ class HuMoments(Extractor):
nu = measure.moments_normalized(mu)
hu = measure.moments_hu(nu)
values_hu = list(hu)"""
image
=
cv2
.
cvtColor
(
image
,
cv2
.
COLOR_BGR2GRAY
)
values_hu
=
cv2
.
HuMoments
(
cv2
.
moments
(
image
)).
flatten
()
values_hu
=
np
.
nan_to_num
(
values_hu
)
values_hu
=
np
.
nan_to_num
(
values_hu
)
values_hu
=
list
(
values_hu
)
labels_hu
=
[
m
+
n
for
m
,
n
in
zip
([
'Hu_'
]
*
len
(
values_hu
),
map
(
str
,
range
(
0
,
len
(
values_hu
))))]
labels
=
labels_hu
types
=
[
Extractor
.
NUMERIC
]
*
len
(
labels
)
values
=
values_hu
...
...
src/pynovisao.py
View file @
f0c310ef
...
...
@@ -566,7 +566,7 @@ class Act(object):
print
(
"Wait to complete processes all images!"
)
with
tqdm
(
total
=
len
(
list_segments
))
as
pppbar
:
for
idx_segment
in
list_segments
:
segment
,
size_segment
,
idx_segment
=
self
.
segmenter
.
get_segment
(
self
,
idx_segment
=
idx_segment
)[:
-
1
]
segment
,
size_segment
,
xml_file
,
idx_segment
=
self
.
segmenter
.
get_segment
(
self
,
idx_segment
=
idx_segment
)[:
-
1
]
# Problem here! Dataset removed.
filepath
=
File
.
save_only_class_image
(
segment
,
self
.
dataset
,
tmp
,
self
.
_image_name
,
idx_segment
)
len_segments
[
idx_segment
]
=
size_segment
...
...
@@ -1146,6 +1146,7 @@ class Act(object):
return
l
,
c
,
i
,
j
,
igual
c
=
c
+
1
#Segment not found, search the next pixel.
l
=
l
+
1
return
-
1
,
-
1
,
-
1
,
-
1
,
False
def
achaSeg
(
self
,
pathS
,
file
,
pog
,
arquivos
):
"""
...
...
src/segmentation/skimage_segmenter.py
View file @
f0c310ef
...
...
@@ -16,7 +16,7 @@ from skimage.util import img_as_float, img_as_ubyte
from
util.config
import
Config
from
util.utils
import
TimeUtils
from
util.x11_colors
import
X11Colors
from
pascal_voc_writer
import
Writer
as
wr
from
abc
import
ABCMeta
,
abstractmethod
class
SkimageSegmenter
(
object
):
...
...
@@ -117,8 +117,9 @@ class SkimageSegmenter(object):
# Get the rectangle that encompasses the countour
x
,
y
,
w
,
h
=
cv2
.
boundingRect
(
max_contour
)
# Create the object for this segment in the .XML file
xml_file
.
addObject
(
name_segment
,
x
,
y
,
x
+
w
,
y
+
h
)
# Create the object for this segment in the .XML file
if
xml_file
is
not
None
:
xml_file
.
addObject
(
name_segment
,
x
,
y
,
x
+
w
,
y
+
h
)
segment
=
segment
[
y
:
y
+
h
,
x
:
x
+
w
]
end_time
=
TimeUtils
.
get_time
()
...
...
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