当前位置:网站首页>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 :
边栏推荐
- TinyMCE visual editor adds Baidu map plug-in
- Market Research - current situation and future development trend of anterior cruciate ligament (ACL) reconstruction Market
- How do I access the kubernetes API?
- [shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
- 【C 题集】of Ⅴ
- C语言,实现三子棋小游戏
- [Jianzhi offer] 57 And are two numbers of S
- What is it that makes you tremble? Those without fans can learn
- Les trois principaux points de douleur traités par servicemesh
- [leetcode] sword finger offer 11 Rotate the minimum number of the array
猜你喜欢

Share how to make professional hand drawn electronic maps

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

#include errors detected. Please update your includePath.

Gee: (II) resampling the image
![[shutter] shutter resource file use (import resource pictures | use image resources)](/img/e9/94ae2e3ee315f490eb3cf14bcf2e49.jpg)
[shutter] shutter resource file use (import resource pictures | use image resources)

开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
![[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)
![[shutter] shutter application theme (themedata | dynamic modification theme)](/img/77/6b0082368943aee7108ac550141f28.gif)
[shutter] shutter application theme (themedata | dynamic modification theme)

The failure rate is as high as 80%. What should we do about digital transformation?

How do I access the kubernetes API?
随机推荐
New feature of go1.18: trylock, which has been tossed n times
Error in PIP installation WHL file: error: is not a supported wheel on this platform
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
APP页面分享口令Rails实现
Promise optimized callback hell
情感计算与理解研究发展概述
UE4 UI自适应屏幕
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
SQL必需掌握的100个重要知识点:管理事务处理
Reading experience of just because
LightGBM原理及天文数据中的应用
攻防世界pwn题:Recho
Unity3D学习笔记4——创建Mesh高级接口
Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision
《Just because》阅读感受
Infrastructure is code: a change is coming
Market Research - current situation and future development trend of anti-counterfeiting label market
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
[shutter] shutter gesture interaction (small ball following the movement of fingers)
一周生活