Home >Backend Development >Python Tutorial >How can local maximum filtering be used to identify pressure peaks in a 2D array representing a dog\'s paw?
Peak Detection in a 2D Array
Challenge:
Detecting peaks in a 2D array representing pressure measurements under a dog's paw, to delineate anatomical subregions.
Solution:
The practical solution involves using a local maximum filter to identify peaks. Here's how:
<code class="python">import numpy as np import matplotlib.pyplot as plt from scipy.ndimage.filters import maximum_filter from scipy.ndimage.morphology import generate_binary_structure, binary_erosion # Define the paw data paw_data = np.loadtxt("paws.txt").reshape(4, 11, 14) # Define the 8-connected neighborhood neighborhood = generate_binary_structure(2, 2) # Function to detect peaks def detect_peaks(image): # Local maximum filter local_max = maximum_filter(image, footprint=neighborhood) == image # Create a mask of the background background = (image == 0) # Erode the background to remove artifacts eroded_background = binary_erosion(background, structure=neighborhood, border_value=1) # Final mask containing only peaks detected_peaks = local_max ^ eroded_background return detected_peaks # Detect peaks for each paw paws = [p.squeeze() for p in np.vsplit(paw_data, 4)] detected_peaks_list = [] for paw in paws: detected_peaks = detect_peaks(paw) detected_peaks_list.append(detected_peaks) # Plot the results fig, axs = plt.subplots(4, 2, figsize=(10, 10)) for i, paw in enumerate(paws): axs[i, 0].imshow(paw) axs[i, 0].set_title("Paw Image") axs[i, 1].imshow(detected_peaks_list[i]) axs[i, 1].set_title("Peak Detection") plt.tight_layout() plt.show()</code>
Considerations:
The above is the detailed content of How can local maximum filtering be used to identify pressure peaks in a 2D array representing a dog\'s paw?. For more information, please follow other related articles on the PHP Chinese website!