0
votes

I have a folder containing hundreds of images and another folder with their corresponding masks. Both the images and the masks are named alike, for e.g. the mask for image1.png is also named as 'image1.png'. All images are grayscale.

enter image description here

I need to overlay the masks onto the original images in a loop but only show the masks bounding boxes, while making the inner surface of the mask completely transparent to show the original image regions as shown above. The current code reads the folder containing images and the masks but I need help with the overlay as requested.

import pandas as pd
import cv2
import numpy as np
import glob
import os
images = glob.glob("images\*.png")
masks = glob.glob("masks\*.png")
images.sort()
masks.sort()
for f1,f2 in zip(images,masks):
    img1 = cv2.imread(f1)
    img2 = cv2.imread(f2)
    img_1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    img_2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
    img_name = f2.split(os.sep)[-1]
    contours, _ = cv2.findContours(img_2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    combined = cv2.drawContours(img_1, contours, -1, (0,255,0),1)
    cv2.imwrite("combined/{}.png".format(img_name[:-4]),combined) 
1
Please provide your images separately rather than a screen-grab of them side-by-side. You also say something needs to be in colour but everything looks grey to me? - Mark Setchell
I do not understand what you mean by mask in color. Your mask is b/w. Do you just want the outline of the mask showing as black on the image as you showed. If so, one way is to get the contours of the mask and then draw them on the image in black. - fmw42
I edited my code to find the contours and draw them on the original images. But now, I am getting only the last image stored with its corresponding mask overlayed. The loop is missing every image and mask except for the last image and mask. - shiva
Are your masks always 90° rectangles? Because then find contours is really overkill. - ypnos
Yes, they are bounding boxes (rectangles). I do not need the bounding boxes in color. Both the images and masks are grayscale images. The bounding box will also be in grayscale. - shiva

1 Answers

0
votes

I found the issue. I need to use Zip in the for loop. I have corrected the code accordingly.