当前位置:网站首页>ITK learning notes (VII) the position of ITK rotation direction remains unchanged
ITK learning notes (VII) the position of ITK rotation direction remains unchanged
2022-07-03 23:50:00 【juluwangriyue】
# -*- coding : UTF-8 -*-
# @file : resample_change_direction.py
# @Time : 2022-02-14 16:19
# @Author : wmz
import os
import numpy as np
import SimpleITK as sitk
def getFiles(path, suffix):
return [os.path.join(root, file) for root, dirs, files in os.walk(path) for file in files if file.endswith(suffix)]
def resampleVolume(outspacing, vol):
""" Resample volume data to the specified spacing size \n paras: outpacing: designated spacing, for example [1,1,1] vol:sitk Read the image Information , Here is volume data \n return: Resampled data """
outsize = [0, 0, 0]
# Read the file size and spacing Information
inputsize = vol.GetSize()
inputspacing = vol.GetSpacing()
transform = sitk.Transform()
transform.SetIdentity()
# Calculation changes spacing After size, Use physical dimensions / Voxel size
outsize[0] = round(inputsize[0] * inputspacing[0] / outspacing[0])
outsize[1] = round(inputsize[1] * inputspacing[1] / outspacing[1])
outsize[2] = round(inputsize[2] * inputspacing[2] / outspacing[2])
# Set some parameters of resampling
resampler = sitk.ResampleImageFilter()
resampler.SetTransform(transform)
resampler.SetInterpolator(sitk.sitkLinear)
resampler.SetOutputOrigin(vol.GetOrigin())
resampler.SetOutputSpacing(outspacing)
resampler.SetOutputDirection(vol.GetDirection())
resampler.SetSize(outsize)
newvol = resampler.Execute(vol)
return newvol
def resample_direction(itk_img, direction=tuple([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0])):
""" Resample volume data to the specified spacing size \n paras: outpacing: designated spacing, for example [1,1,1] vol:sitk Read the image Information , Here is volume data \n return: Resampled data """
outsize = [0, 0, 0]
# Read the file size and spacing Information
img_size = itk_img.GetSize()
spacing = itk_img.GetSpacing()
transform = sitk.Transform()
transform.SetIdentity()
# Calculation changes spacing After size, Use physical dimensions / Voxel size
outsize[0] = round(img_size[0])
outsize[1] = round(img_size[1])
outsize[2] = round(img_size[2])
origin = itk_img.GetOrigin()
new_origin = tuple([origin[0] - spacing[0] * img_size[0], origin[1] - spacing[1] * img_size[1], origin[2]])
# Set some parameters of resampling
resampler = sitk.ResampleImageFilter()
# resampler.SetReferenceImage(itk_img)
resampler.SetTransform(transform)
resampler.SetInterpolator(sitk.sitkLinear)
resampler.SetSize(outsize)
resampler.SetOutputOrigin(new_origin)
resampler.SetOutputDirection(direction)
resampler.SetOutputSpacing(spacing)
new_img = resampler.Execute(itk_img)
out_arr = sitk.GetArrayFromImage(new_img)
return new_img
def resample_image(itk_image, out_spacing,out_direction, is_label=False):
original_size = itk_image.GetSize()
original_spacing = itk_image.GetSpacing()
out_size = [
int(np.round(original_size[0] * (original_spacing[0] / out_spacing[0]))),
int(np.round(original_size[1] * (original_spacing[1] / out_spacing[1]))),
int(np.round(original_size[2] * (original_spacing[2] / out_spacing[2])))
]
resample = sitk.ResampleImageFilter()
resample.SetOutputSpacing(out_spacing)
resample.SetSize(out_size)
resample.SetOutputDirection(out_direction)
resample.SetOutputOrigin(itk_image.GetOrigin())
if is_label:
resample.SetDefaultPixelValue(0) # Fill in the value where there is no image
resample.SetInterpolator(sitk.sitkNearestNeighbor)
else:
resample.SetDefaultPixelValue(-10) # -10 I adjusted the window width out of the window
resample.SetInterpolator(sitk.sitkBSpline)
return resample.Execute(itk_image)
if __name__ == '__main__':
img_fath = r"D:\Dataset\landmark\Data_train_scaled_2"
filelist = getFiles(img_fath, "nii.gz")
dst_path = r"D:\Dataset\landmark\resample_direction_train"
for imgfile in filelist:
org_img = sitk.Image(sitk.ReadImage(imgfile))
direct = org_img.GetDirection()
direction = tuple([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0])
spacing = org_img.GetSpacing()
img_size = org_img.GetSize()
img_arr = sitk.GetArrayFromImage(org_img)
out_img = resample_direction(org_img, direction)
# out_img = resample_image(org_img, spacing, direction)
# out_img = sitk.GetImageFromArray(sitk.GetArrayFromImage(org_img))
# out_arr = sitk.GetArrayFromImage(out_img)
# out_arr = np.flip(out_arr, [1, 2])
# out_img = sitk.GetImageFromArray(out_arr)
# # setup other image characteristics
# origin = org_img.GetOrigin()
# new_origin = tuple([origin[0] - spacing[0] * img_size[0], origin[1] - spacing[1] * img_size[1], origin[2]])
# # out_img.SetOrigin(org_img.GetOrigin())
# out_img.SetOrigin(new_origin)
# out_img.SetSpacing(org_img.GetSpacing())
# set to RAI
out_img.SetDirection(tuple([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]))
out_arr = sitk.GetArrayFromImage(out_img)
dst_file = os.path.join(dst_path, imgfile.rsplit("\\")[-1])
sitk.WriteImage(out_img, dst_file)
print("saved file: ", dst_file)
边栏推荐
- Gorilla/mux framework (RK boot): add tracing Middleware
- The first game of the new year, many bug awards submitted
- Selenium library 4.5.0 keyword explanation (4)
- Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Gossip about redis source code 82
- 炒股开户佣金优惠怎么才能获得,网上开户安全吗
- Correlation analysis summary
- Gossip about redis source code 75
- Idea integrates Microsoft TFs plug-in
猜你喜欢
Fluent learning (4) listview
Zipper table in data warehouse (compressed storage)
leetcode-43. String multiplication
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Alibaba cloud container service differentiation SLO hybrid technology practice
How to understand the gain bandwidth product operational amplifier gain
How to write a good title of 10w+?
2022 a special equipment related management (elevator) examination questions and a special equipment related management (elevator) examination contents
随机推荐
Vscode regular match replace console log(.*)
C # basic knowledge (2)
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
D29:post Office (post office, translation)
SPI based on firmware library
Zipper table in data warehouse (compressed storage)
Gossip about redis source code 80
Learning methods of zynq
Unity shader visualizer shader graph
炒股開戶傭金優惠怎麼才能獲得,網上開戶安全嗎
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
炒股开户佣金优惠怎么才能获得,网上开户安全吗
Generic tips
Cgb2201 preparatory class evening self-study and lecture content
How to write a good title of 10w+?
股票开户佣金最低的券商有哪些大家推荐一下,手机上开户安全吗
[Mongodb] 2. Use mongodb --------- use compass
Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii
D24:divisor and multiple (divisor and multiple, translation + solution)