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

该文提供了将SIPaKMeD数据集中bmp和dat格式转换为png图片及掩膜的Python代码。dat文件包含图像边缘信息,通过读取并连接xy坐标点生成掩膜。代码包括了draw_line函数用于绘制边界,transformImage函数处理单个图像和掩模,以及processingBatch函数处理批量文件的转换。最后,fillMask函数用于填充掩模。
摘要由CSDN通过智能技术生成

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])
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早安不安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值