SIPaKMeD 数据集下载 bmp和dat格式转png掩膜方案

SIPaKMeD 数据集下载

直接从官网下载即可,该数据集网站在国内没有被墙,或者可以科学上网加速下载,官网链接:
https://www.cs.uoi.gr/~marina/sipakmed.html
在这里插入图片描述
在网站下部可以看见五个下载选项,选择自己需要下载的即可

bmp和dat格式转png图片和png掩膜

原始数据集图片为bmp格式和dat的分割标注数据,其中dat文件中为x,y点对应bmp图片,这些xy点围城的圈即为被标注物的边缘信息,可以用python读取原始bmp文件,并将对应坐标点连线,填充即可
代码贴在下方,自行复制即可,仅需修改main函数中的原始文件路径和存储路径,即图中部分
在这里插入图片描述
代码如下:

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])
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐