Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
python3-imagej-tiff
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Elphel
python3-imagej-tiff
Commits
b88b2585
Commit
b88b2585
authored
Dec 21, 2021
by
Andrey Filippov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated for lwir16
parent
d786da7b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
423 additions
and
433 deletions
+423
-433
imagej_tiff.py
imagej_tiff.py
+423
-433
No files found.
imagej_tiff.py
View file @
b88b2585
...
@@ -58,21 +58,21 @@ class bcolors:
...
@@ -58,21 +58,21 @@ class bcolors:
# reshape to tiles
# reshape to tiles
def
get_tile_images
(
image
,
width
=
8
,
height
=
8
):
def
get_tile_images
(
image
,
width
=
8
,
height
=
8
):
_nrows
,
_ncols
,
depth
=
image
.
shape
_nrows
,
_ncols
,
depth
=
image
.
shape
_size
=
image
.
size
_size
=
image
.
size
_strides
=
image
.
strides
_strides
=
image
.
strides
nrows
,
_m
=
divmod
(
_nrows
,
height
)
nrows
,
_m
=
divmod
(
_nrows
,
height
)
ncols
,
_n
=
divmod
(
_ncols
,
width
)
ncols
,
_n
=
divmod
(
_ncols
,
width
)
if
_m
!=
0
or
_n
!=
0
:
if
_m
!=
0
or
_n
!=
0
:
return
None
return
None
return
np
.
lib
.
stride_tricks
.
as_strided
(
return
np
.
lib
.
stride_tricks
.
as_strided
(
np
.
ravel
(
image
),
np
.
ravel
(
image
),
shape
=
(
nrows
,
ncols
,
height
,
width
,
depth
),
shape
=
(
nrows
,
ncols
,
height
,
width
,
depth
),
strides
=
(
height
*
_strides
[
0
],
width
*
_strides
[
1
],
*
_strides
),
strides
=
(
height
*
_strides
[
0
],
width
*
_strides
[
1
],
*
_strides
),
writeable
=
False
writeable
=
False
)
)
# TiffFile has no len exception
# TiffFile has no len exception
#import imageio
#import imageio
...
@@ -80,7 +80,7 @@ def get_tile_images(image, width=8, height=8):
...
@@ -80,7 +80,7 @@ def get_tile_images(image, width=8, height=8):
#from libtiff import TIFF
#from libtiff import TIFF
'''
'''
Description:
Description:
Reads
a tiff
files with multiple layers that were saved by imagej
Reads
TIFF
files with multiple layers that were saved by imagej
Methods:
Methods:
.getstack(items=[])
.getstack(items=[])
returns np.array, layers are stacked along depth - think of RGB channels
returns np.array, layers are stacked along depth - think of RGB channels
...
@@ -95,459 +95,449 @@ Examples:
...
@@ -95,459 +95,449 @@ Examples:
'''
'''
class
imagej_tiff
:
class
imagej_tiff
:
# imagej stores labels lengths in this tag
# imagej stores labels lengths in this tag
__TIFF_TAG_LABELS_LENGTHS
=
50838
__TIFF_TAG_LABELS_LENGTHS
=
50838
# imagej stores labels conents in this tag
# imagej stores labels conents in this tag
__TIFF_TAG_LABELS_STRINGS
=
50839
__TIFF_TAG_LABELS_STRINGS
=
50839
# init
def
__init__
(
self
,
filename
,
layers
=
None
,
tile_list
=
None
):
# init
# file name
def
__init__
(
self
,
filename
,
layers
=
None
,
tile_list
=
None
):
self
.
fname
=
filename
# file name
tif
=
Image
.
open
(
filename
)
self
.
fname
=
filename
# total number of layers in tiff
tif
=
Image
.
open
(
filename
)
self
.
nimages
=
tif
.
n_frames
# total number of layers in tiff
# labels array
self
.
nimages
=
tif
.
n_frames
self
.
labels
=
[]
# labels array
# infos will contain xml data Elphel stores in some of tiff files
self
.
labels
=
[]
self
.
infos
=
[]
# infos will contain xml data Elphel stores in some of tiff files
# dictionary from decoded infos[0] xml data
self
.
infos
=
[]
self
.
props
=
{}
# dictionary from decoded infos[0] xml data
self
.
props
=
{}
# bits per sample, type int
self
.
bpp
=
tif
.
tag
[
258
][
0
]
self
.
__split_labels
(
tif
.
n_frames
,
tif
.
tag
)
self
.
__parse_info
()
try
:
self
.
nan_bug
=
self
.
props
[
'VERSION'
]
==
'1.0'
# data between min and max is mapped to 0..254 instead of 1.255
except
:
self
.
nan_bug
=
False
# other files, not ML ones
# image layers stacked along depth - (think RGB)
self
.
image
=
[]
if
layers
is
None
:
# fill self.image
for
i
in
range
(
self
.
nimages
):
tif
.
seek
(
i
)
a
=
np
.
array
(
tif
)
a
=
np
.
reshape
(
a
,(
a
.
shape
[
0
],
a
.
shape
[
1
],
1
))
#a = a[:,:,np.newaxis]
# bits per sample, type int
self
.
bpp
=
tif
.
tag
[
258
][
0
]
# scale for 8-bits
self
.
__split_labels
(
tif
.
n_frames
,
tif
.
tag
)
# exclude layer named 'other'
self
.
__parse_info
()
if
self
.
bpp
==
8
:
try
:
_min
=
self
.
data_min
self
.
nan_bug
=
self
.
props
[
'VERSION'
]
==
'1.0'
# data between min and max is mapped to 0..254 instead of 1.255
_max
=
self
.
data_max
except
:
_MIN
=
1
self
.
nan_bug
=
False
# other files, not ML ones
_MAX
=
255
# image layers stacked along depth - (think RGB)
if
(
self
.
nan_bug
):
self
.
image
=
[]
_MIN
=
0
_MAX
=
254
if
layers
is
None
:
else
:
# fill self.image
if
self
.
labels
[
i
]
!=
'other'
:
for
i
in
range
(
self
.
nimages
):
a
[
a
==
0
]
=
np
.
nan
tif
.
seek
(
i
)
a
=
a
.
astype
(
float
)
if
self
.
labels
[
i
]
!=
'other'
:
# a[a==0]=np.nan
a
=
(
_max
-
_min
)
*
(
a
-
_MIN
)
/
(
_MAX
-
_MIN
)
+
_min
# init
if
i
==
0
:
self
.
image
=
a
# stack along depth (think of RGB channels)
else
:
self
.
image
=
np
.
append
(
self
.
image
,
a
,
axis
=
2
)
else
:
if
tile_list
is
None
:
indx
=
0
for
layer
in
layers
:
tif
.
seek
(
self
.
labels
.
index
(
layer
))
a
=
np
.
array
(
tif
)
a
=
np
.
array
(
tif
)
if
not
indx
:
a
=
np
.
reshape
(
a
,(
a
.
shape
[
0
],
a
.
shape
[
1
],
1
))
self
.
image
=
np
.
empty
((
a
.
shape
[
0
],
a
.
shape
[
1
],
len
(
layers
)),
a
.
dtype
)
#a = a[:,:,np.newaxis]
self
.
image
[
...
,
indx
]
=
a
# exclude layer named 'other'
indx
+=
1
if
self
.
bpp
==
8
:
_min
=
self
.
data_min
_max
=
self
.
data_max
_MIN
=
1
_MAX
=
255
if
(
self
.
nan_bug
):
_MIN
=
0
_MAX
=
254
else
:
if
self
.
labels
[
i
]
!=
'other'
:
a
[
a
==
0
]
=
np
.
nan
a
=
a
.
astype
(
float
)
if
self
.
labels
[
i
]
!=
'other'
:
a
=
(
_max
-
_min
)
*
(
a
-
_MIN
)
/
(
_MAX
-
_MIN
)
+
_min
if
i
==
0
:
self
.
image
=
a
# stack along depth (think of RGB channels)
else
:
self
.
image
=
np
.
append
(
self
.
image
,
a
,
axis
=
2
)
else
:
else
:
other_label
=
"other"
if
tile_list
is
None
:
# print(tile_list)
indx
=
0
num_tiles
=
len
(
tile_list
)
for
layer
in
layers
:
num_layers
=
len
(
layers
)
tif
.
seek
(
self
.
labels
.
index
(
layer
))
tiles_corr
=
np
.
empty
((
num_tiles
,
num_layers
,
self
.
tileH
*
self
.
tileW
),
dtype
=
float
)
a
=
np
.
array
(
tif
)
# tiles_other=np.empty((num_tiles,3),dtype=float)
if
not
indx
:
tiles_other
=
self
.
gettilesvalues
(
self
.
image
=
np
.
empty
((
a
.
shape
[
0
],
a
.
shape
[
1
],
len
(
layers
)),
a
.
dtype
)
tif
=
tif
,
self
.
image
[
...
,
indx
]
=
a
tile_list
=
tile_list
,
indx
+=
1
label
=
other_label
)
else
:
for
nl
,
label
in
enumerate
(
layers
):
other_label
=
"other"
tif
.
seek
(
self
.
labels
.
index
(
label
))
# print(tile_list)
layer
=
np
.
array
(
tif
)
# 8 or 32 bits
num_tiles
=
len
(
tile_list
)
tilesX
=
layer
.
shape
[
1
]
//
self
.
tileW
num_layers
=
len
(
layers
)
for
nt
,
tl
in
enumerate
(
tile_list
):
tiles_corr
=
np
.
empty
((
num_tiles
,
num_layers
,
self
.
tileH
*
self
.
tileW
),
dtype
=
float
)
ty
=
tl
//
tilesX
# tiles_other=np.empty((num_tiles,3),dtype=float)
tx
=
tl
%
tilesX
tiles_other
=
self
.
gettilesvalues
(
# tiles_corr[nt,nl] = np.ravel(layer[self.tileH*ty:self.tileH*(ty+1),self.tileW*tx:self.tileW*(tx+1)])
tif
=
tif
,
a
=
np
.
ravel
(
layer
[
self
.
tileH
*
ty
:
self
.
tileH
*
(
ty
+
1
),
self
.
tileW
*
tx
:
self
.
tileW
*
(
tx
+
1
)])
tile_list
=
tile_list
,
#convert from int8
label
=
other_label
)
if
self
.
bpp
==
8
:
for
nl
,
label
in
enumerate
(
layers
):
a
=
a
.
astype
(
float
)
tif
.
seek
(
self
.
labels
.
index
(
label
))
if
np
.
isnan
(
tiles_other
[
nt
][
0
]):
layer
=
np
.
array
(
tif
)
# 8 or 32 bits
# print("Skipping NaN tile ",tl)
tilesX
=
layer
.
shape
[
1
]
//
self
.
tileW
a
[
...
]
=
np
.
nan
for
nt
,
tl
in
enumerate
(
tile_list
):
else
:
ty
=
tl
//
tilesX
_min
=
self
.
data_min
tx
=
tl
%
tilesX
_max
=
self
.
data_max
# tiles_corr[nt,nl] = np.ravel(layer[self.tileH*ty:self.tileH*(ty+1),self.tileW*tx:self.tileW*(tx+1)])
_MIN
=
1
a
=
np
.
ravel
(
layer
[
self
.
tileH
*
ty
:
self
.
tileH
*
(
ty
+
1
),
self
.
tileW
*
tx
:
self
.
tileW
*
(
tx
+
1
)])
_MAX
=
255
#convert from int8
if
(
self
.
nan_bug
):
if
self
.
bpp
==
8
:
_MIN
=
0
a
=
a
.
astype
(
float
)
_MAX
=
254
if
np
.
isnan
(
tiles_other
[
nt
][
0
]):
else
:
# print("Skipping NaN tile ",tl)
a
[
a
==
0
]
=
np
.
nan
a
[
...
]
=
np
.
nan
a
=
(
_max
-
_min
)
*
(
a
-
_MIN
)
/
(
_MAX
-
_MIN
)
+
_min
else
:
tiles_corr
[
nt
,
nl
]
=
a
_min
=
self
.
data_min
_max
=
self
.
data_max
_MIN
=
1
_MAX
=
255
if
(
self
.
nan_bug
):
_MIN
=
0
_MAX
=
254
else
:
a
[
a
==
0
]
=
np
.
nan
a
=
(
_max
-
_min
)
*
(
a
-
_MIN
)
/
(
_MAX
-
_MIN
)
+
_min
tiles_corr
[
nt
,
nl
]
=
a
pass
pass
pass
self
.
corr2d
=
tiles_corr
self
.
target_disparity
=
tiles_other
[
...
,
0
]
self
.
gt_ds
=
tiles_other
[
...
,
1
:
3
]
pass
pass
self
.
corr2d
=
tiles_corr
# init done, close the image
self
.
target_disparity
=
tiles_other
[
...
,
0
]
if
(
self
.
props
[
'VERSION'
]
==
2.0
):
self
.
gt_ds
=
tiles_other
[
...
,
1
:
3
]
# self.tileH = self.image.shape[0]//self.props['tileStepY']
# self.tileW = self.image.shape[1]//self.props['tileStepX']
self
.
tileH
=
self
.
props
[
'tileStepY'
]
self
.
tileW
=
self
.
props
[
'tileStepX'
]
pass
pass
# init done, close the image
tif
.
close
()
tif
.
close
()
# label == tiff layer name
def
getvalues
(
self
,
label
=
""
):
# label == tiff layer name
l
=
self
.
getstack
([
label
],
shape_as_tiles
=
True
)
def
getvalues
(
self
,
label
=
""
):
res
=
np
.
empty
((
l
.
shape
[
0
],
l
.
shape
[
1
],
3
))
l
=
self
.
getstack
([
label
],
shape_as_tiles
=
True
)
res
=
np
.
empty
((
l
.
shape
[
0
],
l
.
shape
[
1
],
3
))
for
i
in
range
(
res
.
shape
[
0
]):
for
j
in
range
(
res
.
shape
[
1
]):
for
i
in
range
(
res
.
shape
[
0
]):
# 9x9 -> 81x1
for
j
in
range
(
res
.
shape
[
1
]):
m
=
np
.
ravel
(
l
[
i
,
j
])
# 9x9 -> 81x1
if
self
.
bpp
==
32
:
m
=
np
.
ravel
(
l
[
i
,
j
])
res
[
i
,
j
,
0
]
=
m
[
0
]
if
self
.
bpp
==
32
:
res
[
i
,
j
,
1
]
=
m
[
2
]
res
[
i
,
j
,
0
]
=
m
[
0
]
res
[
i
,
j
,
2
]
=
m
[
4
]
res
[
i
,
j
,
1
]
=
m
[
2
]
elif
self
.
bpp
==
8
:
res
[
i
,
j
,
2
]
=
m
[
4
]
res
[
i
,
j
,
0
]
=
((
m
[
0
]
-
128
)
*
256
+
m
[
1
])
/
128
elif
self
.
bpp
==
8
:
res
[
i
,
j
,
1
]
=
((
m
[
2
]
-
128
)
*
256
+
m
[
3
])
/
128
res
[
i
,
j
,
0
]
=
((
m
[
0
]
-
128
)
*
256
+
m
[
1
])
/
128
res
[
i
,
j
,
2
]
=
(
m
[
4
]
*
256
+
m
[
5
])
/
65536.0
res
[
i
,
j
,
1
]
=
((
m
[
2
]
-
128
)
*
256
+
m
[
3
])
/
128
else
:
res
[
i
,
j
,
2
]
=
(
m
[
4
]
*
256
+
m
[
5
])
/
65536.0
res
[
i
,
j
,
0
]
=
np
.
nan
else
:
res
[
i
,
j
,
1
]
=
np
.
nan
res
[
i
,
j
,
0
]
=
np
.
nan
res
[
i
,
j
,
2
]
=
np
.
nan
res
[
i
,
j
,
1
]
=
np
.
nan
res
[
i
,
j
,
2
]
=
np
.
nan
# NaNize
a
=
res
[:,:,
0
]
# NaNize
a
[
a
==-
256
]
=
np
.
nan
a
=
res
[:,:,
0
]
b
=
res
[:,:,
1
]
a
[
a
==-
256
]
=
np
.
nan
b
[
b
==-
256
]
=
np
.
nan
b
=
res
[:,:,
1
]
c
=
res
[:,:,
2
]
b
[
b
==-
256
]
=
np
.
nan
c
[
c
==
0
]
=
np
.
nan
c
=
res
[:,:,
2
]
return
res
c
[
c
==
0
]
=
np
.
nan
return
res
# 3 values per tile: target disparity, GT disparity, GT confidence
def
gettilesvalues
(
self
,
# 3 values per tile: target disparity, GT disparity, GT confidence
def
gettilesvalues
(
self
,
tif
,
tif
,
tile_list
,
tile_list
,
label
=
""
):
label
=
""
):
res
=
np
.
empty
((
len
(
tile_list
),
3
),
dtype
=
float
)
res
=
np
.
empty
((
len
(
tile_list
),
3
),
dtype
=
float
)
tif
.
seek
(
self
.
labels
.
index
(
label
))
tif
.
seek
(
self
.
labels
.
index
(
label
))
layer
=
np
.
array
(
tif
)
# 8 or 32 bits
layer
=
np
.
array
(
tif
)
# 8 or 32 bits
tilesX
=
layer
.
shape
[
1
]
//
self
.
tileW
tilesX
=
layer
.
shape
[
1
]
//
self
.
tileW
for
i
,
tl
in
enumerate
(
tile_list
):
for
i
,
tl
in
enumerate
(
tile_list
):
ty
=
tl
//
tilesX
ty
=
tl
//
tilesX
tx
=
tl
%
tilesX
tx
=
tl
%
tilesX
m
=
np
.
ravel
(
layer
[
self
.
tileH
*
ty
:
self
.
tileH
*
(
ty
+
1
),
self
.
tileW
*
tx
:
self
.
tileW
*
(
tx
+
1
)])
m
=
np
.
ravel
(
layer
[
self
.
tileH
*
ty
:
self
.
tileH
*
(
ty
+
1
),
self
.
tileW
*
tx
:
self
.
tileW
*
(
tx
+
1
)])
if
self
.
bpp
==
32
:
if
self
.
bpp
==
32
:
res
[
i
,
0
]
=
m
[
0
]
res
[
i
,
0
]
=
m
[
0
]
res
[
i
,
1
]
=
m
[
2
]
res
[
i
,
1
]
=
m
[
2
]
res
[
i
,
2
]
=
m
[
4
]
res
[
i
,
2
]
=
m
[
4
]
elif
self
.
bpp
==
8
:
elif
self
.
bpp
==
8
:
res
[
i
,
0
]
=
((
m
[
0
]
-
128
)
*
256
+
m
[
1
])
/
128
res
[
i
,
0
]
=
((
m
[
0
]
-
128
)
*
256
+
m
[
1
])
/
128
res
[
i
,
1
]
=
((
m
[
2
]
-
128
)
*
256
+
m
[
3
])
/
128
res
[
i
,
1
]
=
((
m
[
2
]
-
128
)
*
256
+
m
[
3
])
/
128
res
[
i
,
2
]
=
(
m
[
4
]
*
256
+
m
[
5
])
/
65536.0
res
[
i
,
2
]
=
(
m
[
4
]
*
256
+
m
[
5
])
/
65536.0
else
:
res
[
i
,
0
]
=
np
.
nan
res
[
i
,
1
]
=
np
.
nan
res
[
i
,
2
]
=
np
.
nan
# NaNize
a
=
res
[
...
,
0
]
a
[
a
==-
256
]
=
np
.
nan
b
=
res
[
...
,
1
]
b
[
b
==-
256
]
=
np
.
nan
c
=
res
[
...
,
2
]
c
[
c
==
0
]
=
np
.
nan
return
res
# get ordered stack of images by provided items
# by index or label name. Divides into [self.tileH][self.tileW] tiles
def
getstack
(
self
,
items
=
[],
shape_as_tiles
=
False
):
a
=
()
if
len
(
items
)
==
0
:
b
=
self
.
image
else
:
else
:
res
[
i
,
0
]
=
np
.
nan
for
i
in
items
:
res
[
i
,
1
]
=
np
.
nan
if
type
(
i
)
==
int
:
res
[
i
,
2
]
=
np
.
nan
a
+=
(
self
.
image
[:,:,
i
],)
# NaNize
elif
type
(
i
)
==
str
:
a
=
res
[
...
,
0
]
j
=
self
.
labels
.
index
(
i
)
a
[
a
==-
256
]
=
np
.
nan
a
+=
(
self
.
image
[:,:,
j
],)
b
=
res
[
...
,
1
]
# stack along depth
b
[
b
==-
256
]
=
np
.
nan
b
=
np
.
stack
(
a
,
axis
=
2
)
c
=
res
[
...
,
2
]
if
shape_as_tiles
:
c
[
c
==
0
]
=
np
.
nan
b
=
get_tile_images
(
b
,
self
.
tileW
,
self
.
tileH
)
return
res
return
b
def
trimStack
(
self
,
stack
,
radius
=
0
):
if
(
radius
==
0
):
radius
=
self
.
props
[
'corrRadius'
]
# get ordered stack of images by provided items
corr_side
=
2
*
radius
+
1
# by index or label name
return
stack
[:,:,:,:
corr_side
,:
corr_side
]
def
getstack
(
self
,
items
=
[],
shape_as_tiles
=
False
):
# get np.array of a channel
a
=
()
# * do not handle out of bounds
if
len
(
items
)
==
0
:
def
channel
(
self
,
index
):
b
=
self
.
image
return
self
.
image
[:,:,
index
]
else
:
for
i
in
items
:
def
getCorrsMeta
(
self
,
items
=
[]):
if
type
(
i
)
==
int
:
stack0
=
self
.
getstack
(
items
,
shape_as_tiles
=
True
)
a
+=
(
self
.
image
[:,:,
i
],)
stack
=
np
.
moveaxis
(
stack0
,
4
,
0
)
# slices - first index
elif
type
(
i
)
==
str
:
radius
=
self
.
props
[
'corrRadius'
]
j
=
self
.
labels
.
index
(
i
)
num_meta
=
self
.
props
[
'numMeta'
]
a
+=
(
self
.
image
[:,:,
j
],)
corr_side
=
2
*
radius
+
1
# stack along depth
corr_tiles
=
stack
[:,:,:,:
corr_side
,:
corr_side
]
b
=
np
.
stack
(
a
,
axis
=
2
)
meta
=
stack
[:,:,:,
-
1
,:
num_meta
]
return
corr_tiles
,
meta
/
self
.
props
[
'tileMetaScale'
]
if
shape_as_tiles
:
b
=
get_tile_images
(
b
,
self
.
tileW
,
self
.
tileH
)
# display images by index or label
def
show_images
(
self
,
items
=
[]):
return
b
# show listed only
# get np.array of a channel
if
len
(
items
)
>
0
:
# * do not handle out of bounds
for
i
in
items
:
def
channel
(
self
,
index
):
if
type
(
i
)
==
int
:
return
self
.
image
[:,:,
index
]
self
.
show_image
(
i
)
elif
type
(
i
)
==
str
:
j
=
self
.
labels
.
index
(
i
)
# display images by index or label
self
.
show_image
(
j
)
def
show_images
(
self
,
items
=
[]):
# show all
else
:
# show listed only
for
i
in
range
(
self
.
nimages
):
if
len
(
items
)
>
0
:
self
.
show_image
(
i
)
for
i
in
items
:
if
type
(
i
)
==
int
:
self
.
show_image
(
i
)
elif
type
(
i
)
==
str
:
j
=
self
.
labels
.
index
(
i
)
self
.
show_image
(
j
)
# show all
else
:
for
i
in
range
(
self
.
nimages
):
self
.
show_image
(
i
)
# display single image
def
show_image
(
self
,
index
):
# display using matplotlib
t
=
self
.
image
[:,:,
index
]
mytitle
=
"("
+
str
(
index
+
1
)
+
" of "
+
str
(
self
.
nimages
)
+
") "
+
self
.
labels
[
index
]
fig
=
plt
.
figure
()
fig
.
canvas
.
set_window_title
(
self
.
fname
+
": "
+
mytitle
)
fig
.
suptitle
(
mytitle
)
#plt.imshow(t,cmap=plt.get_cmap('gray'))
plt
.
imshow
(
t
)
plt
.
colorbar
()
# display using Pillow - need to scale
# remove NaNs - no need
#t[np.isnan(t)]=np.nanmin(t)
# scale to [min/max*255:255] range
#t = (1-(t-np.nanmax(t))/(t-np.nanmin(t)))*255
#tmp_im = Image.fromarray(t)
#tmp_im.show()
# puts etrees in infoss
def
__parse_info
(
self
):
infos
=
[]
for
info
in
self
.
infos
:
infos
.
append
(
ET
.
fromstring
(
info
))
self
.
infos
=
infos
#
specifics
#
display single image
# properties dictionary
def
show_image
(
self
,
index
):
pd
=
{}
# display using matplotlib
if
infos
:
t
=
self
.
image
[:,:,
index
]
for
child
in
infos
[
0
]:
mytitle
=
"("
+
str
(
index
+
1
)
+
" of "
+
str
(
self
.
nimages
)
+
") "
+
self
.
labels
[
index
]
#print(child.tag+"::::::"+child.text)
fig
=
plt
.
figure
()
pd
[
child
.
tag
]
=
child
.
text
fig
.
canvas
.
set_window_title
(
self
.
fname
+
": "
+
mytitle
)
fig
.
suptitle
(
mytitle
)
#plt.imshow(t,cmap=plt.get_cmap('gray'))
plt
.
imshow
(
t
)
plt
.
colorbar
()
self
.
props
=
pd
# display using Pillow - need to scale
# tiles are squares
# remove NaNs - no need
self
.
tileW
=
int
(
self
.
props
[
'tileWidth'
])
#t[np.isnan(t)]=np.nanmin(t)
self
.
tileH
=
int
(
self
.
props
[
'tileWidth'
])
# scale to [min/max*255:255] range
self
.
data_min
=
float
(
self
.
props
[
'data_min'
])
#t = (1-(t-np.nanmax(t))/(t-np.nanmin(t)))*255
self
.
data_max
=
float
(
self
.
props
[
'data_max'
])
#tmp_im = Image.fromarray(t)
#tmp_im.show()
# makes arrays of labels (strings) and unparsed xml infos
def
__split_labels
(
self
,
n
,
tag
):
# puts etrees in infoss
def
__parse_info
(
self
):
# list
tag_lens
=
tag
[
self
.
__TIFF_TAG_LABELS_LENGTHS
]
infos
=
[]
# string
for
info
in
self
.
infos
:
tag_labels
=
tag
[
self
.
__TIFF_TAG_LABELS_STRINGS
]
.
decode
()
infos
.
append
(
ET
.
fromstring
(
info
))
# remove 1st element: it's something like IJIJlabl..
self
.
infos
=
infos
tag_labels
=
tag_labels
[
tag_lens
[
0
]:]
tag_lens
=
tag_lens
[
1
:]
# specifics
# properties dictionary
# the last ones are images labels
pd
=
{}
# normally the difference is expected to be 0 or 1
skip
=
len
(
tag_lens
)
-
n
if
infos
:
for
child
in
infos
[
0
]:
self
.
labels
=
[]
#print(child.tag+"::::::"+child.text)
self
.
infos
=
[]
pd
[
child
.
tag
]
=
child
.
text
for
l
in
tag_lens
:
string
=
tag_labels
[
0
:
l
]
.
replace
(
'
\x00
'
,
''
)
self
.
props
=
pd
if
skip
==
0
:
file_version
=
float
(
self
.
props
[
'VERSION'
])
self
.
labels
.
append
(
string
)
if
(
file_version
<
2.0
):
else
:
# tiles are squares (older version
self
.
infos
.
append
(
string
)
self
.
tileW
=
int
(
self
.
props
[
'tileWidth'
])
skip
-=
1
self
.
tileH
=
int
(
self
.
props
[
'tileWidth'
])
tag_labels
=
tag_labels
[
l
:]
self
.
data_min
=
float
(
self
.
props
[
'data_min'
])
self
.
data_max
=
float
(
self
.
props
[
'data_max'
])
else
:
floats
=
[
'dispOffsetLow'
,
'tileMetaScale'
,
'disparity_low'
,
'dispOffset'
,
'fatZero'
,
'disparity_pwr'
,
'VERSION'
,
'dispOffsetHigh'
,
'disparity_high'
]
ints
=
[
'metaGTConfidence'
,
'tileMetaSlice'
,
'indexReference'
,
'metaLastDiff'
,
'metaGTDisparity'
,
'metaFracValid'
,
'numScenes'
,
'metaTargetDisparity'
,
'disparity_steps'
,
'tileStepX'
,
'tileStepY'
,
"corrRadius"
,
"numMeta"
]
bools
=
[
'randomize_offsets'
]
for
key
in
pd
:
val
=
pd
[
key
]
if
key
in
bools
:
if
(
val
==
'1'
)
or
(
val
==
'true'
)
or
(
val
==
'True'
):
pd
[
key
]
=
1
else
:
pd
[
key
]
=
0
pass
elif
key
in
ints
:
pd
[
key
]
=
int
(
pd
[
key
])
elif
key
in
floats
:
pd
[
key
]
=
float
(
pd
[
key
])
try
:
pd
[
'corrRadius'
]
=
pd
[
'corrRadius'
]
# not yet exists
except
:
pd
[
'corrRadius'
]
=
7
try
:
pd
[
'numMeta'
]
=
pd
[
'numMeta'
]
# not yet exists
except
:
pd
[
'numMeta'
]
=
6
pass
# makes arrays of labels (strings) and unparsed xml infos
def
__split_labels
(
self
,
n
,
tag
):
# list
tag_lens
=
tag
[
self
.
__TIFF_TAG_LABELS_LENGTHS
]
# string
tag_labels
=
tag
[
self
.
__TIFF_TAG_LABELS_STRINGS
]
.
decode
()
# remove 1st element: it's something like IJIJlabl..
tag_labels
=
tag_labels
[
tag_lens
[
0
]:]
tag_lens
=
tag_lens
[
1
:]
# the last ones are images labels
# normally the difference is expected to be 0 or 1
skip
=
len
(
tag_lens
)
-
n
self
.
labels
=
[]
self
.
infos
=
[]
for
l
in
tag_lens
:
string
=
tag_labels
[
0
:
l
]
.
replace
(
'
\x00
'
,
''
)
if
skip
==
0
:
self
.
labels
.
append
(
string
)
else
:
self
.
infos
.
append
(
string
)
skip
-=
1
tag_labels
=
tag_labels
[
l
:]
#MAIN
#MAIN
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
try
:
fname
=
sys
.
argv
[
1
]
except
IndexError
:
#fname = "/mnt/dde6f983-d149-435e-b4a2-88749245cc6c/home/eyesis/x3d_data/data_sets/train/1527182807_896892/v02/ml/1527182807_896892-ML_DATA-08B-O-FZ0.05-OFFS0.40000.tiff"
fname
=
"/home/elphel/lwir16-proc/proc1/models/1626032208_613623/v01/ml32/1626032208_613623-ML-AUX-RND-DOFFS-5.000.tiff"
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
try
:
ijt
=
imagej_tiff
(
fname
)
fname
=
sys
.
argv
[
1
]
except
IndexError
:
fname
=
"/mnt/dde6f983-d149-435e-b4a2-88749245cc6c/home/eyesis/x3d_data/data_sets/train/1527182807_896892/v02/ml/1527182807_896892-ML_DATA-08B-O-FZ0.05-OFFS0.40000.tiff"
# fname = "1521849031_093189-ML_DATA-32B-O-OFFS1.0.tiff"
# fname = "1521849031_093189-ML_DATA-08B-O-OFFS1.0.tiff"
#fname = "1521849031_093189-DISP_MAP-D0.0-46.tif"
#fname = "1526905735_662795-ML_DATA-08B-AIOTD-OFFS2.0.tiff"
#fname = "test.tiff"
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
ijt
=
imagej_tiff
(
fname
)
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
print
(
"TIFF stack labels: "
+
str
(
ijt
.
labels
))
#print(ijt.infos)
rough_string
=
ET
.
tostring
(
ijt
.
infos
[
0
],
"utf-8"
)
reparsed
=
minidom
.
parseString
(
rough_string
)
print
(
reparsed
.
toprettyxml
(
indent
=
"
\t
"
))
#print(ijt.props)
# needed properties:
print
(
"Tiles shape: "
+
str
(
ijt
.
tileW
)
+
"x"
+
str
(
ijt
.
tileH
))
print
(
"Data min: "
+
str
(
ijt
.
data_min
))
print
(
"Data max: "
+
str
(
ijt
.
data_max
))
print
(
ijt
.
image
.
shape
)
# layer order: ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs', 'other']
# now split this into tiles:
#tiles = get_tile_images(ijt.image,ijt.tileW,ijt.tileH)
#print(tiles.shape)
tiles
=
ijt
.
getstack
([
'diagm-pair'
,
'diago-pair'
,
'hor-pairs'
,
'vert-pairs'
],
shape_as_tiles
=
True
)
print
(
"Stack of images shape: "
+
str
(
tiles
.
shape
))
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
# provide layer name
values
=
ijt
.
getvalues
(
label
=
'other'
)
print
(
"Stack of values shape: "
+
str
(
values
.
shape
))
# each tile's disparity:
fig
=
plt
.
figure
()
fig
.
suptitle
(
"Estimated Disparity"
)
plt
.
imshow
(
values
[:,:,
0
])
plt
.
colorbar
()
fig
=
plt
.
figure
()
fig
.
suptitle
(
"Esitmated+Residual disparity"
)
plt
.
imshow
(
values
[:,:,
1
])
plt
.
colorbar
()
fig
=
plt
.
figure
()
fig
.
suptitle
(
"Residual disparity confidence"
)
plt
.
imshow
(
values
[:,:,
2
])
plt
.
colorbar
()
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
#print(values)
#print(value_tiles[131,162].flatten())
#print(np.ravel(value_tiles[131,162]))
#values = np.empty((vt.shape[0],vt.shape[1],3))
#for i in range(values.shape[0]):
# for j in range(values.shape[1]):
# values[i,j,0] = get_v1()
#print(tiles[121,160,:,:,0].shape)
#_nrows = int(ijt.image.shape[0] / ijt.tileH)
#_ncols = int(ijt.image.shape[1] / ijt.tileW)
#_nrows = 32
#_ncols = 32
#print(str(_nrows)+" "+str(_ncols))
#fig, ax = plt.subplots(nrows=_nrows, ncols=_ncols)
#for i in range(_nrows):
# for j in range(_ncols):
# ax[i,j].imshow(tiles[i+100,j,:,:,0])
# ax[i,j].set_axis_off()
#for i in range(5):
# fig = plt.figure()
# plt.imshow(tiles[121,160,:,:,i])
# plt.colorbar()
#ijt.show_images(['other']
)
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
#ijt.show_images([0,3])
print
(
"TIFF stack labels: "
+
str
(
ijt
.
labels
))
#ijt.show_images(['X-corr','Y-corr'])
#print(ijt.infos)
#ijt.show_images(['R-vign',3])
ijt
.
show_images
()
rough_string
=
ET
.
tostring
(
ijt
.
infos
[
0
],
"utf-8"
)
plt
.
show
()
reparsed
=
minidom
.
parseString
(
rough_string
)
print
(
reparsed
.
toprettyxml
(
indent
=
"
\t
"
))
# needed properties:
print
(
"Tiles shape: "
+
str
(
ijt
.
tileW
)
+
"x"
+
str
(
ijt
.
tileH
))
try
:
print
(
"Data min: "
+
str
(
ijt
.
data_min
))
print
(
"Data max: "
+
str
(
ijt
.
data_max
))
except
:
print
(
"Data min/max are not provided"
)
print
(
ijt
.
image
.
shape
)
# tiles,tiles_meta = ijt.getCorrsMeta(['0-1','1-2','2-3','3-4'])
tiles
,
tiles_meta
=
ijt
.
getCorrsMeta
([])
print
(
"Corr stack shape: "
+
str
(
tiles
.
shape
))
print
(
"Meta stack shape: "
+
str
(
tiles_meta
.
shape
))
exit
(
0
)
#
# each tile's disparity:
# provide layer name
values
=
ijt
.
getvalues
(
label
=
'other'
)
print
(
"Stack of values shape: "
+
str
(
values
.
shape
))
fig
=
plt
.
figure
()
fig
.
suptitle
(
"Estimated Disparity"
)
plt
.
imshow
(
values
[:,:,
0
])
plt
.
colorbar
()
# Examples
fig
=
plt
.
figure
()
fig
.
suptitle
(
"Esitmated+Residual disparity"
)
plt
.
imshow
(
values
[:,:,
1
])
plt
.
colorbar
()
# 1: get default stack of images
fig
=
plt
.
figure
()
#a = ijt.getstack()
fig
.
suptitle
(
"Residual disparity confidence"
)
#print(a.shape)
plt
.
imshow
(
values
[:,:,
2
])
plt
.
colorbar
()
# 2: get defined ordered stack of images by tiff image index or by label name
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
#a = ijt.getstack([1,2,'X-corr'])
#print(a.shape)
# 3: will throw an error if there's no such label
ijt
.
show_images
()
#a = ijt.getstack([1,2,'Unknown'])
plt
.
show
()
#print(a.shape)
# 4: will throw an error if index is out of bounds
#a = ijt.getstack([1,2,'X-corr'])
#print(a.shape)
# 5: dev excercise
#a = np.array([[1,2],[3,4]])
#b = np.array([[5,6],[7,8]])
#c = np.array([[10,11],[12,13]])
#print("test1:")
#ka = (a,b,c)
#d = np.stack(ka,axis=2)
#print(d)
#print("test2:")
# Examples
#e = np.stack((d[:,:,1],d[:,:,0]),axis=2)
#print(e)
# 1: get default stack of images
#a = ijt.getstack()
#print(a.shape)
# 2: get defined ordered stack of images by tiff image index or by label name
#a = ijt.getstack([1,2,'X-corr'])
#print(a.shape)
# 3: will throw an error if there's no such label
#a = ijt.getstack([1,2,'Unknown'])
#print(a.shape)
# 4: will throw an error if index is out of bounds
#a = ijt.getstack([1,2,'X-corr'])
#print(a.shape)
# 5: dev excercise
#a = np.array([[1,2],[3,4]])
#b = np.array([[5,6],[7,8]])
#c = np.array([[10,11],[12,13]])
#print("test1:")
#ka = (a,b,c)
#d = np.stack(ka,axis=2)
#print(d)
#print("test2:")
#e = np.stack((d[:,:,1],d[:,:,0]),axis=2)
#print(e)
...
...
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