Hi All,
I have a set of dicom images. I can open these images using pydicom
image_slice_one = pydicom.read_file('CT_1.dcm') # slice 1
image_slice_two = pydicom.read_file('CT_2.dcm') # slice 2
image_slice_three = pydicom.read_file('CT_3.dcm') # etc
This works and I can even visualize them using matplotlib. So far, so good!
Now, I have a separate .dcm file which contains contours around Regions of Interest. (ROI) I can also open that file,
rt_struct = pydicom.dcmread('RTStructure.dcm') #dicom file that contains the contours
and I see that it contains the locations of the contours, for example
rt_struct.ROIContourSequence[1].ContourSequence[0]
rt_struct.ROIContourSequence[1].ContourSequence[1]
rt_struct.ROIContourSequence[2].ContourSequence[55] #etc
Great!
But how I do know which image (dicom file / slice mentioned above) each one of these ContourSequences refers to?
When I use dicompyler (a dicom viewer), I can see the contours overlayed on the images, so clearly the viewer is able to figure out which contoursequence is associated with which image. When I scroll up/down to different slices, the viewer updates the contours (in green in the image below) accordingly.

I want to do the same thing in python. How do I know which belongs to which image? Is there a way of easily doing this? Maybe there is a function that does this. Or there must be some metadata hidden somewhere that is allowing the dicom viewer program to know which contour belongs to which image?
My ultimate goal: to select the pixels in these images that fall within the contours and find their average brightness.
I am new to dicom and pydicom and will greatlly appreciate any insights. My guess is that this is a common need so there is an easy way to do it.
Thank you very much in advance!
Semihcan