SIPaKMeD dataset download bmp and dat format to png mask scheme

SIPaKMeD dataset download bmp and dat format to png mask scheme

SIPaKMeD dataset download

You can download it directly from the official website. The data set website is not blocked in China, or you can speed up the download scientifically. The official website link:
https://www.cs.uoi.gr/~marina/sipakmed.html
insert image description here
can be seen at the bottom of the website Five download options, choose what you need to download

bmp and dat format to png image and png mask

The original data set picture is in bmp format and dat segmentation label data, where x and y points in the dat file correspond to the bmp picture, and the circle surrounded by these xy points is the edge information of the marked object, and the original bmp file can be read with python , and connect the corresponding coordinate points, fill it in. Paste
the code below and copy it by yourself. You only need to modify the original file path and storage path in the main function, that is, part of the
insert image description here
code in the figure is as follows:

import numpy as np
from PIL import Image
import os
import matplotlib as plt
from scipy import ndimage
import cv2


def draw_line(image, x1, y1, x2, y2):
    """Draw a line on a binary image using Bresenham's line algorithm."""
    dx = abs(x2 - x1)
    dy = abs(y2 - y1)
    x, y = x1, y1
    sx = -1 if x1 > x2 else 1
    sy = -1 if y1 > y2 else 1
    if dx > dy:
        err = dx / 2.0
        while x != x2:
            image[y, x] = 1
            err -= dy
            if err < 0:
                y += sy
                err += dx
            x += sx
    else:
        err = dy / 2.0
        while y != y2:
            image[y, x] = 1
            err -= dx
            if err < 0:
                x += sx
                err += dy
            y += sy
    image[y, x] = 1
    return image





def transformImage(origin_path, image_name, mask_name, save_path):
    image_path = origin_path + image_name
    mask_path = origin_path + mask_name
    img = Image.open(image_path)
    y_y, x_x = img.size

    # Load the segmentation information from the DAT file
    with open(mask_path, 'r') as f:
        # Skip the first line of the file, which contains a header
        next(f)

        # Read the coordinates of the boundary points
        points = []
        for line in f:
            x, y = line.split(',')
            points.append((int(float(x)), int(float(y))))

    # Create a binary mask with the same dimensions as the BMP image
    mask = np.zeros(img.size, dtype=np.uint8)

    # Draw the boundary of the lesion on the mask
    for i in range(len(points)):
        y1, x1 = points[i]
        y2, x2 = points[(i + 1) % len(points)]
        if y1 < 0 or y2 < 0 or x1 < 0 or x2 < 0:
            return
        if y1 >= y_y or y2 >= y_y or x1 >= x_x or x2 >= x_x:
            return
        mask[y1, x1] = 1
        mask = draw_line(mask, x1, y1, x2, y2)

    # Save the original BMP image as a PNG file
    img.save(save_path + image_name[:-4] + '.png')

    mask = cv2.transpose(mask)

    # Save the mask as a PNG file
    mask_img = Image.fromarray(mask * 255)
    mask_img.save(save_path + mask_name[:-4] + '.png')


def fillMask(mask_path):
    """ fill mask png """
    img_mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
    img_mask = ndimage.binary_fill_holes(img_mask)
    img_mask = np.float32(img_mask)
    cv2.imwrite(mask_path, img_mask, [int(cv2.IMWRITE_PNG_COMPRESSION), 3])


def processingBatchCRO(file_path, save_path):
    """
        :depict processing batch bmp and dat file to png file
        :param file_path: str
        :param save_path: str
        :return: bool
        """
    print("Begin process batch")
    filename_list = os.listdir(file_path)
    bmp_file = []
    dat_file = []
    for i in filename_list:
        if i[-3:] == 'bmp':
            bmp_file.append(i)
        elif i[-3:] == 'dat':
            dat_file.append(i)

    i, j = 0, 0
    while i < len(bmp_file) and j < len(dat_file):

        if bmp_file[i][:6] == dat_file[j][:6]:

            print('Process', bmp_file[i], dat_file[j])
            transformImage(file_path, bmp_file[i], dat_file[j], save_path)
            j = j + 1
        else:
            i = i + 1


def processingBatch(file_path, save_path):
    """
    :depict processing batch bmp and dat file to png file
    :param file_path: str
    :param save_path: str
    :return: bool
    """
    print("Begin process batch")
    filename_list = os.listdir(file_path)
    bmp_file = []
    dat_file = []
    for i in filename_list:
        if i[-3:] == 'bmp':
            bmp_file.append(i)
        elif i[-3:] == 'dat':
            dat_file.append(i)

    i, j = 0, 0
    while i < len(bmp_file) and j < len(dat_file):

        if bmp_file[i][:3] == dat_file[j][:3]:

            print('Process', bmp_file[i], dat_file[j])
            transformImage(file_path, bmp_file[i], dat_file[j], save_path)
            j = j + 1
        else:
            i = i + 1

def fillImageProcessCRO(file_path):
    print("Begin process batch")
    filename_list = os.listdir(file_path)
    for i in filename_list:
        if i[6] == '_':
            print("Process", i)
            fillMask(file_path + i)


def fillImageProcess(file_path):
    print("Begin process batch")
    filename_list = os.listdir(file_path)
    for i in filename_list:
        if i[3] == '_':
            print("Process", i)
            fillMask(file_path + i)


if __name__ == '__main__':
    # Original file path
    file_name = [r'K:/DataSets/SIPaKMeD/im_Dyskeratotic/',
                 r'K:/DataSets/SIPaKMeD/im_Koilocytotic/',
                 r'K:/DataSets/SIPaKMeD/im_Metaplastic/',
                 r'K:/DataSets/SIPaKMeD/im_Parabasal/',
                 r'K:/DataSets/SIPaKMeD/im_Superficial-Intermediate/']
    # Save path
    save_path = [r'K:/DataSets/SIPaKMeD/png_Dyskeratotic/',
                 r'K:/DataSets/SIPaKMeD/png_Koilocytotic/',
                 r'K:/DataSets/SIPaKMeD/png_Metaplastic/',
                 r'K:/DataSets/SIPaKMeD/png_Parabasal/',
                 r'K:/DataSets/SIPaKMeD/png_Superficial-Intermediate/']
    for i in range(5):
        processingBatchCRO(file_name[i] + 'CROPPED/', save_path[i] + 'CROPPED/')
        fillImageProcessCRO(save_path[i] + 'CROPPED/')

    for i in range(5):
        processingBatch(file_name[i], save_path[i])
        fillImageProcess(save_path[i])

Guess you like

Origin blog.csdn.net/weixin_51717597/article/details/129281661