当前位置:网站首页>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 :
边栏推荐
- 《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
- 【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
- Servicemesh mainly solves three pain points
- 图像基础概念与YUV/RGB深入理解
- Record the functions of sharing web pages on wechat, QQ and Weibo
- Reading experience of just because
- TinyMCE visual editor adds Baidu map plug-in
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- [QT] QT multithreading development - reentrancy and thread safety
- About test cases
猜你喜欢

SimpleITK使用——3. 常见操作

#include errors detected. Please update your includePath.

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

Oriental Aesthetics and software design

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

Introduction to the principle of geographical detector

Reading experience of just because

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

腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?

SimpleITK使用——4. 奇怪的问题
随机推荐
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
一周生活
Ransack组合条件搜索实现
Introduction to the principle of geographical detector
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
U++ 学习笔记 ----松弛
Sql service intercepts string
Basic concepts of image and deep understanding of yuv/rgb
How do I access the kubernetes API?
Pointer array parameter passing, pointer parameter passing
Learn computer knowledge from scratch
在beforeDestroy中销毁localStorage中的值无效
Market Research - current situation and future development trend of marine clutch Market
[C question set] of V
Evolution of messaging and streaming systems under the native tide of open source cloud
Official announcement! The golden decade of new programmers and developers was officially released
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
Market Research - current situation and future development trend of cell-based seafood market
phpcms实现订单直接支付宝支付功能
Market Research - current market situation and future development trend of aircraft audio control panel system