def crop_along_cline(filename):
    print(filename)
    # logpath = os.path.join(logdir, filename + ".txt")
    # file = open(logpath, 'w')
    # file.write(repr(filename) + '\n')

    srcpath = os.path.join(srcdir, filename)
    dstpath = os.path.join(dstdir, filename)
    field_path = os.path.join(field_dir, filename)
    clinepath = os.path.join(preclinedir, filename)
    if srcmskdir:
        srcmskpath = os.path.join(srcmskdir, filename)
        dstmskpath = os.path.join(dstmskdir, filename)
    # print(clinepath)

    srcdata, header = nrrd.read(srcpath)
    clinedata, header = nrrd.read(clinepath)
    if srcmskdir:
        srcmskdata, header = nrrd.read(srcmskpath)
    # print("srcmsk", np.unique(srcmskdata))
    # file.write("srcmsk: " + repr(np.unique(srcmskdata)) + '\n')
    # print(srcdata.shape)
    # print(srcmskdata.shape)
    # print(clinedata.shape)

    # clines = get_center_lines(clinedata, point_cnt=500)
    clines = get_centerlines_by_points(clinedata, point_cnt=500)

    # print(type(clines))
    # print(len(clines))

    src_results = expand_along_curves(srcdata, clines, 50)
    if srcmskdir:
        srcmsk_results = expand_along_curves(srcmskdata, clines, 50)

    # print(len(src_results))
    # print(len(srcmsk_results))

    src_rst = src_results[0][0]
    src_dp_field = src_results[0][1]
    src_rst = src_rst.astype(np.int32)
    src_rst = np.transpose(src_rst, (1, 2, 0))
    nrrd.write(dstpath, src_rst)
    nrrd.write(field_path, src_dp_field)

    if srcmskdir:  # src with mask
        msk_rst = srcmsk_results[0][0]
        # print(type(msk_rst))
        msk_rst = msk_rst.astype(np.int16)
        # print(type(msk_rst))
        msk_rst = np.transpose(msk_rst, (1, 2, 0))
        # print("dstmsk", np.unique(msk_rst))
        nrrd.write(dstmskpath, msk_rst)
    # print("finished", filename)

    # file.close()
    return
def crop_along_cline(sfilename, cl_idx):
    print("data:", sfilename)
    basename = os.path.splitext(sfilename)[0]
    srcpath = os.path.join(srcdir, sfilename)
    srcmask_path = os.path.join(srcmask_dir, sfilename)

    srcdata, _ = nrrd.read(srcpath)
    srcmsk_data, _ = nrrd.read(srcmask_path)
    # print("srcmask: ", np.unique(srcmsk_data))

    clines = get_center_lines(clinedata, point_cnt=500)
    src_results = expand_along_curves(srcdata, clines, win)
    srcmsk_results = expand_along_curves(srcmsk_data, clines, win)

    src_rst = src_results[0][0].astype(np.int32)
    # print("src: ", np.unique(src_rst))
    src_rst = scale_range(src_rst, -1000, 1000, 1, 254)
    src_rst = src_rst.astype(np.uint8)
    src_warp = np.transpose(src_rst, (1, 2, 0))
    # print("src: ", np.unique(src_warp))

    srcmsk_rst = srcmsk_results[0][0]
    # print("srcmask: ", np.unique(srcmsk_rst))
    srcmsk_rst = (srcmsk_rst >= 0.5)
    srcmsk_rst = srcmsk_rst.astype(np.uint8)
    srcmsk_warp = np.transpose(srcmsk_rst, (1, 2, 0))
    # print("srcmask: ", np.unique(srcmsk_warp))

    (h, w, c) = src_rst.shape
    for img_idx in range(c):
        if not 1 in srcmsk_warp[:, :, img_idx]:
            continue
        imgname = basename + '_cl_' + str(cl_idx) + '_slice_' + str(
            img_idx) + '.png'
        imgpath = os.path.join(pngdir, imgname)
        imgmaskpath = os.path.join(pngmask_dir, imgname)
        imgmaskvispath = os.path.join(pngmaskvis_dir, imgname)
        im = Image.fromarray(src_warp[:, :, img_idx], mode="L")
        im.save(imgpath)
        im = Image.fromarray(srcmsk_warp[:, :, img_idx], mode="L")
        im.save(imgmaskpath)
        im = Image.fromarray(255 * srcmsk_warp[:, :, img_idx], mode="L")
        im.save(imgmaskvispath)
def crop_along_cline(sfilename, cl_idx):
    print("data:", sfilename)
    basename = os.path.splitext(sfilename)[0]
    srcpath = os.path.join(srcdir, sfilename)
    srcmask_path = os.path.join(srcmask_dir, sfilename)

    srcdata, _ = nrrd.read(srcpath)
    srcmsk_data, _ = nrrd.read(srcmask_path)
    # print("srcmask: ", np.unique(srcmsk_data))

    clines = get_center_lines(clinedata, point_cnt=500)
    src_results = expand_along_curves(srcdata, clines, win)
    srcmsk_results = expand_along_curves(srcmsk_data, clines, win)

    src_rst = src_results[0][0].astype(np.int32)
    # print("src: ", np.unique(src_rst))
    src_rst = scale_range(src_rst, -1000, 1000, 1, 254)
    src_rst = src_rst.astype(np.uint8)
    src_warp = np.transpose(src_rst, (1, 2, 0))
    # print("src: ", np.unique(src_warp))

    srcmsk_rst = srcmsk_results[0][0]
    # print("srcmask: ", np.unique(srcmsk_rst))
    srcmsk_rst = (srcmsk_rst >= 0.5)
    srcmsk_rst = srcmsk_rst.astype(np.uint8)
    srcmsk_warp = np.transpose(srcmsk_rst, (1, 2, 0))
    # print("srcmask: ", np.unique(srcmsk_warp))

    (h, w, c) = src_rst.shape
    for img_idx in range(20, c - 20):  # skip 20 layers both ends
        if not 1 in srcmsk_warp[:, :, img_idx]:
            continue
        slice_name = basename + '_cl_' + str(cl_idx) + '_slice_' + str(img_idx) + '.npz'
        slice_path = os.path.join(npzdir, slice_name)
        slice_mask_path = os.path.join(npzmaskdir, slice_name)
        slice_maskvis_path = os.path.join(npzmaskvisdir, slice_name)
        npz_slice = src_warp[:, :, img_idx - 20:img_idx + 20:5]  # TODO: modify second 20 to 21
        np.savez(slice_path, npz_slice)
        # print(np.unique(npz_slice))
        npzmask_slice = srcmsk_warp[:, :, img_idx]
        np.savez(slice_mask_path, npzmask_slice)
        npzmaskvis_slice = srcmsk_warp[:, :, img_idx] * 255
        np.savez(slice_maskvis_path, npzmaskvis_slice)
def crop_along_cline(sfilename, cidx):
    if verbose:
        print("data", sfilename)
    basename, ext = os.path.splitext(sfilename)
    sfilename_cidx = basename + '_cline' + str(cidx) + ext
    srcpath = os.path.join(srcdir, sfilename)
    dstpath = os.path.join(dstdir, sfilename_cidx)
    if field_dir:
        field_path = os.path.join(field_dir, sfilename)

    if not os.path.isfile(srcpath):
        print("cannot find", srcpath)
        return
    srcdata, header = nrrd.read(srcpath)
    if verbose:
        print("original mask: ", np.unique(srcdata))
    srcdata = srcdata.astype(np.int32)

    clinedata = cline_list[cidx]
    clines = get_center_lines(clinedata, point_cnt=500)
    src_results = expand_along_curves(srcdata, clines, 50)

    if if_mask:
        src_rst = src_results[0][0]
        # print("src_rst dtype: ", src_rst.dtype)
        # print("transformed mask0: ", np.unique(src_rst))
        src_rst = src_rst.astype(np.uint8)
    else:
        src_rst = src_results[0][0].astype(np.int32)

    src_rst = np.transpose(src_rst, (1, 2, 0))
    if field_dir:
        src_field = src_results[0][1]
    if verbose:
        print("transformed mask: ", np.unique(src_rst))
    nrrd.write(dstpath, src_rst)
    if field_dir:
        nrrd.write(field_path, src_field)