File: show_histogram_python.py

package info (click to toggle)
camitk 6.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 389,496 kB
  • sloc: cpp: 103,476; sh: 2,448; python: 1,618; xml: 984; makefile: 128; perl: 84; sed: 20
file content (22 lines) | stat: -rw-r--r-- 637 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# This is a CamiTK python action
#
# Compute and plot image histogram using numpy and matplotlib
import camitk
import numpy as np
import matplotlib
matplotlib.use('qtagg')
import matplotlib.pyplot as plt

def process(self:camitk.Action):
    image_component = self.getTargets()[0]
    image_data = image_component.getImageDataAsNumpy()

    flattened = image_data.ravel()  # or array3d.flatten()

    # Plot histogram
    plt.hist(flattened, bins=50, color='blue', alpha=0.7)
    plt.title("Histogram '" + image_component.getName() + "' voxel values")
    plt.xlabel("Value")
    plt.ylabel("Frequency")
    plt.grid(True)
    plt.show()