matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.58k stars 11.69k forks source link

About the colour of the mask #911

Closed y-kl8 closed 6 years ago

y-kl8 commented 6 years ago

In the semantic segmentation, we will set the colour of different class, but in the maskrcnn, we caculate the binary mask of each ROI, how to ensure the colour of every mask, and how to make the different object of the same class has the different colour mask.

fastlater commented 6 years ago

MaskRCNN is an instance segmentation algorithm so each mask includes only one object. I guess that part is clear for you. Now, how to ensure each object is marked with the correct class that it belong. Well, the key is in the load_mask method. Each user has to find a way to load his own mask but for example, you can follow the load_mask method used in the first release of this repo.

def load_mask(self, image_id):
        info = self.image_info[image_id]
        mask_paths = info["mask_paths"]
        count = len(mask_paths)
        masks = []
        print('image info: ',info)
        #print('Class_names:',self.class_names)
        imgName = info["path"]
        b = imgName.split("/")
        #print(b)
        #print(b[-1])
        imgID = b[-1].split("_")

        for i, mask_path in enumerate(mask_paths):
            masks.append(cv2.imread(mask_path, 0))

        masks = np.stack(masks, axis=-1)
        masks = np.where(masks > 128, 1, 0)

        #Handle occlusions
        occlusion = np.logical_not(masks[:, :, -1]).astype(np.uint8)
        for i in range(count-2, -1, -1):
            masks[:, :, i] = masks[:, :, i] * occlusion
            occlusion = np.logical_and(occlusion, np.logical_not(masks[:, :, i]))

        # Map class names to class IDs.
        if imgID[0] == 'obj1':
            class_ids = np.full(count,1)
        elif imgID[0] == 'obj2':
            class_ids = np.full(count,2)
        elif imgID[0] == 'obj3':
            class_ids = np.full(count,3)
        else:
            class_ids = np.full(count,4)

        print("Class_ids: ",' ', class_ids.shape,' ', class_ids)
        print('')

        return masks, class_ids.astype(np.int32)

In this case, each mask includes only 1 class but you can adjust it. It does use the name of the file to specify the class that it belong. It will be good if someone else can give a better way to identify the class name because this one requires to rename the input data folders.

y-kl8 commented 6 years ago

Thanks a lot, and another question,for every mask which has only one object, how can we set the colour of each one in this code? For example, in the output picture, why the person 1 is blue and the person 2 is red?

fastlater commented 6 years ago

As I said MaskRCNN is an instance segmentation, not a class segmentation. The algorithm first try to find different instances in the image. It doesn't focus on classes, it does focus on instances. However, you can easily change the method in visualize.py file to paint with only one color the instances found which belong to the same class.

padmaksha18 commented 5 years ago

@fastlater Hello, I am using the matterport code for an use case where i am generating masks to blur personal details in images like faces, car no plate etc. The masks generated are very transparent and hence nothing gets blurred actually. I tried changing the code in visualize.py for each of the brightness and saturation values. def random_colors(N, bright=True): """ Generate random colors. To get visually distinct colors, generate them in HSV space then convert to RGB. """ brightness = 1.0 if bright else 0.7 hsv = [(i / N, 1, brightness) for i in range(N)] colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv)) random.shuffle(colors) return colors

But if i change the brightness to 0 or 1 , it either becomes dark or white but the density of color donot change. How can i achieve a complete opaque color, kindly advice. Many thanks in advance.

nyshthefantastic commented 4 years ago

@fastlater Hello, I am using the matterport code for an use case where i am generating masks to blur personal details in images like faces, car no plate etc. The masks generated are very transparent and hence nothing gets blurred actually. I tried changing the code in visualize.py for each of the brightness and saturation values. def random_colors(N, bright=True): """ Generate random colors. To get visually distinct colors, generate them in HSV space then convert to RGB. """ brightness = 1.0 if bright else 0.7 hsv = [(i / N, 1, brightness) for i in range(N)] colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv)) random.shuffle(colors) return colors

But if i change the brightness to 0 or 1 , it either becomes dark or white but the density of color donot change. How can i achieve a complete opaque color, kindly advice. Many thanks in advance.

@padmaksha18 in visualize.py under apply_mask change alpha value to 1.