One: ITK function
The main functions of ITK and OpenCV are similar, both of which are oriented to image processing, but there are certain differences in the field of application between the two; ITK is mainly used for segmentation and registration of medical images (c++ and python versions are available). OPENCV is the basis of computer vision (with c++ and python versions), it can be said that OPENCV will definitely be used in two-dimensional image algorithms. Many pre-processing tasks for machine learning and deep learning are also done by opencv.
Two: compile
environment:
Windows10
VS2019
Download the ITK software package from the official website:
Create the installation directory:
And create build and bin folders in ITK5.2.0
Start compiling with Cmake:
source code: Change the directory to the previously downloaded itk decompression file.
binaries: The directory is the previously built build folder.
Click configure to run.
Then choose your own corresponding configuration:
After the above is over, a bunch of red will pop up, and then check advanced.
BUILD_SHARED_LIBS is off by default, which means that the library is statically compiled and only lib is generated. If selected, it will be dynamically compiled and a dll file will be generated (selected here)
Find the CMAKE_INSTALL_PREFIX option, which indicates the installation path of ITK (here changed to the bin folder we built before). Then click configure again to run.
Run until there is no red, if there is still red, click and run configure several times.
Then generate, click to run generate.
Finally, generating done will be displayed (proving that the generation was successful).
Three: Installation
Open VS2019 as administrator, then open ITK.sln
Find ALL_BUILD in the resource manager, right click to generate
If you follow the process, generally no errors will be reported.
Then find INSTALL and right-click to generate it to start the installation.
Four: Environment configuration
First, add the bin file under the previously created bin folder to the environment variable path of the system, and then restart the computer.
Then create a new project with vs2019, add bin\include\ITK-5.2 in the VC++ directory---include directory
Add bin\lib to the VC++ directory---library directory
Add many .lib files in the connector --- input --- additional dependencies (that is, all the file names ending in .lib in the bin\lib folder).
Quickly get the name of the .lib file, create a new txt file under the bin\lib folder, and enter it
DIR *.lib* /B >LIST.TXT Then change the suffix to .bat to run the file. Double-click to run, and a LIST.TXT file will be generated, which contains all the .lib file names.
Add all .libs in the LIST.TXT file to Additional Dependencies.
The above environment is configured.
Five: Read the DICOM sequence and get the value quickly.
#include<iostream>
#include "itkGDCMImageIO.h"
#include "itkImageSeriesReader.h"//用于读取图像序列
#include "itkGDCMSeriesFileNames.h"//用于读取序列文件名
using namespace std;
int main()
{
string file = "./GTI5BZZQ";//dicom文件夹
//初始化待读取的dicom序列的各种参数
using PixelType = signed short;//像素数据类型
constexpr unsigned int dims = 3;//3维
using ImageType = itk::Image<PixelType, dims>;
using ReaderType = itk::ImageSeriesReader<ImageType>;
using ImageIOType = itk::GDCMImageIO;
using NamesGeneratorType = itk::GDCMSeriesFileNames;
//设置IO,并获取文件名
ImageIOType::Pointer gdcmIO = ImageIOType::New();
NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
namesGenerator->SetInputDirectory(file);
const ReaderType::FileNamesContainer& filenames = namesGenerator->GetInputFileNames();
//输出文件名
for (int i = 0; i < filenames.size(); i++)
{
cout << filenames[i] << endl;
}
//读取dcm序列
ReaderType::Pointer reader = ReaderType::New();
reader->SetImageIO(gdcmIO);
reader->SetFileNames(filenames);
reader->Update();
//通过直接读取内存的方式获取像素值(高效率
short* ptr = reader->GetOutput()->GetBufferPointer();//将获得首地址的指针
//只需偏移就可以获得任何位置像素
cout << ptr[0] << endl;
gdcmIO->Delete();
namesGenerator->Delete();
reader->Delete();
return 0;
}