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

Conversion of Green Regions Connected to Red Areas in Images

This Python script (code and code) uses OpenCV to modify an image by converting any green pixel clusters that are connected to red regions into red. It first converts the input image into HSV color space to better distinguish color ranges and then creates binary masks for red and green pixels. The script uses morphological dilation to slightly expand the red areas, allowing flexible control over what counts as “connected.” It then identifies connected green components using an 8-connectivity algorithm and checks whether any part of each component touches the red zone. If so, those green areas are recolored red in the output image. This script is useful for image analysis or visualization tasks where color adjacency relationships need to be highlighted or unified automatically.