From ba4f4e51058c6af1554e8a5bfc5bd15a045ae4d5 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Fri, 13 Jun 2014 18:02:39 -0600 Subject: [PATCH] Added image analysis example --- examples/__main__.py | 1 + examples/imageAnalysis.py | 98 +++++++++++++++++++++++++ pyqtgraph/graphicsItems/IsocurveItem.py | 6 +- 3 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 examples/imageAnalysis.py diff --git a/examples/__main__.py b/examples/__main__.py index ea948bf4..cb1b87a1 100644 --- a/examples/__main__.py +++ b/examples/__main__.py @@ -26,6 +26,7 @@ examples = OrderedDict([ ('Crosshair / Mouse interaction', 'crosshair.py'), ('Data Slicing', 'DataSlicing.py'), ('Plot Customization', 'customPlot.py'), + ('Image Analysis', 'imageAnalysis.py'), ('Dock widgets', 'dockarea.py'), ('Console', 'ConsoleWidget.py'), ('Histograms', 'histogram.py'), diff --git a/examples/imageAnalysis.py b/examples/imageAnalysis.py new file mode 100644 index 00000000..8283144e --- /dev/null +++ b/examples/imageAnalysis.py @@ -0,0 +1,98 @@ +# -*- coding: utf-8 -*- +""" +Demonstrates common image analysis tools. + +Many of the features demonstrated here are already provided by the ImageView +widget, but here we present a lower-level approach that provides finer control +over the user interface. +""" +import initExample ## Add path to library (just for examples; you do not need this) + +import pyqtgraph as pg +from pyqtgraph.Qt import QtCore, QtGui +import numpy as np + +pg.mkQApp() + +win = pg.GraphicsLayoutWidget() +win.setWindowTitle('pyqtgraph example: Image Analysis') + +# A plot area (ViewBox + axes) for displaying the image +p1 = win.addPlot() + +# Item for displaying image data +img = pg.ImageItem() +p1.addItem(img) + +# Custom ROI for selecting an image region +roi = pg.ROI([-8, 14], [6, 5]) +roi.addScaleHandle([0.5, 1], [0.5, 0.5]) +roi.addScaleHandle([0, 0.5], [0.5, 0.5]) +p1.addItem(roi) +roi.setZValue(10) # make sure ROI is drawn above image + +# Isocurve drawing +iso = pg.IsocurveItem(level=0.8, pen='g') +iso.setParentItem(img) +iso.setZValue(5) + +# Contrast/color control +hist = pg.HistogramLUTItem() +hist.setImageItem(img) +win.addItem(hist) + +# Draggable line for setting isocurve level +isoLine = pg.InfiniteLine(angle=0, movable=True, pen='g') +hist.vb.addItem(isoLine) +hist.vb.setMouseEnabled(y=False) # makes user interaction a little easier +isoLine.setValue(0.8) +isoLine.setZValue(1000) # bring iso line above contrast controls + +# Another plot area for displaying ROI data +win.nextRow() +p2 = win.addPlot(colspan=2) +p2.setMaximumHeight(250) +win.resize(800, 800) +win.show() + + +# Generate image data +data = np.random.normal(size=(100, 200)) +data[20:80, 20:80] += 2. +data = pg.gaussianFilter(data, (3, 3)) +data += np.random.normal(size=(100, 200)) * 0.1 +img.setImage(data) +hist.setLevels(data.min(), data.max()) + +# build isocurves from smoothed data +iso.setData(pg.gaussianFilter(data, (2, 2))) + +# set position and scale of image +img.scale(0.2, 0.2) +img.translate(-50, 0) + +# zoom to fit imageo +p1.autoRange() + + +# Callbacks for handling user interaction +def updatePlot(): + global img, roi, data, p2 + selected = roi.getArrayRegion(data, img) + p2.plot(selected.mean(axis=1), clear=True) + +roi.sigRegionChanged.connect(updatePlot) +updatePlot() + +def updateIsocurve(): + global isoLine, iso + iso.setLevel(isoLine.value()) + +isoLine.sigDragged.connect(updateIsocurve) + + +## Start Qt event loop unless running in interactive mode or using pyside. +if __name__ == '__main__': + import sys + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): + QtGui.QApplication.instance().exec_() diff --git a/pyqtgraph/graphicsItems/IsocurveItem.py b/pyqtgraph/graphicsItems/IsocurveItem.py index 897df999..4474e29a 100644 --- a/pyqtgraph/graphicsItems/IsocurveItem.py +++ b/pyqtgraph/graphicsItems/IsocurveItem.py @@ -35,11 +35,6 @@ class IsocurveItem(GraphicsObject): self.setPen(pen) self.setData(data, level) - - - #if data is not None and level is not None: - #self.updateLines(data, level) - def setData(self, data, level=None): """ @@ -65,6 +60,7 @@ class IsocurveItem(GraphicsObject): """Set the level at which the isocurve is drawn.""" self.level = level self.path = None + self.prepareGeometryChange() self.update()