Skip to content

Conversation

Copy link

Copilot AI commented Nov 6, 2025

Implements mask merging for datasets where a single image has multiple defect annotations split across separate mask files that need to be combined into one unified mask.

Implementation

merge_masks.py - Standalone utility script that:

  • Extracts base image IDs from mask filenames using regex pattern matching (_[text]_[number]_mask)
  • Groups masks by image ID
  • Combines masks using pixel-wise cv2.bitwise_or() to preserve all defect regions
  • Outputs grayscale masks with filenames matching source image IDs

MERGE_MASKS_README.md - Documentation with usage examples and technical details

Usage

python merge_masks.py --input /path/to/masks --output /path/to/merged

# Input masks:
#   20251008新数据-磷化倒角_102011703_白茬_3_mask.png
#   20251008新数据-磷化倒角_102011703_白茬_5_mask.png
#
# Output:
#   20251008新数据-磷化倒角_102011703.png

Default paths configured per requirements. Supports PNG/JPG/JPEG/BMP formats with extension preservation. Handles dimension mismatches and overlapping mask regions correctly.

Original prompt

Combine multiple defect masks from a single folder into one unified mask per image, ensuring the resulting mask file names match the corresponding original image file names. For instance, if the masks folder contains multiple masks like '20251008新数据-磷化倒角_102011703_白茬_3_mask.png', '20251008新数据-磷化倒角_102011703_白茬_5_mask.png', all these masks for the same image should be merged into one file. The final mask file name should be '20251008新数据-磷化倒角_102011703.png'. Ensure to use pixel-wise 'bitwise_or' for combining masks and save them as grayscale images in the output folder. The script should take input from 'D:\qw\Code_github\cicai_all\cicaiquexian_seg\train\masks' and output to 'D:\qw\Code_github\cicai_all\merged_masks_images'. Output file extensions need to match the input naming convention.

This pull request was created as a result of the following prompt from Copilot chat.

Combine multiple defect masks from a single folder into one unified mask per image, ensuring the resulting mask file names match the corresponding original image file names. For instance, if the masks folder contains multiple masks like '20251008新数据-磷化倒角_102011703_白茬_3_mask.png', '20251008新数据-磷化倒角_102011703_白茬_5_mask.png', all these masks for the same image should be merged into one file. The final mask file name should be '20251008新数据-磷化倒角_102011703.png'. Ensure to use pixel-wise 'bitwise_or' for combining masks and save them as grayscale images in the output folder. The script should take input from 'D:\qw\Code_github\cicai_all\cicaiquexian_seg\train\masks' and output to 'D:\qw\Code_github\cicai_all\merged_masks_images'. Output file extensions need to match the input naming convention.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits November 6, 2025 02:21
Co-authored-by: Kingwin97 <86706629+Kingwin97@users.noreply.github.com>
Co-authored-by: Kingwin97 <86706629+Kingwin97@users.noreply.github.com>
Co-authored-by: Kingwin97 <86706629+Kingwin97@users.noreply.github.com>
Copilot AI changed the title [WIP] Combine multiple defect masks into unified mask per image Add utility to merge multiple defect masks into unified masks per image Nov 6, 2025
Copilot AI requested a review from Kingwin97 November 6, 2025 02:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants