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

Defect (Dissimilar) Detection Using SSIM-Based Image Comparison and Contour Highlighting

This Python script (code) performs a defect detection analysis by comparing two grayscale images—page35a.png (the test image) and page35b.png (the reference). It uses the Structural Similarity Index (SSIM) to compute a pixel-wise similarity map, which is then inverted to emphasize dissimilar regions. After normalizing and thresholding the resulting difference map, the script identifies and outlines all defective areas using contours, drawing them in blue. The most significant defect (based on contour area) is highlighted in green to distinguish it from smaller discrepancies. Finally, the annotated result is saved as defect_highlighted.png and displayed. This method effectively locates and visualizes the most structurally inconsistent region, making it especially useful for pattern-based defect inspection in repetitive image arrays.

To make detection more sensitive (detect smaller differences):

threshold_value = 10 # lower value = more sensitive
_, thresh = cv2.threshold(diff_norm, threshold_value, 255, cv2.THRESH_BINARY)

To make detection less sensitive (ignore small differences):

threshold_value = 50 # higher value = less sensitive
_, thresh = cv2.threshold(diff_norm, threshold_value, 255, cv2.THRESH_BINARY)