# Reading In Videofluoroscopy (VFS) AVIs, Converting to DICOM Series?

**URL:** https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692
**Category:** Uncategorized
**Tags:** itk, python
**Created:** [June 11, 2024, 6:20am UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692 "2024-06-11T06:20:34Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [June 11, 2024, 6:20am UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/1 "2024-06-11T06:20:34Z")

</div>

Hello Everyone.

I have a bunch of Videofluoroscopy (VFS) studies exported off the scanner in .avi format…

I’m not surprised to see `itk.imread('movi_xyz.avi')` in Python to tell me:

`RuntimeError: Could not create IO object for reading file movi_xyz.avi`

I’m wondering, what’s the shortest path from .avi to DICOM series — I’m going to have to script this is Python.

Kind regards,  
Constantine

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [June 11, 2024, 1:46pm UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/2 "2024-06-11T13:46:28Z")

</div>

Use OpenCV to read in the AVI file. Convert it to a Numpy array. Then use SimpleITK to convert it to a series of SimpleITK images and write out that DICOM.

---

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [June 12, 2024, 4:10am UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/3 "2024-06-12T04:10:08Z")

</div>

Thank you, @dchen. Sounds easy enough.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [June 12, 2024, 4:17am UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/4 "2024-06-12T04:17:35Z")

</div>

As my advisor once said to me “It’s a simple matter of code.” I wanted to kill him at that point.

---

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [June 12, 2024, 4:28am UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/5 "2024-06-12T04:28:35Z")

</div>

Yup, and now that I’m writing the code…

Would you tell me, I still have to do this frame-by-frame, when using OpenCV, right?

And then, frame-by-frame (converted to numpy array) use SimpleITK to write as DICOM?

Maybe you can link me to an example, please?

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [June 12, 2024, 2:39pm UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/6 "2024-06-12T14:39:42Z")

</div>

OK, I asked Google’s AI to write the code for me. Here’s the result:

```auto

import cv2
import SimpleITK as sitk

# Define the video file path
video_path = "video_1_75ff58251edb441e99ebbcba22c2641c.mp4"

# Create a video capture object
cap = cv2.VideoCapture(video_path)

# Check if the video capture object is opened successfully
if not cap.isOpened():
    print("Error opening video stream or file")
    exit()

# List to store the numpy arrays of the video frames
frames = []

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()

    # If the frame is read correctly retrieve frame as a numpy array
    if ret:
        frames.append(frame)

        # Press 'q' to quit
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Break the loop if there is an error in reading
    else:
        break

# Release the video capture object
cap.release()

# Print the total number of frames in the video
print(f"Total number of frames in video: {len(frames)}")

# You can now access each frame as a numpy array using the 'frames' list
#
for f in frames:
    print()
    print(type(f))
    print(f.shape)

    img = sitk.GetImageFromArray(f)
    print(type(img))
    print(img.GetSize())

```

I only wrote the loop at the end. And the code worked. We’ll all be out of jobs soon.

And if you want to see how to write a DICOM series from a 3d image, here’s SimpleITK’s example:  
[https://simpleitk.readthedocs.io/en/master/link\_DicomSeriesReadModifyWrite\_docs.html](https://simpleitk.readthedocs.io/en/master/link_DicomSeriesReadModifyWrite_docs.html)

---

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [June 13, 2024, 2:15am UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/7 "2024-06-13T02:15:40Z")

</div>

@dchen Dave — thank you very much for being so forthcoming with the answers and tips! And yes, it is important to know how to ask Google AI nicely…

I’ve never played with SimpleITK, but some things may be… well, simple enough in it, like the sitk.GetImageFromArray (static?) method.

Is there an equivalent in the (plain) ITK — or, given a (numpy) array/buffer, is there a way to set image data from a buffer in a regular ITK image, like in itk::ImportImageContainer::SetImportPointer(…)?

---

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [June 13, 2024, 2:25am UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/8 "2024-06-13T02:25:36Z")

</div>

It was from a while back — I remember that ITK offers itk::VideoFileReader and it was my (now seems unjustified) assumption that Python ITK wrapping may expose the class.

What is the easy way to discover what is and what isn’t Python-wrapped in ITK?

Now I’m faced with a choice of giving up on ITK Python wrapping and going back to C++, to be able to access full feature set and documentation. I find lack of Python ITK wrapped documentation a big gap and stumbling block, where I need to feel my way in the dark. Or else, I’d be very glad to find that I’m doing something wrong is this process.

---

<div class="post-metadata">

### Author: ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)
#### Post date: [June 13, 2024, 2:54am UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/9 "2024-06-13T02:54:41Z")

</div>

Yes, ITK has a similar function, itk.image\_from\_array.

---

<div class="post-metadata">

### Author: ![constantine](https://discourse.itk.org/user_avatar/discourse.itk.org/constantine/32/3915_2.png) [@constantine](https://discourse.itk.org/u/constantine)
#### Post date: [June 14, 2024, 12:03am UTC](https://discourse.itk.org/t/reading-in-videofluoroscopy-vfs-avis-converting-to-dicom-series/6692/10 "2024-06-14T00:03:43Z")

</div>

Ah, Dave (@dchen) — thank you! Yes, in this case it does look like I’ve fallen short in the RTFM department — I can see this `itk.image_from_array` mentioned on the ITK Python Quick Start guide page, about ⅓ of the way through. Shame on me. 🫢

Although, now it is aliased to `itk::GetImageFromArray`.

I’m still wondering though how to discover ITK Python API in the absence of full reference/full-text-search.

For now I’ve built my own full-text-search:

```auto
import re, itk
print(', '.join([s for s in dir(itk) if 'imagefromarray' in re.sub(r'[\W_]+', '', s.lower())]))

```

To return this sort of thing:

`GetImageFromArray, image_from_array`

Probably should turn this into a function to be loaded on start-up…
