示例#1
0
def cell_sampling(xml_file, save_path, size):
    labels = get_labels(xml_file)
    # print(labels)

    filename = os.path.splitext(xml_file)[0]
    if (not os.path.isfile(filename + ".tif")) and (
            not os.path.isfile(filename + ".kfb")):
        print(filename + " doesn't exist")
        return
    try:
        slide = openslide.OpenSlide(filename + ".tif")
    except:
        slide = TSlide(filename + ".kfb")

    size_x, size_y = slide.dimensions
    points_xy = get_windows(labels, size_x, size_y, size)
    # print(points_xy)

    # generate jpg files
    for (x, y) in points_xy:
        cell = slide.read_region((x, y), 0, (size, size)).convert("RGB")
        cell.save(save_path + "/" + os.path.basename(filename) + "_" + str(x) +
                  "_" + str(y) + ".jpg")

    slide.close()

    # generate xml files
    new_xmls = Xml(os.path.basename(filename), save_path, points_xy, labels,
                   size)
    new_xmls.gen_xml()

    print("processed ", xml_file)
示例#2
0
def cell_sampling(label_file, wsi_path, save_path, size):

    labels = get_labels(label_file)
    if len(labels) == 0:
        return

    print("PROCESSING %s ..." % wsi_path)

    scale = 20
    reader = kr.reader()
    kr.reader.ReadInfo(reader, wsi_path, scale, True)

    points_xy = get_windows_new(labels, size)
    filename, _ = os.path.splitext(os.path.basename(wsi_path))

    # generate img files
    points_num = len(points_xy)
    for i, (x, y) in enumerate(points_xy):
        if ((i % 100) == 0):
            print(filename, "processed #", i)
        cell = reader.ReadRoi(x, y, size, size, scale)
        
        image_file_name = save_path + "/" + filename + "_" + str(x) + "_" + str(y) + ".bmp"
        
        # change l and s of image
        # cell = hls_trans_smart(cell)
        
        cv2.imwrite(image_file_name, cell)

    # generate xml files
    print(filename, "generating xml")
    new_xmls = Xml(filename, save_path, points_xy, labels, size)
    new_xmls.gen_xml()

    print("[INFO]", "processed ", filename)
示例#3
0
def cellSampling(files_list, save_path, size):
    for txt_file in files_list:
        # from .txt filename, get .jpg filename
        filename = os.path.splitext(txt_file)[0]

        # open .txt file
        labels = get_labels(txt_file)
        print(labels)

        # open .jpg file
        img_file = filename + ".jpg"
        img = Image.open(img_file)

        size_x, size_y = img.size
        points_xy = get_windows(labels, size_x, size_y, size)
        print(points_xy)

        # generate jpg files
        for (x, y) in points_xy:
            img.crop(
                (x, y, x + size,
                 y + size)).save(save_path + "/" + os.path.basename(filename) +
                                 "_" + str(x) + "_" + str(y) + ".jpg")

        img.close()

        # generate xml files
        new_xmls = Xml(os.path.basename(filename), save_path, points_xy,
                       labels, size)
        new_xmls.gen_xml()
def cell_sampling(xml_file, tiff_path, save_path, size, scale):

    #    print("#INFO# ", "start cp file")

    # copy remote file to local
    #    local_xml_file = cp_originfile_from_remote(xml_file, path_temp)
    #    xml_file = local_xml_file

    #    print("#INFO# ", "end cp file")

    labels = get_labels(xml_file)

    # filename = os.path.splitext(xml_file)[0]
    # if (not os.path.isfile(filename+".tif")) and (not os.path.isfile(filename+".kfb")):
    #     print(filename + " doesn't exist")
    #     return

    print("PROCESSING %s ..." % tiff_path)

    try:
        try:
            slide = openslide.OpenSlide(tiff_path)
        except:
            slide = TSlide(tiff_path)
    except:
        print("ERROR #", "can not open pic ", tiff_path)
        exit()

    size_x, size_y = slide.dimensions
    #    points_xy = get_windows(labels, size_x, size_y, size)
    # new gen method for cell base instead list all pic

    points_xy = get_windows_new(labels, size_x, size_y, size, tiff_path)
    #    print("labels: ", labels)
    #    print("points_xy: ", points_xy)
    #    print("size_x: ", size_x, "   size_y:", size_y, "   size:", size)

    # generate jpg files
    # print(filename)
    points_num = len(points_xy)
    for i, (x, y) in enumerate(points_xy):
        if ((i % 100) == 0):
            print("# process # ", i, '/', points_num - 1, x, y, size, size)
        cell = slide.read_region((x, y), 0, (size, size)).convert("RGB")

        filename, _ = os.path.splitext(os.path.basename(tiff_path))
        image_file_name = save_path + "/" + filename + "_" + str(
            x) + "_" + str(y) + ".jpg"

        # need scale pic from 1216 to 608

        cell = cell.resize((int(size * scale + 0.5), int(size * scale + 0.5)))
        cell.save(image_file_name)

        # print("#INFO# ", "get one region cost time is ", (end_get_region - start_get_region).microseconds)
        # print("#INFO# ", "save one region cost time is ", (end_save_region - end_get_region).microseconds)

    slide.close()

    # generate xml files
    new_xmls = Xml(os.path.basename(filename), save_path, points_xy, labels,
                   size, scale)
    new_xmls.gen_xml()

    #end_one_big_pic = datetime.utcnow()

    print("INFO# ", "small pics num is ", i)

    # rm temp local file
    #    rm_tempfile_from_local(local_xml_file, path_temp)

    print("INFO# ", "processed ", xml_file)