Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to solve the problem of reading images in common formats

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to use Python to solve common format image reading", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Python to solve common format image reading" article.

1. Medical image data conversion in raw,mhd format

Raw+mhd format is a common medical image format, each patient's data contains a mhd file and a raw file with the same name, mhd is meta header data, the data header information, while raw stores pixel information. Method, and we need to install the corresponding library in our own Python environment

Pip install SimpleITK

The data of mhd+raw is often three-dimensional volume data. We can read the specific information of the data from the mhd file, such as image size, slice size, pixel size and so on. Use sitk.ReadImage () to read the image, and sitk.GetArrayFromImage () to get the image matrix.

The specific code is as follows:

Import osimport SimpleITK as sitkimport matplotlib.pyplot as pltfrom natsort import natsortedfrom tqdmimport tqdmimport cv2import numpy as npmhd_path ='. / xxx.mhd' # mhd file needs to be placed in the same folder as the raw file with the same name data = sitk.ReadImage (mhd_path) # read mhd file # print (data) spacing = data.GetSpacing () # get spacing size img_data = sitk.GetArrayFromImage (data) # get image matrix print (img_data.shape) # number of images Save np.save ('img_data.npy'') according to conversion to npy Np.array (img_data)) # convert the image to png format to save for i in range (img_data.shape [0]): cv2.imwrite ('. / {} .png '.format (I), img_data [I,:]) 2.Medical image data conversion in dicom format

DICOM (Digital Imaging and Communications in Medicine), which means medical digital imaging and communication, is an international standard for medical images and related information (ISO 12052). DICOM is widely used in radiology, cardiovascular imaging and radiation diagnosis and diagnosis equipment (X-ray, CT, nuclear magnetic resonance, ultrasound, etc.), and has been more and more widely used in ophthalmology, dentistry and other medical fields. The medical images of all patients are stored in DICOM file format. Reading dicom images using Python can be done using the pydicom library and the SimpleITK library. Because in the field of medical image processing, different window widths and window levels are needed to export images for different images, I have added the corresponding code in the following code.

Pip install SimpleITKpip install pydicom

The specific code for using the pydicom method is as follows:

Import osimport SimpleITK as sitkimport matplotlib.pyplot as pltfrom natsort import natsortedfrom tqdmimport tqdmimport cv2import pydicomimport numpy as np# adjusts the window width of the image def window_transform (ct_array, window_width, window_center, normal=False): min_window = float (window_center)-0.5*float (window_width) new_img = (ct_array-min_window) / float (window_width) new_ img [new _ img

< 0] = 0 new_img[new_img >

1] = 1 if not normal: new_img = (new_img * 255). Astype ('uint8') return new_imgimg_path =' xx.dcm'# reads all the dicom image information image = pydicom.read_file (img_path) # to get the image matrix image_data = image.pixel_array# to get the patient information in dicom, Information ['PatientID'] = image.PatientIDinformation [' PatientName'] = image.PatientNameinformation ['PatientBirthDate'] = image.PatientBirthDateinformation [' PatientSex'] = image.PatientSexwindow_width = 1000window_center = 30image_data = window_transform (image_data) Window_width, window_center, normal=False) cv2.imwrite ('. / img.png',image_data)

The specific code for using the SimpleITK method is as follows:

Import osimport SimpleITK as sitkimport matplotlib.pyplot as pltfrom natsort import natsortedfrom tqdmimport tqdmimport cv2import pydicomimport numpy as npimg_path = 'xx.dcm'# reads all dicom image information image = sitk.ReadImage (img_path) # into a grayscale image rescalFilt = sitk.RescaleIntensityImageFilter () rescalFilt.SetOutputMaximum (255) rescalFilt.SetOutputMinimum (0) image = rescalFilt.Execute (image) image_data = sitk.GetArrayFromImage (image_data) image_data = np.squeeze (dicom_data) cv2.imwrite ('. / img.png') Image_data) 3. Medical image conversion in nii format

Medical imaging uses the DICOM standard in the early days, and basically all manufacturers will use products that meet the DICOM standard, but this standard is not convenient for data analysis. With the rise of neuroimaging, a variety of data storage standards were born, such as analyze. Later, in order to facilitate academic exchanges, NIH led experts from other organizations to set up a working group to develop a new data storage standard for neuroimaging, called NIFTI. To read NIFTI images with nii suffix, you need to install the Nibel library.

Pip install nibabel

The image data data can be read using the nib.load () function, where dataobj is the image matrix.

Import os import numpy as np import nibabel as nibimport matplotlib.pyplot as pltfrom glob import globimg_path ='. / xxx.nii'image = nib.load (img_path) image_data = image.dataobjfor i in range (0, dataobj.shape [2]): cv2.imwrite ('. / {} .png '.format (I), img_data [I,::]) these are the contents of this article on "how to use Python to solve Image Reading in Common formats" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report