当前位置:网站首页>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 :
边栏推荐
- LandingSite eBand B1冒烟测试用例
- Market Research - current market situation and future development trend of total nutrition products
- Market Research - current market situation and future development trend of genome editing mutation detection kit
- 使用 EMQX Cloud 实现物联网设备一机一密验证
- 20220702 how do programmers build knowledge systems?
- Pointer array parameter passing, pointer parameter passing
- Sql service intercepts string
- Ransack combined condition search implementation
- [shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
- [Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
猜你喜欢

From "bronze" to "King", there are three secrets of enterprise digitalization

"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!

Official announcement! The golden decade of new programmers and developers was officially released

The difference between include < > and include ""

Technical solution of vision and manipulator calibration system

New feature of go1.18: introduce new netip Network Library
![[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)

分享一下如何制作专业的手绘电子地图

Evolution of messaging and streaming systems under the native tide of open source cloud

Introduction to the principle of geographical detector
随机推荐
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
Using emqx cloud to realize one machine one secret verification of IOT devices
Image segmentation using pixellib
Kubernetes resource object introduction and common commands (4)
任务和特权级保护
Official announcement! The golden decade of new programmers and developers was officially released
Web侧防御指南
Etcd raft protocol
Market Research - current market situation and future development trend of aircraft wireless intercom system
100 important knowledge points that SQL must master: management transaction processing
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
ArrayList analysis 2: pits in ITR, listiterator, and sublist
Hanoi Tower problem
UE4 UI自适应屏幕
《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
540. Single element in ordered array
About test cases
From personal heroes to versatile developers, the era of programmer 3.0 is coming
Market Research - current situation and future development trend of preclinical medical device testing service market