From 152c5d393ffda15c92943b1f53f5266b1f26b08a Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Wed, 7 Sep 2016 17:56:00 -0700 Subject: [PATCH] Fixed bool / monochrome image display, added more unit tests --- pyqtgraph/functions.py | 2 ++ pyqtgraph/graphicsItems/HistogramLUTItem.py | 4 ++-- pyqtgraph/graphicsItems/ImageItem.py | 6 ++++-- pyqtgraph/graphicsItems/tests/test_ImageItem.py | 12 ++++++++++++ pyqtgraph/tests/image_testing.py | 2 +- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index 9199fea7..d79c350f 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -959,6 +959,8 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): elif data.dtype.kind == 'i': s = 2**(data.itemsize*8 - 1) levels = np.array([-s, s-1]) + elif data.dtype.kind == 'b': + levels = np.array([0,1]) else: raise Exception('levels argument is required for float input types') if not isinstance(levels, np.ndarray): diff --git a/pyqtgraph/graphicsItems/HistogramLUTItem.py b/pyqtgraph/graphicsItems/HistogramLUTItem.py index c46dbbbe..31764250 100644 --- a/pyqtgraph/graphicsItems/HistogramLUTItem.py +++ b/pyqtgraph/graphicsItems/HistogramLUTItem.py @@ -179,8 +179,8 @@ class HistogramLUTItem(GraphicsWidget): return self.lut def regionChanged(self): - #if self.imageItem is not None: - #self.imageItem.setLevels(self.region.getRegion()) + if self.imageItem() is not None: + self.imageItem().setLevels(self.region.getRegion()) self.sigLevelChangeFinished.emit(self) #self.update() diff --git a/pyqtgraph/graphicsItems/ImageItem.py b/pyqtgraph/graphicsItems/ImageItem.py index 26897cf0..3d45ad77 100644 --- a/pyqtgraph/graphicsItems/ImageItem.py +++ b/pyqtgraph/graphicsItems/ImageItem.py @@ -379,12 +379,14 @@ class ImageItem(GraphicsObject): eflsize = 2**(image.itemsize*8) ind = np.arange(eflsize) minlev, maxlev = levels + levdiff = maxlev - minlev + levdiff = 1 if levdiff == 0 else levdiff # don't allow division by 0 if lut is None: - efflut = fn.rescaleData(ind, scale=255./(maxlev-minlev), + efflut = fn.rescaleData(ind, scale=255./levdiff, offset=minlev, dtype=np.ubyte) else: lutdtype = np.min_scalar_type(lut.shape[0]-1) - efflut = fn.rescaleData(ind, scale=(lut.shape[0]-1)/(maxlev-minlev), + efflut = fn.rescaleData(ind, scale=(lut.shape[0]-1)/levdiff, offset=minlev, dtype=lutdtype, clip=(0, lut.shape[0]-1)) efflut = lut[efflut] diff --git a/pyqtgraph/graphicsItems/tests/test_ImageItem.py b/pyqtgraph/graphicsItems/tests/test_ImageItem.py index e247abe3..4f310bc3 100644 --- a/pyqtgraph/graphicsItems/tests/test_ImageItem.py +++ b/pyqtgraph/graphicsItems/tests/test_ImageItem.py @@ -60,6 +60,18 @@ def test_ImageItem(transpose=False): img.setLevels([127, 128]) assertImageApproved(w, 'imageitem/gradient_mono_byte_levels', 'Mono byte gradient w/ levels to isolate diagonal.') + # test monochrome image + data = np.zeros((10, 10), dtype='uint8') + data[:5,:5] = 1 + data[5:,5:] = 1 + img.setImage(data) + assertImageApproved(w, 'imageitem/monochrome', 'Ubyte image with only 0,1 values.') + + # test bool + data = data.astype(bool) + img.setImage(data) + assertImageApproved(w, 'imageitem/bool', 'Boolean mask.') + # test RGBA byte data = np.zeros((100, 100, 4), dtype='ubyte') data[..., 0] = np.linspace(0, 255, 100).reshape(100, 1) diff --git a/pyqtgraph/tests/image_testing.py b/pyqtgraph/tests/image_testing.py index 135ef59b..628bde1a 100644 --- a/pyqtgraph/tests/image_testing.py +++ b/pyqtgraph/tests/image_testing.py @@ -42,7 +42,7 @@ Procedure for unit-testing with images: # pyqtgraph should be tested against. When adding or changing test images, # create and push a new tag and update this variable. To test locally, begin # by creating the tag in your ~/.pyqtgraph/test-data repository. -testDataTag = 'test-data-5' +testDataTag = 'test-data-6' import time