How To Measure FWHM from Image Python

Determining the full width at half maximum (FWHM) of features in an image is important for quantifying resolution and quality in many science and engineering fields. This guide will walk through the key steps for measuring FWHM from images in Python.

Overview

  • FWHM measures the width of a peak in a curve at half of its maximum value
  • Useful for assessing resolution of imaging systems and quality of focused features
  • Can be calculated by fitting curve to cross sectional intensity profile
  • Python libraries like NumPy, SciPy, Matplotlib, scikit-image enable FWHM analysis

Importing Python Libraries

The main libraries we will use are:

import numpy as np 
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from skimage import io, filters
JavaScript
  • NumPy: Provides arrays and mathematical operations
  • Matplotlib: Visualization and plotting
  • SciPy: Curve fitting and optimization
  • scikit-image: Image processing and analysis

Loading the Image

We will work with a sample microscope image, but the process is adaptable to any image.

image = io.imread('sample_image.jpg') 
JavaScript
  • Many common image formats supported
  • Image loaded as NumPy array

Visualizing the Image

Use Matplotlib to display the image:

fig, ax = plt.subplots()
ax.imshow(image, cmap='gray')
JavaScript
  • cmap=’gray’ sets grayscale color mapping
  • Can zoom in on features of interest

Applying Filters

Use scikit-image to filter and enhance image:

image_filtered = filters.gaussian(image, sigma=2)
edges = filters.sobel(image_filtered)
JavaScript
  • gaussian reduces noise
  • sobel detects edges

Select Profile Cross Section

row_num = 300 # Select row number
profile = edges[row_num, :] # Extract row intensities
JavaScript
  • Profile contains intensity values along cross section
  • Can visualize profile as line plot

Plot Profile

plt.plot(profile) 
plt.xlabel('Pixel Number')
plt.ylabel('Intensity')
JavaScript
  • Profile intensities now visualized
  • Can inspect peaks representing edges

Fit Gaussian Curve

Use SciPy curve fitting to fit Gaussian model:

mean = len(profile) / 2 # Estimate center position

def gaussian(x, amp, cen, wid):
    return amp * np.exp(-(x - cen)**2 / wid)

popt, pcov = curve_fit(gaussian, range(len(profile)), profile, p0=[1, mean, 1])
JavaScript
  • gaussian defines model function
  • curve_fit determines optimal parameters
  • popt contains best fit parameters

Calculate FWHM

The width parameter represents the standard deviation $\sigma$.

fwhm = 2 * np.sqrt(2 * np.log(2)) * popt[2] 
print(f'FWHM: {fwhm:.2f} pixels')
JavaScript
  • FWHM is 2.355$\sigma$ for Gaussian function
  • Print FWHM value from fitted width

Overlay Fit on Profile

x = np.arange(len(profile))  
y = gaussian(x, *popt) 

plt.plot(x, profile)
plt.plot(x, y, '--')
plt.xlabel('Pixel Number')
plt.ylabel('Intensity')
JavaScript
  • Overplots fitted Gaussian curve
  • Matches selected peak nicely

Consider Multiple Peaks

Above examines single peak, but should consider full image:

  • Repeat process with multiple row and column cuts
  • Can automation with loops through coordinates
  • Take average as overall FWHM assessment

Summary

In this guide we covered key steps for measuring FWHM from images in Python:

πŸ“Œ Load image and filter noise
πŸ“Œ Take cross sectional intensity profile
πŸ“Œ Fit Gaussian curve to peaks
πŸ“Œ Calculate FWHM from fitted width
πŸ“Œ Analyze multiple profiles for robust measurement

Being able to quantify feature sizes like FWHM enables resolution comparison across image sets and systems. The Python-based workflow presented here provides an accessible way to conduct this important analysis.

Leave a Comment