Basem
Basem

Reputation: 25

plotting an image in Hyperspy using Python to filter the pixel intensity

I am using HyperSpy software for image processing using python code below.

I am trying to plot the grayscale image shown below (SI[3]) but after filtering out unwanted pixels values (background noise). so the shells shown in the image have a pixel intensity of 24,400 and above, while the background hvae less than that.

I want to re-plot the image such that the scalebar is cropped from 0-24,400 (only the bright pixels remain in the image while the rest are set to 0).

Another thing is I plan count the number of these pixels with value of 24,400 and above. how can i count the number of pixels?

import hyperspy.api as hs
import numpy as np
#load the data
SI = hs.load("SI HAADF 0945 230 nm.emd")
#plot the image
SI[3].plot()

output:

enter image description here

Upvotes: 0

Views: 294

Answers (1)

Eric Prestat
Eric Prestat

Reputation: 21

For the first of your question, here is an example on how to specify the intensity range to display by using vmin, vmax in the plot method of hyperspy signals:

import hyperspy.api as hs
import skimage

# Create synthetic data
data = skimage.img_as_float(skimage.data.binary_blobs()) * 100
s = hs.signals.Signal2D(data)
# Add noise
s.add_gaussian_noise(10)

# Display image with specified minimum value
s.plot(vmin=75, vmax=100)

vmin, vmax can also take percentile value, which is particularly used when visualizing multidimensional dataset - for more details, see the API reference, https://hyperspy.readthedocs.io/en/stable/api/hyperspy._signals.signal2d.html#hyperspy._signals.signal2d.Signal2D.plot

For the second part of your question, you can simply do the following to apply a threshold, here 100, and then take the sum:

pixel_number = (s.data > 100).sum()

If you want something more sophisticated, you may need to consider using scikit-image to use segmentation, for example:

https://scikit-image.org/docs/stable/user_guide/tutorial_segmentation.html

https://scikit-image.org/docs/stable/auto_examples/segmentation/plot_random_walker_segmentation.html#sphx-glr-auto-examples-segmentation-plot-random-walker-segmentation-py

Upvotes: 1

Related Questions