PyQtGraph - Getting Histogram Object for Image View

PyQtGraph - Getting Histogram Object for Image View

PyQtGraph is a popular library for fast plotting and image display in Python, particularly suited for applications that require real-time or interactive plotting. In PyQtGraph, if you want to get a histogram object for an image view, you can use the HistogramLUTItem class, which provides a histogram of image levels and a lookup table (LUT) for color mapping.

First, ensure you have PyQtGraph and its dependencies installed:

pip install pyqtgraph 

To demonstrate how to get a histogram object for an image view, let's go through an example:

1. Import Necessary Libraries

Import pyqtgraph and other necessary PyQt modules. If you haven't already, you might need to install PyQt5 or PyQt6.

import pyqtgraph as pg from pyqtgraph.Qt import QtGui import numpy as np 

2. Create a Simple Application

Create a PyQt application and a main window. Add an ImageView widget to the window.

app = QtGui.QApplication([]) # Create main window main_window = QtGui.QMainWindow() main_window.setWindowTitle('ImageView with Histogram Example') # Create an ImageView widget image_view = pg.ImageView() main_window.setCentralWidget(image_view) main_window.resize(800, 600) 

3. Generate Sample Image Data

Generate a sample image data and display it in the ImageView widget.

# Create sample image data data = np.random.normal(loc=128, scale=50, size=(200, 200)).astype(np.uint8) # Display the image image_view.setImage(data) 

4. Get the Histogram Object

The histogram object can be accessed directly from the ImageView widget.

histogram = image_view.getHistogramWidget() 

5. Show the Application

Finally, show the application window and start the event loop.

main_window.show() # Start Qt event loop if __name__ == '__main__': import sys sys.exit(app.exec_()) 

Full Example Code

Here's the complete code snippet:

import pyqtgraph as pg from pyqtgraph.Qt import QtGui import numpy as np app = QtGui.QApplication([]) # Create main window main_window = QtGui.QMainWindow() main_window.setWindowTitle('ImageView with Histogram Example') # Create an ImageView widget image_view = pg.ImageView() main_window.setCentralWidget(image_view) main_window.resize(800, 600) # Create sample image data data = np.random.normal(loc=128, scale=50, size=(200, 200)).astype(np.uint8) # Display the image image_view.setImage(data) # Get the histogram object histogram = image_view.getHistogramWidget() main_window.show() # Start Qt event loop if __name__ == '__main__': import sys sys.exit(app.exec_()) 

This script creates a window displaying an image with its corresponding histogram. You can interact with the histogram to adjust the contrast and brightness of the image. The ImageView widget in PyQtGraph is a powerful tool for image display and analysis, providing built-in support for histograms and other useful features.


More Tags

webhooks remote-host code-translation predicate angular-cli substring guava kubectl information-visualization drive

More Programming Guides

Other Guides

More Programming Examples