user25377348
user25377348

Reputation:

down quality of mask with json data for train u net model

I want mask my images with json formats as data for training model of u net. I use this below code to mask them:


import json
import numpy as np
import cv2
import os

# Path to the folder containing JSON files
json_folder = os.path.expanduser('~/Desktop/jeson')  # Folder with JSON files
# Path to the folder for saving mask images
mask_folder = os.path.expanduser('~/Desktop/masks')  # Folder to save masks

# Ensure the folder for saving masks exists
os.makedirs(mask_folder, exist_ok=True)

# List all JSON files in the folder
for filename in os.listdir(json_folder):
    if filename.endswith('.json'):
        json_file = os.path.join(json_folder, filename)

        # Load data from the JSON file
        with open(json_file) as f:
            data = json.load(f)

        # Create an empty mask
        mask = np.zeros((data['imageHeight'], data['imageWidth']), dtype=np.uint8)

        # Add regions to the mask
        for shape in data['shapes']:
            points = np.array(shape['points'], dtype=np.int32)
            if len(points) > 0:
                # Fill the area defined by the points with white color
                cv2.fillPoly(mask, [points], 49)

        # Save the mask as a PNG image using OpenCV
        mask_filename = os.path.splitext(filename)[0] + '_mask.png'
        cv2.imwrite(os.path.join(mask_folder, mask_filename), mask)

print("Conversion completed, and mask images have been saved in the 'masks' folder!")


but there is an interesting problem. it mask just a few of them good in start but others masked with just a thin line. when I again try to use this code it masked all them with a thin line.

for instance image before process in labelme tool: my images before process them in lamelme

for instance masked jeson data: jeson masked

how I can fix this problem?

Upvotes: 0

Views: 85

Answers (1)

user25377348
user25377348

Reputation:

I could not solve my problem to mask accurate with JESON data. I try to use open CV and creat polygon to mask my originals images so I decide to use this script as my time and accuracy project requirements:


import cv2
import os
import time

# Path to the folder containing red images
red_folder = os.path.expanduser('~/Desktop/red')

# Path to the folder for saving masked images
mask_folder = os.path.expanduser('~/Desktop/q')

# Ensure the folder for saving masks exists
os.makedirs(mask_folder, exist_ok=True)

# List all image files in the folder
image_files = [f for f in os.listdir(red_folder) if f.endswith(('.jpg', '.png', '.jpeg'))]

# Iterate over each image
for i, image_file in enumerate(image_files):
    # Read the image
    image_path = os.path.join(red_folder, image_file)
    image = cv2.imread(image_path)
    
    # Display the original image for debugging
    cv2.imshow("Original Image", image)
    cv2.waitKey(0)  # Wait for a key press
    cv2.destroyAllWindows()  # Close the window

    # Create binary mask for red regions
    mask1 = cv2.inRange(image, (0, 0, 100), (100, 100, 255))  # Bright red
    mask2 = cv2.inRange(image, (0, 100, 100), (100, 255, 255))  # Dark red
    mask = cv2.bitwise_or(mask1, mask2)  # Combine both masks

    # Display the mask for debugging
    cv2.imshow("Mask", mask)
    cv2.waitKey(0)  # Wait for a key press
    cv2.destroyAllWindows()  # Close the window

    # Save the masked image
    mask_filename = f"mask_{image_file}"
    mask_path = os.path.join(mask_folder, mask_filename)
    cv2.imwrite(mask_path, mask)
    
    print(f"Processed image {i+1}/{len(image_files)}: {mask_filename}")
    
    # Wait for 2 seconds
    time.sleep(2)


however this way has challenges in masking when there is more red color things. this code add these more red thing to mask and lowing the quality. and is not accurate when I want train a conjuctival pulpebral model in u net in the future as is my final medical goal.

Upvotes: 0

Related Questions