当前位置:网站首页>Simpleitk use - 3 Common operations
Simpleitk use - 3 Common operations
2022-07-02 22:33:00 【Ton ton don't fight wild】
List of articles
1. corrosion / inflation
1.1 python Realization
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")
The effect is similar to the following :
Reference resources :
- Welcome to the SimpleITK Image Filtering Tutorial
- Find it according to the previous reference link :
GrayscaleErodeImageFilter, Go on SimpleITK Online documentation for To search - And find out :itk::simple::GrayscaleErodeImageFilter Class Reference
- These include :GrayscaleErode() This interface .
- If you accidentally forget the meaning of corrosion and expansion , Take a look at this article :opencv Learning notes ( 8、 ... and ): Image morphology operation
- Corrosion is the dark side getting more , Less light , Every time filter Take the minimum value to cover (0-255,0 It's black )
- Expansion is the reduction of darkness , Less light , Every time filter Take the maximum value to cover
- SimpleITK Analyze medical images
- ITK07 Mask processing
- ITK Do corrosive operation , This uses
itkGrayscaleErodeImageFilterThis class
1.2. C++ Realization
If you want to search ITK Some classes of , You can go straight to SimpleITK Documents Search in , Those... Will be shown here ImageFilter Corresponding ITK The documentation in .
for example : Search in the document erosion, You can get the following results , It's better to see which function you need 



There are several more. , I won't put the screenshot here
1.2.1 inflation (Dilate)
Expansion corresponds to ImageFilter yes :
- itk::BinaryDilateImageFilter< TInputImage, TOutputImage, TKernel > Class Template Reference
- Expansive C++ Examples in :SphinxExamples/src/Filtering/BinaryMathematicalMorphology/DilateABinaryImage/Code.cxx
- As follows :
#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);
// Mainly this sentence
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 corrosion (Erode)
Corrosion corresponds to ImageFilter yes :
- [itk::BinaryErodeImageFilter< TInputImage, TOutputImage, TKernel > Class Template Reference](itk::BinaryErodeImageFilter< TInputImage, TOutputImage, TKernel > Class Template Reference)
- Corrosive C++ Examples in :SphinxExamples/src/Filtering/MathematicalMorphology/ErodeBinaryImageUsingFlatStruct/Code.cxx, as well as SphinxExamples/src/Filtering/BinaryMathematicalMorphology/ErodeABinaryImage/Code.cxx
2. SimpleITK Conduct padding operation
My usage scenarios :
- Yes mask The image is done crop, Deleted mask Some unnecessary marking points in
- crop after mask The image size has changed , So we need to change back to crop Previous 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 and Crop It's like , Just one is cutting , One is to add .
Reference resources :
3. SimpleITK Extract a slice
According to the reference 1, You can know ,
- Pure use SimpleITK Of Image Go slice , The code is more complex
- It is suggested to directly turn to numpy Array
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)
# Do some array slicing
....
demo_image =sitk.GetImageFromArray(demo_array)
demo_image.CopyInformation(demo_mask_image )
Reference resources :
- SimpleITK file :Advanced Image Reading
- SimpleITK reading a slice of an image
- Python/ExtractSlice.py
- itk::simple::ExtractImageFilter Class Reference
4. SimpleITK Of GetSize and GetArrayFromImage After xyz Corresponding
Reference resources :【SimpleITK course 】GetSize() Methods and GetArrayFromImage() Method 

namely
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 Threshold osteotomy
The data I use is relatively simple , Here is a simple preprocessing .
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)
# stay 125~2000 Within the scope of , The assignment is 1, Out of range , The assignment is 0. Got mask Namely 125~2000 The content of this intensity value , Bones .
Like the image below :
- Above is the brain CT Adjust the window width and window level to level:700,window:500 The resulting image
- Here are the results of using threshold osteotomy , It looks ok

Reference resources :
边栏推荐
- Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
- "New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
- 分享一下如何制作专业的手绘电子地图
- U++ 学习笔记 ----松弛
- New feature of go1.18: trylock, which has been tossed n times
- Market Research - current situation and future development trend of herringbone gear Market
- From personal heroes to versatile developers, the era of programmer 3.0 is coming
- How to center the positioned text horizontally and vertically
- Unity3d learning notes 4 - create mesh advanced interface
- Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
猜你喜欢

Reading experience of just because

开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...

SimpleITK使用——3. 常见操作

基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习

About test cases

Pointer and string

Daily book -- analyze the pain points of software automation from simple to deep

20220702-程序员如何构建知识体系?

#include<>和#include“”的区别

Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
随机推荐
Reading experience of just because
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
20220702 how do programmers build knowledge systems?
Sql service intercepts string
LxC terminal login method
Micro service gateway selection, please accept my knees!
100 important knowledge points that SQL must master: management transaction processing
UE4 游戏架构 学习笔记
Servicemesh mainly solves three pain points
数据库系统概论第一章简答题-期末考得怎么样?
分享一下如何制作专业的手绘电子地图
[Jianzhi offer] 57 And are two numbers of S
How to write a good program when a big book speaks every day?
U++ 原始内存 学习笔记
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
[shutter] shutter application theme (themedata | dynamic modification theme)
Unity发布WebGL播放声音的一种方法
[leetcode] sword finger offer 04 Search in two-dimensional array
LandingSite eBand B1冒烟测试用例
Bridge emqx cloud data to AWS IOT through the public network