# How to select a ROI on dicomseries?

**URL:** https://discourse.itk.org/t/how-to-select-a-roi-on-dicomseries/2945
**Category:** Beginner Questions
**Tags:** imageseries, dicom, image, roi
**Created:** [April 15, 2020, 1:10pm UTC](https://discourse.itk.org/t/how-to-select-a-roi-on-dicomseries/2945 "2020-04-15T13:10:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.itk.org/user_avatar/discourse.itk.org/amnesie/32/1341_2.png) [@Amnesie](https://discourse.itk.org/u/Amnesie)
#### Post date: [April 15, 2020, 1:10pm UTC](https://discourse.itk.org/t/how-to-select-a-roi-on-dicomseries/2945/1 "2020-04-15T13:10:03Z")

</div>

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](https://itk.org/Doxygen50/html/Examples_2IO_2ImageReadRegionOfInterestWrite_8cxx-example.html) 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](https://itk.org/Doxygen50/html/Examples_2IO_2ImageReadRegionOfInterestWrite_8cxx-example.html) RegionOfInterestImageFilter has a BUG ,it’s missing a line of code: `reader->update()`. when the pointer reader after getting data

---

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.itk.org/user_avatar/discourse.itk.org/amnesie/32/1341_2.png) [@Amnesie](https://discourse.itk.org/u/Amnesie)
#### Post date: [April 20, 2020, 3:54pm UTC](https://discourse.itk.org/t/how-to-select-a-roi-on-dicomseries/2945/2 "2020-04-20T15:54:11Z")

</div>

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](https://itk.org/Doxygen50/html/Examples_2IO_2ImageReadRegionOfInterestWrite_8cxx-example.html) 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!

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 20, 2020, 5:04pm UTC](https://discourse.itk.org/t/how-to-select-a-roi-on-dicomseries/2945/3 "2020-04-20T17:04:33Z")

</div>

RoI filter can be used with 3D images:

```cpp
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](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/IO/DicomSeriesReadSeriesWrite.cxx) 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()`.

---

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.itk.org/user_avatar/discourse.itk.org/amnesie/32/1341_2.png) [@Amnesie](https://discourse.itk.org/u/Amnesie)
#### Post date: [April 21, 2020, 10:29am UTC](https://discourse.itk.org/t/how-to-select-a-roi-on-dicomseries/2945/4 "2020-04-21T10:29:33Z")

</div>

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);`and`region.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?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 21, 2020, 1:59pm UTC](https://discourse.itk.org/t/how-to-select-a-roi-on-dicomseries/2945/5 "2020-04-21T13:59:46Z")

</div>

> [@Amnesie](#):
>
> SetUpperIndex

_Modify the [Size](https://itk.org/Doxygen/html/structitk_1_1Size.html) of the [ImageRegion](https://itk.org/Doxygen/html/classitk_1_1ImageRegion.html) 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`.

---

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.itk.org/user_avatar/discourse.itk.org/amnesie/32/1341_2.png) [@Amnesie](https://discourse.itk.org/u/Amnesie)
#### Post date: [April 21, 2020, 10:28pm UTC](https://discourse.itk.org/t/how-to-select-a-roi-on-dicomseries/2945/6 "2020-04-21T22:28:43Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 21, 2020, 10:34pm UTC](https://discourse.itk.org/t/how-to-select-a-roi-on-dicomseries/2945/7 "2020-04-21T22:34:35Z")

</div>

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