当前位置:网站首页>SimpleITK使用——3. 常见操作
SimpleITK使用——3. 常见操作
2022-07-02 22:01:00 【吨吨不打野】
文章目录
1. 腐蚀/膨胀
1.1 python实现
import SimpleITK as sitk
demo_mask_path = "./seg.nii.gz"
demo_mask_image = sitk.ReadImage(demo_mask_path)
dilate_img=sitk.GrayscaleDilate(demo_mask_image , kernelRadius=[3,3,3])
sitk.WriteImage(dilate_img, "./dilatenii.gz")
效果类似下面这样:
参考:
- Welcome to the SimpleITK Image Filtering Tutorial
- 根据上一参考链接找到:
GrayscaleErodeImageFilter,进而去SimpleITK的在线文档进行搜索 - 进而找到:itk::simple::GrayscaleErodeImageFilter Class Reference
- 其中包括:GrayscaleErode()这个接口。
- 如果不小心忘了腐蚀和膨胀的意义,可以看看这个文章:opencv学习笔记(八):图像形态学操作
- 腐蚀是暗部变多,亮部变少,每次filter取最小值来覆盖(0-255,0是黑色)
- 膨胀是暗部变少,亮部变少,每次filter取最大值来覆盖
- SimpleITK等解析医学图像
- ITK07 掩膜处理
- ITK做腐蚀操作,这个使用了
itkGrayscaleErodeImageFilter这个类
1.2. C++实现
如果想搜索ITK的某些类,可以直接去SimpleITK的文档里去搜索,这里会显示那些ImageFilter的对应ITK中的说明文档。
例如:去文档中搜索erosion,可以得到以下结果,还是看一下哪个是自己需要的功能比较好



还有几个,这里就不放截图了
1.2.1 膨胀(Dilate)
膨胀对应的ImageFilter是:
- itk::BinaryDilateImageFilter< TInputImage, TOutputImage, TKernel > Class Template Reference
- 膨胀的C++示例位于:SphinxExamples/src/Filtering/BinaryMathematicalMorphology/DilateABinaryImage/Code.cxx
- 具体如下:
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkFlatStructuringElement.h"
#include "itkBinaryDilateImageFilter.h"
int main(int argc, char * argv[])
{
if (argc < 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " <inputImage> <outputImage> <radius>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputImage = argv[1];
const char * outputImage = argv[2];
const unsigned int radiusValue = std::stoi(argv[3]);
using PixelType = unsigned char;
constexpr unsigned int Dimension = 2;
using ImageType = itk::Image<PixelType, Dimension>;
const auto input = itk::ReadImage<ImageType>(inputImage);
using StructuringElementType = itk::FlatStructuringElement<Dimension>;
StructuringElementType::RadiusType radius;
radius.Fill(radiusValue);
StructuringElementType structuringElement = StructuringElementType::Ball(radius);
// 主要是这句
using BinaryDilateImageFilterType = itk::BinaryDilateImageFilter<ImageType, ImageType, StructuringElementType>;
BinaryDilateImageFilterType::Pointer dilateFilter = BinaryDilateImageFilterType::New();
dilateFilter->SetInput(input);
dilateFilter->SetKernel(structuringElement);
dilateFilter->SetForegroundValue(255); // Value to dilate
try
{
itk::WriteImage(dilateFilter->GetOutput(), outputImage);
}
catch (itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
1.2.2 腐蚀(Erode)
腐蚀对应的ImageFilter是:
- [itk::BinaryErodeImageFilter< TInputImage, TOutputImage, TKernel > Class Template Reference](itk::BinaryErodeImageFilter< TInputImage, TOutputImage, TKernel > Class Template Reference)
- 腐蚀的C++示例位于:SphinxExamples/src/Filtering/MathematicalMorphology/ErodeBinaryImageUsingFlatStruct/Code.cxx,以及SphinxExamples/src/Filtering/BinaryMathematicalMorphology/ErodeABinaryImage/Code.cxx
2. SimpleITK进行padding操作
我的使用场景:
- 对mask图像做了crop,删除了mask中一些不需要的标记点
- crop之后mask图像大小发生了变化,因此需要变回到crop之前的size
import SimpleITK as sitk
demo_mask_path = "./seg.nii.gz"
demo_mask_image = sitk.ReadImage(demo_mask_path)
cropped_image = sitk.Crop(demo_mask_image ,lowerBoundaryCropSize=[0,0,0],upperBoundaryCropSize=[crop_size,0,0])
half_image_y_pad =sitk.ConstantPad(cropped_image ,padLowerBound=[0,0,0],padUpperBound=[crop_size,0,0],constant=0)
ConstantPad和Crop很像,只是一个是裁剪,一个是补充。
参考:
3. SimpleITK提取某个切片
根据参考1,可以知道,
- 纯使用SimpleITK的Image去进行切片,代码比较复杂
- 建议还是直接转为numpy数组进行
import SimpleITK as sitk
demo_mask_path = "./seg.nii.gz"
demo_mask_image = sitk.ReadImage(demo_mask_path)
demo_array = sitk.GetArrayFromImage(demo_mask_image)
# 进行一些数组切片操作
....
demo_image =sitk.GetImageFromArray(demo_array)
demo_image.CopyInformation(demo_mask_image )
参考:
- SimpleITK 文档:Advanced Image Reading
- SimpleITK reading a slice of an image
- Python/ExtractSlice.py
- itk::simple::ExtractImageFilter Class Reference
4. SimpleITK的GetSize和GetArrayFromImage后的xyz对应
参考:【SimpleITK教程】GetSize()方法和GetArrayFromImage()方法

即
padding_path="XXXX.nii.gz"
padding_image=sitk.ReadImage(padding_path)
print(padding_image.GetSize())
padding_array = sitk.GetArrayFromImage(padding_image)
print(padding_array.shape)
padding_array[78,131,80]
> (240, 240, 155) (x,y,z)
(155, 240, 240) (z,y,x)
2
5. SimpleITK阈值去骨
我使用的数据比较简单,这里也就是个简单的预处理。
import SimpleITK as sitk
image_path = "./im.nii.gz"
image = sitk.ReadImage(image_path)
skull_mask =sitk.BinaryThreshold(image, lowerThreshold=125, upperThreshold=2000, insideValue=1, outsideValue=0)
# 在125~2000范围内的,赋值为1,范围外的,赋值为0。得到的mask就是125~2000这个强度值的内容,即骨骼。
类似下图:
- 上面是将脑部CT窗宽窗位调整至level:700,window:500得到的图像
- 下面是使用阈值去骨后的结果,看起来还行

参考:
边栏推荐
- Using emqx cloud to realize one machine one secret verification of IOT devices
- [shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
- LightGBM原理及天文数据中的应用
- Introduction to victoriametrics
- Try to get property'num for PHP database data reading_ rows' of non-object?
- Introduction to the principle of geographical detector
- U++ 原始内存 学习笔记
- 100 important knowledge points that SQL must master: management transaction processing
- Sql service intercepts string
- 20220702-程序员如何构建知识体系?
猜你喜欢

Introduction to the principle of geographical detector

Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
![[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)](/img/07/6f2dfb543cb0ab4f27169da7e6ad07.jpg)
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)

From personal heroes to versatile developers, the era of programmer 3.0 is coming

#include errors detected. Please update your includePath.
![[staff] Sibelius 7.5.1 score software installation (software download | software installation)](/img/1a/4932a7931c54248c065cf8a1462d34.jpg)
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
![[001] [arm-cortex-m3/4] internal register](/img/49/a0eceac1a67267216dd9b2566033a1.jpg)
[001] [arm-cortex-m3/4] internal register

Daily book - low code you must understand in the era of digital transformation

Official announcement! The golden decade of new programmers and developers was officially released
![[shutter] shutter gesture interaction (small ball following the movement of fingers)](/img/5a/a8dad8a0943645c980cc4fe7cb55d4.gif)
[shutter] shutter gesture interaction (small ball following the movement of fingers)
随机推荐
Perceptron model and Application
Error in PIP installation WHL file: error: is not a supported wheel on this platform
使用 EMQX Cloud 实现物联网设备一机一密验证
Web side defense Guide
Pointer - function pointer
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
技术人创业:失败不是成功,但反思是
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Les trois principaux points de douleur traités par servicemesh
Technical solution of vision and manipulator calibration system
[QT] QT multithreading development - four methods to realize multithreading design
服务可见可观测性
Web侧防御指南
Market Research - current market situation and future development trend of third-party data platform
PHP微信抢红包的算法
Market Research - current situation and future development trend of anterior cruciate ligament (ACL) reconstruction Market
PIP audit: a powerful security vulnerability scanning tool
【C 题集】of Ⅴ
[leetcode] sword finger offer 11 Rotate the minimum number of the array