简体   繁体   中英

how to mask the segmented area in a image?

I want to create a semantic segmentation model, which can segment the follicles in USG, using U-net. I have labelled my training data using Labelme. The color of the labelled area is #800000. How to mask this image in black and white form. I have replace the color with #ffffff. But while training the model it is giving an error: 0img.png violating range [0, 1]. Found maximum pixel value 255. Please help me to solve this problem. Images are given below. Thanks in advance.

USG 扫描

分割图像

Maybe a little late to the party but I found it better to write a separate code that will generate the mask using OpenCV.

    import json
    import cv2
    import os
    import numpy as np
    from glob import glob
    
    
    output_dir = "masks/"
    #creating the ground_truth folder if it doesn't exist
    os.makedirs(output_dir, exist_ok=True)
    
    json_files = glob("dataset/*.json")
    #loading the json file
    for image_json in json_files:
        with open(image_json) as file:
            data = json.load(file)
        filename = data["imagePath"].split(".")[0]
        
        # creating a new ground truth image
        mask = np.zeros((data["imageHeight"], data["imageWidth"]), dtype='uint8')
        for shape in data['shapes']:
            mask = cv2.fillPoly(mask, [np.array(shape['points'], dtype=np.int32)], 255)
    
        # saving the ground truth masks
        cv2.imwrite(os.path.join(output_dir,filename) + ".png", mask)

This code answers your question "How to generate a black and white mask?". But the other thing ie the "maximum value is 255", maybe it's because you haven't normalised the image array before training. I was doing a segmentation project earlier and I had used this code after using LabelMe, although when reading the image I normalised it by dividing it by 255.

May I know what language and framework that you're using? But without losing generality, let's say in Python, since you labelled the data with Labelme which saves the vertices of the polygon annotations, you can use OpenCV's API cv2.fillPoly() function to fill a zero matrix (with save size as your input image) with a specified color. This step makes the mask for visualization.

After this, a manual mapping between the colors and the labels is needed. The labels are often stored in a 1-channel matrix (not as a coloured image which has 3 or 4 channels), with other shapes same as the input image. If you have only two classes, you can just mark the zero values as background (say 0), and other colors as 1, and save it in a matrix file for further usage. Therefore, the colored images are just what you see for visually checking, not for training. What the model needs is just some simple and different numbers. For example, these days people use 'logits' to output the probability of a pixel for each class, and choose the argmax() of the probabilities, which returns labels for each pixel.

Therefore, maybe try making a matrix with element values in [0,num_of_classes - 1] would help to solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM