How to select a ROI on dicomseries?

Hi everyone,
I want to know how to select a ROI on dicomseries? I knew how to read dicomseries and did it for a single dicom. but for dicomseries i have no idea.
For example i have a dicom series,now i want to select a ROI for all of them ,each slice will set the same ROI ,then save as new dicom series. I have readed the example RegionOfInterestImageFilter
It help me work on single dicom .For series I try to change the “reader and writer” to ImageSeriesReader and ImageSeriesWriter,but not work .There must be something here that I don’t know. Someone give me some hits? Thanks!

btw, the example RegionOfInterestImageFilter has a BUG ,it’s missing a line of code: reader->update(). when the pointer reader after getting data

Hello everyone!
I know my problem description is wordy and bad.So here is my new description:
I want to know How to use the “RegionOfInterestImageFilter” on DicomSeries. There is the example for Single Dicom image. And for Series i have no idea about how to set the index and size for region. Or this filiter can` t work on 3D ?Someone give me some hits? Thanks!

RoI filter can be used with 3D images:

start[0] = std::stoi( argv[3] );
start[1] = std::stoi( argv[4] );
start[2] = std::stoi( argv[5] );
size[0] = std::stoi( argv[6] );
size[1] = std::stoi( argv[7] );
size[2] = std::stoi( argv[8] );

Here is an example of how to read a DICOM series as a 3D image and write it as a DICOM series. You need to insert some processing in the middle: add reader->Update(); and then use reader->GetOutput().

Thanks a lot! it works ,but i also have a question.
I was used 2 Points, One Point start[] and another is end[] point .both is 3D point. then set region.SetIndex(start);andregion.SetUpperIndex(end); without using region.setsize(size). It didnt work before.
That means if i use the RoI filter i must set the size argument?

Modify the Size of the ImageRegion so that the provided index will be the upper corner index.

So you don’t have to modify the size directly if you call SetUpperIndex.

Here is the code . The whole code is too long so i pick the ROI Part.

///////////////////////////////////ROI//////////////////////////////////////////////////////////

using FilterType = itk::RegionOfInterestImageFilter< ImageType, ImageType >;//imagetype is 3D

FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());//reader is Seriesreader

CoordinanteTrans(StartPosition[0], StartPosition[1],size, WindowSize);
CoordinanteTrans(EndPosition[0], EndPosition[1],size, WindowSize);
std::cout <<"After CoordinanteTrans:"<< StartPosition[0] <<","<<StartPosition[1]<<std::endl << EndPosition[0] << "," << EndPosition[1] << std::endl;

const auto startx = static_cast<itk::IndexValueType>(StartPosition[0]);
const auto endx = static_cast<itk::IndexValueType>(EndPosition[0]);
const auto starty = static_cast<itk::IndexValueType>(StartPosition[1]);
const auto endy = static_cast<itk::IndexValueType>(EndPosition[1]);

ImageType::IndexType start;
start[0] = startx;
start[1] = starty;
start[2] = 0;        //always from the first Slice z=0

ImageType::IndexType end;
end[0] = endx;
end[1] = endy;
end[2] = size[2];//size[2] is the number of Slices from reader

ImageType::SizeType size2;

//size2[0] = abs(end[0] - start[0]) + 1;
//size2[1] = abs(end[1] - start[1]) + 1;
//size2[2] = size[2];

region.SetIndex(start);
region.SetUpperIndex(end);
//region.SetSize(size2);

filter->SetRegionOfInterest(region);
filter->Update();

const unsigned int firstSlice = start[2];
const unsigned int lastSlice = start[2] + size[2] - 1;
nameGenerator->SetStartIndex(firstSlice);
nameGenerator->SetEndIndex(lastSlice);
nameGenerator->SetIncrementIndex(1);

using WriterType = itk::ImageSeriesWriter<ImageType, Image2DType>;
WriterType::Pointer writer = WriterType::New();
writer->SetInput(filter->GetOutput());
writer->SetImageIO(gdcmImageIO);
writer->SetFileNames(nameGenerator->GetFileNames());
writer->SetMetaDataDictionaryArray(reader->GetMetaDataDictionaryArray());
try
{
	writer->Update();
}
catch (itk::ExceptionObject & error)
{
	std::cerr << "Error: " << error << std::endl;
	return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

If i invoke the region.SetSize(size2); not call the SetUpperIndex then it works…
Do you know why? Thank you again!

I think size2 is greater by +1 than SetUpperIndex. You could inspect it in the debugger.