Demo about effect of the different stiching kernels¶
[ ]:
%matplotlib inline
import matplotlib.pyplot as plt
[ ]:
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
[ ]:
from nabu.stitching.overlap import OverlapStitchingStrategy, ZStichOverlapKernel
plot the two raw data side by side¶
[ ]:
fig,ax = plt.subplots(2)
ax[0].imshow(raw_data_1)
ax[1].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)
[ ]:
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¶
[ ]:
plot_strategy_result(
overlap_strategy="mean",
img_1=raw_data_1,
img_2=raw_data_2,
)
closest strategy¶
[ ]:
plot_strategy_result(
overlap_strategy="closest",
img_1=raw_data_1,
img_2=raw_data_2,
)
linear weights¶
[ ]:
plot_strategy_result(
overlap_strategy="linear weights",
img_1=raw_data_1,
img_2=raw_data_2,
)
cosinus weights¶
[ ]:
plot_strategy_result(
overlap_strategy="cosinus weights",
img_1=raw_data_1,
img_2=raw_data_2,
)
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
[ ]:
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,
# )
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
[ ]:
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,
)
[ ]: