Python Automation and Machine Learning for EM and ICs

An Online Book, Second Edition by Dr. Yougui Liao (2019)

Python Automation and Machine Learning for EM and ICs - An Online Book

Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

Detection and Visualization of Peak Intensities in Images with Adjustable Dot Marking and Thresholding

This script (code) processes a grayscale SEM image to analyze its brightness distribution along horizontal rows. It first loads the image, then calculates the total pixel intensity for each row by summing all column values in that row. Next, it generates an x-axis representing the row numbers and plots the summed intensities, allowing visual identification of intensity variations across the image. The resulting plot helps highlight patterns, gradients, or bands that may exist in the image’s horizontal direction.

The updated Python script (code) introduces a peak detection function using scipy.signal.find_peaks, which identifies local maxima in the summed column intensities for each row of the image. This addition allows for the automatic identification of prominent intensity variations across the rows, which can be indicative of key features in the image, such as edges or patterns. The peaks are visualized by overlaying red dots on the plotted summed intensities, facilitating the analysis of spatially significant regions in the image. This enhancement aids in the quantitative analysis of image features, providing a clearer representation of intensity trends and facilitating the detection of patterns that may otherwise be difficult to discern visually.

This script (code) added a print statement that outputs the row numbers where these peaks occur, allowing for a clearer understanding of where significant intensity variations are present in the image. These new functions help highlight important features in the image data, aiding in the analysis of spatial intensity distributions across rows.

For each detected peak, the updated script (code) calculates and prints the row numbers x_axis[peaks] - n and x_axis[peaks] + n, where n represents a predefined offset, allowing for a more detailed examination of the data in relation to neighboring rows. These enhancements enable better visualization and understanding of the intensity variations across rows in the image.

In the updated script (code), for each identified pair of surrounding rows, we then computed the average intensity of the rows within this range. This additional functionality allows for a more detailed characterization of the image intensity variations around the peaks, providing insights into the local intensity behavior and enabling further analysis of intensity patterns in the context of the overall image.

The added functions in the updated script (code) calculate the average intensities of pixels in each column between the row pair peak - n and peak + n for each peak. This means we will compute the average intensity for each column within that specific row range.

The added functions in the updated script (code) plot green dots on the original image at the pixels in the rows corresponding to the peaks, where the pixel intensities in each column exceed 80% of the average intensity calculated for that specific row range (average_intensity), and then print the row numbers where the green dots are plotted.

The added functions in the updated script (code) print the row numbers, where the green dots are plotted, by adding a code line that outputs the row number (peak) and the corresponding column index (col_idx) when the condition for plotting the green dot is met, and then plot red dots at the center of continuous green dots on the original image. These red dots represent a group of continuous green dots (i.e., consecutive pixels that meet the condition for being green dots).

With the updated script (code), only green dots and corresponding red dots are shown if there are more than m continuous green dots by adding a check before plotting the red dots. That is, we'll only plot the red dot if the number of continuous green dots exceeds the threshold m.

With the modified script (code), green dots are not plotted, and dots with a specified radius (e.g., 3 pixels) are used to replace the single-pixel red dots.

The additional codes (code) extract the intensities at the detected peaks using summed_intensities[peaks] and calculate the average of these peak intensities.

The additional codes (code) is to ensure that the operations inside the loop for peak in x_axis[peaks] are only executed for certain peaks based on average_peak_intensity by adding a condition that filters out peaks whose intensity does not meet the desired threshold in relation to average_peak_intensity.

The script (code) has been enhanced to plot and save an image with the row numbers (only for rows containing red dots) and the count of red dots. It first counts the number of red dots in each row and then overlays the row numbers and red dot counts on the image using matplotlib. The data, including row numbers and corresponding red dot counts, is saved to a CSV file using Python's csv module. This allows for both visual representation and easy access to the red dot data in a tabular format. The numbers of peaks with maximum and minimum counts of red dots, and the counts of peaks with maximum and minimum red dots are also into the CSV file as a summary, and then, print the occurrence of the smallest and largest values in the "Red Dot Count" column.

The updated script (code) now includes a feature that annotates the image with red dot counts next to the corresponding rows where the red dots are located. It calculates the count of red dots for each row and places these counts to the left of the respective row numbers on the image using matplotlib's plt.text() function. The image, with the red dots and dot counts, is saved as 'image_with_red_dots.png'. Additionally, the script continues to save the detailed red dot data and summary information in a CSV file, including the difference between adjacent row numbers. This enhanced functionality provides a visual representation of the red dot counts alongside the corresponding rows, making the data more accessible for further analysis.

Variable Description
image_path
Path to the input image file.
image
Grayscale image loaded using OpenCV.
summed_intensities
Sum of pixel intensities along each row of the image.
x_axis
An array representing the row numbers (indices) corresponding to summed_intensities.
peaks
Indices of the detected peaks in summed_intensities using find_peaks.
average_peak_intensity
Average of the intensities at the peak positions.
peak_intensities
Intensities of the image at the detected peak positions.
image_with_dots
Copy of the original image to draw the red and green dots on.
red_dot_centers
List of tuples storing the positions of red dots (center of continuous green dots).
m
Minimum number of continuous green dots required to plot red dots.
dot_radius
Radius of the red dots to be plotted.
n
Number of rows around each peak to consider for calculating average intensities.
intensity_threshold_factor
A multiplier used to filter peaks based on their intensity relative to average_peak_intensity.
selected_peaks
List of peaks that pass the intensity threshold condition.
green_dot_start
Tracks the start column index of continuous green dots.
green_dot_count
Counts the number of continuous green dots.
start_row
Start row index for calculating the average intensity around each peak.
end_row
End row index for calculating the average intensity around each peak.
average_intensity
The average intensity for the rows between start_row and end_row.
column_averages
Average intensity for each column between start_row and end_row.
col_idx
Column index in the loop for processing each pixel in column_averages.
avg_intensity
The intensity for the current column used to check if it exceeds the threshold.
red_dot_counts
A dictionary that stores the count of red dots for each row. The key is the row number, and the value is the count of red dots in that row.
csv_filename
The name of the CSV file where the row numbers and red dot counts are saved. It is set to 'red_dot_counts.csv'.
row
The row number from red_dot_counts that is used when writing to the CSV file.
count
The count of red dots for the corresponding row in red_dot_counts, used when writing to the CSV file.
prev_row
The previous row number in the iteration. Used to calculate the difference with the current row number.
diff
The difference between the current row number and the previous row number. If it's the first row, it remains an empty string.
max_peak
The row number (peak) that has the maximum count of red dots. Found using the max() function on the red_dot_counts dictionary.
min_peak
The row number (peak) that has the minimum count of red dots. Found using the min() function on the red_dot_counts dictionary.
center_col
The column index of the center of a sequence of continuous green dots.