Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/machinevisiontoolbox/ImageProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,9 @@ def blend(
gamma=gamma,
dtype=cvdepth,
)
return self.__class__(out, colororder=self.colororder)
# keep the type OpenCV gave: Image(out) alone would narrow float64 to
# float32, and a uint16 image that fits to uint8
return self.__class__(out, dtype=out.dtype, colororder=self.colororder)

def choose(self, image2: Any, mask: Any) -> Self:
r"""
Expand Down
22 changes: 16 additions & 6 deletions tests/test_image_processing_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,26 @@ def test_single_precision_output(self, dtype):
b = Image(np.full((3, 3), 200.0, dtype=np.float32))
assert a.blend(b, 0.5, dtype=dtype).dtype == np.float32

@pytest.mark.xfail(
strict=True,
reason="blend() builds the result with Image(out), which narrows float64 "
"to float32, so dtype='double' and float64 inputs lose their precision",
)
@pytest.mark.parametrize("dtype", ["double", "float64"])
def test_double_precision_output(self, dtype):
a = Image(np.full((3, 3), 100.0), dtype="float64")
b = Image(np.full((3, 3), 200.0), dtype="float64")
assert a.blend(b, 0.5, dtype=dtype).dtype == np.float64
out = a.blend(b, 0.5, dtype=dtype)
assert out.dtype == np.float64
assert (out.array == 150).all()

def test_double_precision_inputs_keep_their_precision(self):
a = Image(np.full((3, 3), 100.0), dtype="float64")
b = Image(np.full((3, 3), 200.0), dtype="float64")
assert a.blend(b, 0.5).dtype == np.float64

def test_uint16_images_stay_uint16(self):
# small values fit in 8 bits, but the result is still the input type
a = Image(np.full((3, 3), 100, dtype=np.uint16), dtype=np.uint16)
b = Image(np.full((3, 3), 200, dtype=np.uint16), dtype=np.uint16)
out = a.blend(b, 0.5)
assert out.dtype == np.uint16
assert (out.array == 150).all()

def test_bad_output_type(self):
a = Image(np.zeros((3, 3), dtype=np.float32))
Expand Down
Loading