Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make more than 1 binary mask in an image? #2

Open
BabakRG opened this issue Jul 26, 2022 · 1 comment
Open

How to make more than 1 binary mask in an image? #2

BabakRG opened this issue Jul 26, 2022 · 1 comment

Comments

@BabakRG
Copy link

BabakRG commented Jul 26, 2022

@maftouni Hi,
Hope you be well, I have got a question. The code you scripted, just make binary mask from first region in each image. How can I convert all regions in images to binary masks?
Thanks a lot.

@BabakRG BabakRG closed this as completed Jul 26, 2022
@BabakRG BabakRG reopened this Jul 26, 2022
@phsilvafct
Copy link

@BabakRG I have made some modifications to the code as to represent all the regions(polygons) into the binary mask, I also made the change of storing the masks with the same filename as the original images. @maftouni Maybe you can add this code the repository as there are several questions on the subject online.

import json
import os
import numpy as np
import PIL.Image
import cv2
import matplotlib.pyplot as plt


with open("annotation.json", "r") as read_file:
    data = json.load(read_file)

all_file_names=list(data.keys())

Files_in_directory = []
for root, dirs, files in os.walk("sample_frames"):
    for filename in files:
        Files_in_directory.append(filename)
        
for j in range(len(all_file_names)): 
    image_name=data[all_file_names[j]]['filename']
    if image_name in Files_in_directory: 
         img = np.asarray(PIL.Image.open('sample_frames/'+image_name))    
    else:
        continue
    
    if data[all_file_names[j]]['regions'] != {}:
        x = 0
        mask = np.zeros((img.shape[0],img.shape[1]))
        while x < len(data[all_file_names[j]]['regions']):
            try: 
                shape1_x=data[all_file_names[j]]['regions'][str(x)]['shape_attributes']['all_points_x']
                shape1_y=data[all_file_names[j]]['regions'][str(x)]['shape_attributes']['all_points_y']
            except : 
                shape1_x=data[all_file_names[j]]['regions'][x]['shape_attributes']['all_points_x']
                shape1_y=data[all_file_names[j]]['regions'][x]['shape_attributes']['all_points_y']
            

            ab=np.stack((shape1_x, shape1_y), axis=1)
            img2=cv2.drawContours(img, [ab], -1, (255,255,255), -1)
            img3=cv2.drawContours(mask, [ab], -1, 255, -1)
            x+=1

        tempTuple = os.path.splitext(data[all_file_names[j]]['filename'])
        cv2.imwrite('binary_masks/' + tempTuple[0] + '.png',mask.astype(np.uint8))

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

No branches or pull requests

2 participants