Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About data preprocessing #19

Open
wruii opened this issue Dec 11, 2024 · 1 comment
Open

About data preprocessing #19

wruii opened this issue Dec 11, 2024 · 1 comment

Comments

@wruii
Copy link

wruii commented Dec 11, 2024

Excuse me~ I want to ask how CT Organ data be processed? just transforming them into 2D and dividing the data into the training/test set are okay? thanks!

@nickk124
Copy link
Member

Hi, we extract .png slice images from the original .nii files of the dataset with the code provided below. You'll just need to modify the source and destination files for where your input .nii and output files will be, respectively.

#!/usr/bin/env python
#########################################
#       nii2png for Python 3.7          #
#         NIfTI Image Converter         #
#                v0.2.9                 #
#                                       #
#     Written by Alexander Laurence     #
# http://Celestial.Tokyo/~AlexLaurence/ #
#    [email protected]   #
#              09 May 2019              #
#              MIT License              #
#########################################

import scipy, numpy, shutil, os, nibabel
import sys, getopt
import cv2

import imageio
import os

def main(inputfile, outputfile):
    # inputfile = ''
    # outputfile = ''
    # try:
    #     opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
    # except getopt.GetoptError:
    #     print('nii2png.py -i <inputfile> -o <outputfile>')
    #     sys.exit(2)
    # for opt, arg in opts:
    #     if opt == '-h':
    #         print('nii2png.py -i <inputfile> -o <outputfile>')
    #         sys.exit()
    #     elif opt in ("-i", "--input"):
    #         inputfile = arg
    #     elif opt in ("-o", "--output"):
    #         outputfile = arg

    # print('Input file is ', inputfile)
    # print('Output folder is ', outputfile)

    # set fn as your 4d nifti file
    image_array = nibabel.load(inputfile).get_data()
    print(len(image_array.shape))
    ask_rotate = 'n'

    # ask if rotate
    # ask_rotate = input('Would you like to rotate the orientation? (y/n) ')

    # if ask_rotate.lower() == 'y':
    #     ask_rotate_num = int(input('OK. By 90° 180° or 270°? '))
    #     if ask_rotate_num == 90 or ask_rotate_num == 180 or ask_rotate_num == 270:
    #         print('Got it. Your images will be rotated by {} degrees.'.format(ask_rotate_num))
    #     else:
    #         print('You must enter a value that is either 90, 180, or 270. Quitting...')
    #         sys.exit()
    # elif ask_rotate.lower() == 'n':
    #     print('OK, Your images will be converted it as it is.')
    # else:
    #     print('You must choose either y or n. Quitting...')
    #     sys.exit()

    # if 4D image inputted
    if len(image_array.shape) == 4:
        # set 4d array dimension values
        nx, ny, nz, nw = image_array.shape

        # set destination folder
        if not os.path.exists(outputfile):
            os.makedirs(outputfile)
            # print("Created ouput directory: " + outputfile)

        # print('Reading NIfTI file...')

        total_volumes = image_array.shape[3]
        total_slices = image_array.shape[2]

        # iterate through volumes
        for current_volume in range(0, total_volumes):
            slice_counter = 0
            # iterate through slices
            for current_slice in range(0, total_slices):
                if (slice_counter % 1) == 0:
                    # rotate or no rotate
                    if ask_rotate.lower() == 'y':
                        if ask_rotate_num == 90 or ask_rotate_num == 180 or ask_rotate_num == 270:
                            print('Rotating image...')
                            if ask_rotate_num == 90:
                                data = numpy.rot90(image_array[:, :, current_slice, current_volume])
                            elif ask_rotate_num == 180:
                                data = numpy.rot90(numpy.rot90(image_array[:, :, current_slice, current_volume]))
                            elif ask_rotate_num == 270:
                                data = numpy.rot90(numpy.rot90(numpy.rot90(image_array[:, :, current_slice, current_volume])))
                    elif ask_rotate.lower() == 'n':
                        data = image_array[:, :, current_slice, current_volume]
                            
                    #alternate slices and save as png
                    # print('Saving image...')
                    image_name = inputfile[:-4] + "_t" + "{:0>3}".format(str(current_volume+1)) + "_z" + "{:0>3}".format(str(current_slice+1))+ ".png"
                    imageio.imwrite(image_name, data)
                    # print('Saved.')

                    #move images to folder
                    # print('Moving files...')
                    src = image_name
                    shutil.move(src, outputfile)
                    slice_counter += 1
                    # print('Moved.')

        # print('Finished converting images')

    # else if 3D image inputted
    elif len(image_array.shape) == 3:
        # set 4d array dimension values
        nx, ny, nz = image_array.shape

        # set destination folder
        if not os.path.exists(outputfile):
            os.makedirs(outputfile)
            # print("Created ouput directory: " + outputfile)

        # print('Reading NIfTI file...')

        total_slices = image_array.shape[2]

        slice_counter = 0
        # iterate through slices
        for current_slice in range(0, total_slices):
            # alternate slices
            if (slice_counter % 1) == 0:
                # rotate or no rotate
                # if ask_rotate.lower() == 'y':
                #     if ask_rotate_num == 90 or ask_rotate_num == 180 or ask_rotate_num == 270:
                #         if ask_rotate_num == 90:
                #             data = numpy.rot90(image_array[:, :, current_slice])
                #         elif ask_rotate_num == 180:
                #             data = numpy.rot90(numpy.rot90(image_array[:, :, current_slice]))
                #         elif ask_rotate_num == 270:
                #             data = numpy.rot90(numpy.rot90(numpy.rot90(image_array[:, :, current_slice])))
                # elif ask_rotate.lower() == 'n':
                data = image_array[:, :, current_slice]

                #alternate slices and save as png
                if (slice_counter % 1) == 0:
                    # print('Saving image...')
                    image_namek = inputfile[:-4] + "_z" + "{:0>3}".format(str(current_slice+1))+ ".png"
                    image_name = image_namek.replace('label','image')
                    cv2.imwrite(image_name, data)
                    # print('Saved.')

                    #move images to folder
                    # print('Moving image...')
                    src = image_name
                    shutil.move(src, outputfile)
                    slice_counter += 1
                    # print('Moved.')

        # print('Finished converting images')
    # else:
        # print('Not a 3D or 4D Image. Please try again.')

source = None
destination = None

for (dirpath, dirnames, filenames) in os.walk(source):
    for filename in filenames:
        if 'nii.gz' not in filename: continue
        main(os.path.join(dirpath,filename),destination)
        print(filename)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants