CT volume data mha jpg format into slices

CT volume data mha jpg format into slices

mha format

.mha file is stored in a format volume data by a description of the data header and the data, general data we get the original medical image is .dcm is dicom file, dicom file contains a wide variety of very complex tag, usually we are only interested in dicom image information, it is generally converted into a plurality of slices will dicom a file, e.g. .mha .mhd files, which contains only a simple description information and body data information to facilitate processing. Look .mha file as shown below

mha file

The upper half is described about the data, such as real size dimensions, voxel data, storing the type of data, the following XXX and the like, is the data part of, the ElementType on the interpretation of the following data is a Short type number, a total of 512 512 58 number of Short type.
.mhd files and .mha file type, but generally divided into .mhd .mha files and file .raw file, corresponding to the two parts .mha file, part of the description, a portion of the data file is pure.

Window width and wide

CT value is generally in the range of -1000 to + 1000, 2000 a plurality of levels, the general display 256 can show the levels (0-255 grayscale), we can not exhibit all of the information (may be even the human eye to distinguish not), it is necessary to set a range, such as in the center of 40, 400 to the width of the range, display image information, i.e., CT values ​​between -160 and 240 to be displayed, are counted as less than -160 -160 , 240 above 240 are counted, i.e. the mapping -160 ~ 240-0 ~ 255, arranged such different window level and window width can display information (CT values ​​of various tissues different) in different tissues.

Jpg convert .mha into slices

Below using opencv and simpleitk, the .mha jpg file into a set of files.

import SimpleITK as sitk
import numpy as np
import cv2
import os

def mha2jpg(mhaPath,outFolder,windowsCenter,windowsSize):
    
    """
    The function can output a group of jpg files by a specified mha file.
    Args:
        mhaPath:mha file path.
        outfolder:The folder that the jpg files are saved.
        windowsCenter:the CT windows center.
        windowsSize:the CT windows size.
    Return:void

    """
    image = sitk.ReadImage(mhaPath)
    img_data = sitk.GetArrayFromImage(image)
    channel = img_data.shape[0]

    if not os.path.exists(outFolder):
        os.makedirs(outFolder)


    low = windowsCenter-windowsSize/2
    high = windowsCenter+windowsSize/2

    for s in range(channel):
        slicer = img_data[s,:,:]
        slicer[slicer<low] = low
        slicer[slicer>high] = high
        slicer = slicer-low
        img = cv2.normalize(slicer, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
        cv2.imwrite(os.path.join(outFolder,str(s)+'.jpg'),img)

def main():
    mha = input("Enter the mha path:")
    out = input("Enter the out folder:")
    wc = int(input("Enter the windows center:"))
    ws = int(input("Enter the windows size:"))
    mha2jpg(mha,out,wc,ws)



if __name__ == "__main__":
    main()

result

Here randomly selected sections converted two sheets, respectively, to select a different window width of window level can be obtained at FIG.

Window width 1 Window width 1

plan

Bloggers are learning the skills and knowledge relevant aspects of medical imaging process, opened a new git, will find useful reusable code lost in, just started, is still relatively empty
https://github.com/MangoWAY/ medicalImageScriptDemo

Guess you like

Origin www.cnblogs.com/WAoyu/p/11819344.html