Demo about effect of the different stiching kernels¶
In [1]:
Copied!
%matplotlib inline
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.pyplot as plt
In [2]:
Copied!
import scipy.misc
import numpy
from nabu.testutils import get_data
raw_data_1 = get_data('brain_phantom.npz')['data']
raw_data_2 = numpy.random.rand(*raw_data_1.shape) *255.0
import scipy.misc
import numpy
from nabu.testutils import get_data
raw_data_1 = get_data('brain_phantom.npz')['data']
raw_data_2 = numpy.random.rand(*raw_data_1.shape) *255.0
/tmp/ipykernel_3269051/1871216464.py:1: DeprecationWarning: scipy.misc is deprecated and will be removed in 2.0.0 import scipy.misc
In [3]:
Copied!
from nabu.stitching.overlap import OverlapStitchingStrategy, ZStichOverlapKernel
from nabu.stitching.overlap import OverlapStitchingStrategy, ZStichOverlapKernel
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) Cell In[3], line 1 ----> 1 from nabu.stitching.overlap import OverlapStitchingStrategy, ZStichOverlapKernel ImportError: cannot import name 'ZStichOverlapKernel' from 'nabu.stitching.overlap' (/home/pierre/.venv/py311/lib/python3.11/site-packages/nabu/stitching/overlap.py)
plot the two raw data side by side¶
In [4]:
Copied!
fig,ax = plt.subplots(2)
ax[0].imshow(raw_data_1)
ax[1].imshow(raw_data_2)
fig,ax = plt.subplots(2)
ax[0].imshow(raw_data_1)
ax[1].imshow(raw_data_2)
Out[4]:
<matplotlib.image.AxesImage at 0x7efc66727850>
In [5]:
Copied!
fig = plt.figure()
plt.title("raw data")
plt.subplot(121)
plt.imshow(raw_data_1)
plt.subplot(122)
plt.imshow(raw_data_2)
fig = plt.figure()
plt.title("raw data")
plt.subplot(121)
plt.imshow(raw_data_1)
plt.subplot(122)
plt.imshow(raw_data_2)
Out[5]:
<matplotlib.image.AxesImage at 0x7efc663c4b10>
In [6]:
Copied!
def plot_strategy_result(overlap_strategy, img_1, img_2, with_weights=True, extra_params=None):
strategy = OverlapStitchingStrategy.from_value(overlap_strategy)
kernel = ZStichOverlapKernel(
frame_width=img_1.shape[1],
stitching_strategy=strategy,
overlap_size=img_1.shape[0],
extra_params=extra_params,
)
stitch = kernel.stitch(img_1, img_2)[0]
if with_weights:
w_1 = kernel.weights_img_1
w_2 = kernel.weights_img_2
else:
w_1 = w_2 = None
fig = plt.figure(constrained_layout=True)
if with_weights:
axes = fig.subplot_mosaic(
[
["00", "01"],
["10", "11"],
["2", "2"],
],
)
else:
axes = fig.subplot_mosaic(
[
["2", "2"],
],
)
if w_1 is not None and w_2 is not None:
plt.title(f"{strategy.value} strategy")
# plot weights curves
axes["00"].set_title("image 1 weigths")
axes["00"].plot(numpy.arange(len(w_1)), w_1, color="red")
axes["01"].set_title("image 2 weigths")
axes["01"].plot(numpy.arange(len(w_2)), w_2, color="blue")
# plot weighted images
axes["10"].set_title("image 1 with weights applied")
axes["10"].imshow(img_1*w_1)
axes["11"].set_title("image 2 with weights applied")
axes["11"].imshow(img_2*w_2)
# plot stitched images
axes["2"].set_title("stitched image")
axes["2"].imshow(stitch)
def plot_strategy_result(overlap_strategy, img_1, img_2, with_weights=True, extra_params=None):
strategy = OverlapStitchingStrategy.from_value(overlap_strategy)
kernel = ZStichOverlapKernel(
frame_width=img_1.shape[1],
stitching_strategy=strategy,
overlap_size=img_1.shape[0],
extra_params=extra_params,
)
stitch = kernel.stitch(img_1, img_2)[0]
if with_weights:
w_1 = kernel.weights_img_1
w_2 = kernel.weights_img_2
else:
w_1 = w_2 = None
fig = plt.figure(constrained_layout=True)
if with_weights:
axes = fig.subplot_mosaic(
[
["00", "01"],
["10", "11"],
["2", "2"],
],
)
else:
axes = fig.subplot_mosaic(
[
["2", "2"],
],
)
if w_1 is not None and w_2 is not None:
plt.title(f"{strategy.value} strategy")
# plot weights curves
axes["00"].set_title("image 1 weigths")
axes["00"].plot(numpy.arange(len(w_1)), w_1, color="red")
axes["01"].set_title("image 2 weigths")
axes["01"].plot(numpy.arange(len(w_2)), w_2, color="blue")
# plot weighted images
axes["10"].set_title("image 1 with weights applied")
axes["10"].imshow(img_1*w_1)
axes["11"].set_title("image 2 with weights applied")
axes["11"].imshow(img_2*w_2)
# plot stitched images
axes["2"].set_title("stitched image")
axes["2"].imshow(stitch)
mean strategy¶
In [7]:
Copied!
plot_strategy_result(
overlap_strategy="mean",
img_1=raw_data_1,
img_2=raw_data_2,
)
plot_strategy_result(
overlap_strategy="mean",
img_1=raw_data_1,
img_2=raw_data_2,
)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[7], line 1 ----> 1 plot_strategy_result( 2 overlap_strategy="mean", 3 img_1=raw_data_1, 4 img_2=raw_data_2, 5 ) Cell In[6], line 2, in plot_strategy_result(overlap_strategy, img_1, img_2, with_weights, extra_params) 1 def plot_strategy_result(overlap_strategy, img_1, img_2, with_weights=True, extra_params=None): ----> 2 strategy = OverlapStitchingStrategy.from_value(overlap_strategy) 3 kernel = ZStichOverlapKernel( 4 frame_width=img_1.shape[1], 5 stitching_strategy=strategy, 6 overlap_size=img_1.shape[0], 7 extra_params=extra_params, 8 ) 9 stitch = kernel.stitch(img_1, img_2)[0] File /usr/lib/python3.11/enum.py:789, in EnumType.__getattr__(cls, name) 787 return cls._member_map_[name] 788 except KeyError: --> 789 raise AttributeError(name) from None AttributeError: from_value
closest strategy¶
In [8]:
Copied!
plot_strategy_result(
overlap_strategy="closest",
img_1=raw_data_1,
img_2=raw_data_2,
)
plot_strategy_result(
overlap_strategy="closest",
img_1=raw_data_1,
img_2=raw_data_2,
)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[8], line 1 ----> 1 plot_strategy_result( 2 overlap_strategy="closest", 3 img_1=raw_data_1, 4 img_2=raw_data_2, 5 ) Cell In[6], line 2, in plot_strategy_result(overlap_strategy, img_1, img_2, with_weights, extra_params) 1 def plot_strategy_result(overlap_strategy, img_1, img_2, with_weights=True, extra_params=None): ----> 2 strategy = OverlapStitchingStrategy.from_value(overlap_strategy) 3 kernel = ZStichOverlapKernel( 4 frame_width=img_1.shape[1], 5 stitching_strategy=strategy, 6 overlap_size=img_1.shape[0], 7 extra_params=extra_params, 8 ) 9 stitch = kernel.stitch(img_1, img_2)[0] File /usr/lib/python3.11/enum.py:789, in EnumType.__getattr__(cls, name) 787 return cls._member_map_[name] 788 except KeyError: --> 789 raise AttributeError(name) from None AttributeError: from_value
linear weights¶
In [9]:
Copied!
plot_strategy_result(
overlap_strategy="linear weights",
img_1=raw_data_1,
img_2=raw_data_2,
)
plot_strategy_result(
overlap_strategy="linear weights",
img_1=raw_data_1,
img_2=raw_data_2,
)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[9], line 1 ----> 1 plot_strategy_result( 2 overlap_strategy="linear weights", 3 img_1=raw_data_1, 4 img_2=raw_data_2, 5 ) Cell In[6], line 2, in plot_strategy_result(overlap_strategy, img_1, img_2, with_weights, extra_params) 1 def plot_strategy_result(overlap_strategy, img_1, img_2, with_weights=True, extra_params=None): ----> 2 strategy = OverlapStitchingStrategy.from_value(overlap_strategy) 3 kernel = ZStichOverlapKernel( 4 frame_width=img_1.shape[1], 5 stitching_strategy=strategy, 6 overlap_size=img_1.shape[0], 7 extra_params=extra_params, 8 ) 9 stitch = kernel.stitch(img_1, img_2)[0] File /usr/lib/python3.11/enum.py:789, in EnumType.__getattr__(cls, name) 787 return cls._member_map_[name] 788 except KeyError: --> 789 raise AttributeError(name) from None AttributeError: from_value
cosinus weights¶
In [10]:
Copied!
plot_strategy_result(
overlap_strategy="cosinus weights",
img_1=raw_data_1,
img_2=raw_data_2,
)
plot_strategy_result(
overlap_strategy="cosinus weights",
img_1=raw_data_1,
img_2=raw_data_2,
)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[10], line 1 ----> 1 plot_strategy_result( 2 overlap_strategy="cosinus weights", 3 img_1=raw_data_1, 4 img_2=raw_data_2, 5 ) Cell In[6], line 2, in plot_strategy_result(overlap_strategy, img_1, img_2, with_weights, extra_params) 1 def plot_strategy_result(overlap_strategy, img_1, img_2, with_weights=True, extra_params=None): ----> 2 strategy = OverlapStitchingStrategy.from_value(overlap_strategy) 3 kernel = ZStichOverlapKernel( 4 frame_width=img_1.shape[1], 5 stitching_strategy=strategy, 6 overlap_size=img_1.shape[0], 7 extra_params=extra_params, 8 ) 9 stitch = kernel.stitch(img_1, img_2)[0] File /usr/lib/python3.11/enum.py:789, in EnumType.__getattr__(cls, name) 787 return cls._member_map_[name] 788 except KeyError: --> 789 raise AttributeError(name) from None AttributeError: from_value
image minimum divergence¶
This method will not applied a simple predetermined weights on the two images.
It split the two images into two parts: high frequency and low frequency.
The two low frequency parts will be stitched using a 'sinusoidal' / cosinus weights approach. When the two high frequency part will be stitched by taking the lower divergent pixels
In [11]:
Copied!
plot_strategy_result(
overlap_strategy="image minimum divergence",
img_1=raw_data_1,
img_2=raw_data_2,
with_weights=False,
extra_params={
"frequency_threshold": 5,
},
)
# uncomment to compare vs 'raw' sinusoidal
# plot_strategy_result(
# overlap_strategy="cosinus weights",
# img_1=raw_data_1,
# img_2=raw_data_2,
# with_weights=False,
# )
plot_strategy_result(
overlap_strategy="image minimum divergence",
img_1=raw_data_1,
img_2=raw_data_2,
with_weights=False,
extra_params={
"frequency_threshold": 5,
},
)
# uncomment to compare vs 'raw' sinusoidal
# plot_strategy_result(
# overlap_strategy="cosinus weights",
# img_1=raw_data_1,
# img_2=raw_data_2,
# with_weights=False,
# )
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[11], line 1 ----> 1 plot_strategy_result( 2 overlap_strategy="image minimum divergence", 3 img_1=raw_data_1, 4 img_2=raw_data_2, 5 with_weights=False, 6 extra_params={ 7 "frequency_threshold": 5, 8 }, 9 ) 10 # uncomment to compare vs 'raw' sinusoidal 11 # plot_strategy_result( 12 # overlap_strategy="cosinus weights", (...) 15 # with_weights=False, 16 # ) Cell In[6], line 2, in plot_strategy_result(overlap_strategy, img_1, img_2, with_weights, extra_params) 1 def plot_strategy_result(overlap_strategy, img_1, img_2, with_weights=True, extra_params=None): ----> 2 strategy = OverlapStitchingStrategy.from_value(overlap_strategy) 3 kernel = ZStichOverlapKernel( 4 frame_width=img_1.shape[1], 5 stitching_strategy=strategy, 6 overlap_size=img_1.shape[0], 7 extra_params=extra_params, 8 ) 9 stitch = kernel.stitch(img_1, img_2)[0] File /usr/lib/python3.11/enum.py:789, in EnumType.__getattr__(cls, name) 787 return cls._member_map_[name] 788 except KeyError: --> 789 raise AttributeError(name) from None AttributeError: from_value
higher signal¶
This method will not applied predetermine weights on the two images.
It will pick pixel on the image having the higher signal.
A use case is that if there is some artefacts on images which creates stripes (from scintillator artefacts for example) it could be removed from this method
In [12]:
Copied!
raw_data = get_data("brain_phantom.npz")["data"]
new_dataset_1 = raw_data_1.copy()
new_dataset_1[40:75] = 0.0
new_dataset_1[:, 210:245] = 0.0
new_dataset_2 = raw_data_1.copy()
new_dataset_2[:, 100:120] = 0.0
fig = plt.figure()
plt.title("raw data")
plt.subplot(121)
plt.imshow(new_dataset_1)
plt.subplot(122)
plt.imshow(new_dataset_2)
plot_strategy_result(
overlap_strategy="higher signal",
img_1=new_dataset_1,
img_2=new_dataset_2,
with_weights=False,
)
raw_data = get_data("brain_phantom.npz")["data"]
new_dataset_1 = raw_data_1.copy()
new_dataset_1[40:75] = 0.0
new_dataset_1[:, 210:245] = 0.0
new_dataset_2 = raw_data_1.copy()
new_dataset_2[:, 100:120] = 0.0
fig = plt.figure()
plt.title("raw data")
plt.subplot(121)
plt.imshow(new_dataset_1)
plt.subplot(122)
plt.imshow(new_dataset_2)
plot_strategy_result(
overlap_strategy="higher signal",
img_1=new_dataset_1,
img_2=new_dataset_2,
with_weights=False,
)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[12], line 16 13 plt.subplot(122) 14 plt.imshow(new_dataset_2) ---> 16 plot_strategy_result( 17 overlap_strategy="higher signal", 18 img_1=new_dataset_1, 19 img_2=new_dataset_2, 20 with_weights=False, 21 ) Cell In[6], line 2, in plot_strategy_result(overlap_strategy, img_1, img_2, with_weights, extra_params) 1 def plot_strategy_result(overlap_strategy, img_1, img_2, with_weights=True, extra_params=None): ----> 2 strategy = OverlapStitchingStrategy.from_value(overlap_strategy) 3 kernel = ZStichOverlapKernel( 4 frame_width=img_1.shape[1], 5 stitching_strategy=strategy, 6 overlap_size=img_1.shape[0], 7 extra_params=extra_params, 8 ) 9 stitch = kernel.stitch(img_1, img_2)[0] File /usr/lib/python3.11/enum.py:789, in EnumType.__getattr__(cls, name) 787 return cls._member_map_[name] 788 except KeyError: --> 789 raise AttributeError(name) from None AttributeError: from_value
In [ ]:
Copied!