Click here to Skip to main content
15,867,686 members
Articles / Multimedia / Video

Introducing Portable Imaging IO Library for C#

Rate me:
Please Sign up or sign in to vote.
4.89/5 (17 votes)
15 Sep 2015LGPL34 min read 50.2K   42   15
Portable video IO library for C#
Image 1
All image stream readers in a single portable library.

Contents

  1. Introduction
  2. The portable imaging IO library
  3. NuGet package
  4. Conclusion

Introduction

Image 2

.NET Framework lacks does not provide an video IO API. Instead we have to rely on various 3rd party libraries which offer platform-depended solutions. Current image-processing and computer-vision libraries such as AForge.NET, Accord.NET or EmguCV bring many algorithms but they lack unified API for video, camera and image directory reading and writing. The AForge.NET and Accord.NET libraries relay on DirectX which is not portable and the EmguCV merely wraps the OpenCV file and video capture but does not provide the unified API.

DotImaging.IO library is a part of DotImaging Framework providing an unified API for IO video access: web-camera support, various video-format reading / writing, image-directory reader. All operations are stream-like and are abstracted therefore do not depend on actual video source. The library is made in platform-abstract fashion. If you have been using System.IO.Stream class then you will certain be familiar with the DotImaging.IO library.

DotImaging.IO

A figure below shows the unified architecture for imaging IO reading and writing, camera capture and directory reading.

Click on the figure to enlarge the diagram.
Image 3
Class diagram. The figure shows the unified API for the file, camera and directory reading and writing.

The base class that all other classes inherit is ImageStream<TImage> where TImage is the generic image type. This class contains the base stream properties, but you probably will not be using it directly. The more interesting classes, which you are going to use much more often are: ImageStreamReader<TImage> and ImageStreamWriter<TImage>. Those classes have very nice properties:

  1. the class members use the standard names for stream-like classes

    By using familiar names developer does not have to learn anything new.

  2. they are enough for all basic video IO operations

    if you just want shared IO operations which do not include specific type members the ImageStreamReader<TImage> and ImageStreamWriter<TImage> classes are enough for all video IO operations. The provided abstraction layer enables you to write programs that do not handle video stream source types separately which makes the code clean and easy maintainable.

Now, here are some concrete samples!

Reading

For the basic stream operations only ImageStreamReader class is required which is non-generic version of ImageStreamReader<TImage> class. The list below shows how to capture camera and file stream, and read image directory. The ImageStreamReader<TImage> class also implements System.IEnumerable<TImage> interface so you can use any reader inside foreach loop as shown in sample below.

  1. camera
    C#
    //capture from camera
    ImageStreamReader reader = new CameraCapture(cameraIndex: 0);
  2. video
    C#
    //capture from video
    ImageStreamReader reader = new FileCapture("file name.mp4"); 
  3. directory image stream
    C#
    //capture from folder(s)
    ImageStreamReader reader = new ImageDirectoryCapture("Image folder", "*.jpg", recursive: false); 
C#
reader.Open(); //open reader

var singleImage = reader.ReadAs<Bgr<byte>>(); //read one image

//...or read all images
foreach(var image in reader)
{
   //do something with the image
}

//or read to a managed buffer 
Bgr<byte>[,] buffer = null;
reader.ReadTo(ref buffer);

reader.Close(); //close reader

More options

  • More specific options are implemented in concrete classes because they are specific for each IO source. The list below which members can be seen on the provided class diagram. They are also shown below:
  • file - FileCapture
    (inherited from VideoCaptureBase)

  • camera - CameraCapture
    (inherited from VideoCaptureBase) + camera hardware parameters - brightness, contrast, exposure, frame rate, gain, hue, saturation

  • image directory reading - ImageDirectoryCapture
    root directory info (can read recursively), file informations, current image name

Readers also implement the new async pattern so you can enjoy in asynchronously video reading.

C#
Image<Bgr, byte> frame = null;
async void captureFrame()
{
   frame = await capture.ReadAsync(); //reads stream asynchronously
}

Writing

The library also provides the ability to compile videos from images. If you have been using System.IO.StreamWriter, you will know how to use the VideoWriter class.

C#
Image<Bgr, byte> frame = ... //an image

ImageStreamWriter writer = new VideoWriter
                               (
                                 fileName: "output.avi", 
                                 frameSize: new Size(640, 480), 
                                 fps: 30
                               ); //create writer

writer.Open(); //open writer
writer.Write(frame); //write single image 
writer.Close(); //close writer

The VideoWriter class can also accept codec names, but the write operation may fail if the codec is not installed.

NuGet package

Although the implementation uses the unmanaged libraries the provided NuGet package is platform abstract - for now (x86 /x64 - Windows). Depending on the running OS architecture the appropriate unmanaged library is loaded so the developer does not need to handle multiple OS architectures separately. The image below shows the pre-compiled library locations.

Image 4
Pre-compiled libraries. Unmanaged libraries are pre-compiled and loaded on demand depending on OS architecture.

Conclusion

This article presents the portable video IO library which offers unified stream-like interface for file and camera capture and image directory reading. The library also enables video writing. The code contains complete source as well as samples with comments. The library is the part of DotImaging Framework, a generic .NET imaging framework, so do not forget to take a peek :).

History

  • 15 October 2014 - First version released
  • 27 April 2015 - Updated
  • 15 September 2015 - Updated

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
Croatia Croatia
Software Developer - image processing with application in modern browser applications. Author of the freely available libraries:

Comments and Discussions

 
QuestionList of connected webcams Pin
Member 856658218-Oct-15 4:59
Member 856658218-Oct-15 4:59 
AnswerRe: List of connected webcams Pin
Darko Jurić18-Oct-15 10:12
Darko Jurić18-Oct-15 10:12 
QuestionWPF Source to Image Pin
Member 856658223-Sep-15 1:29
Member 856658223-Sep-15 1:29 
AnswerRe: WPF Source to Image Pin
Darko Jurić23-Sep-15 15:53
Darko Jurić23-Sep-15 15:53 
GeneralMy vote of 1 Pin
Dineshshp15-Sep-15 20:56
professionalDineshshp15-Sep-15 20:56 
GeneralRe: My vote of 1 Pin
Darko Jurić16-Sep-15 2:49
Darko Jurić16-Sep-15 2:49 
GeneralMy vote of 5 Pin
joyhen12312315-Sep-15 17:04
joyhen12312315-Sep-15 17:04 
QuestionVideo output data/bit rate Pin
johnds1974uk24-Mar-15 0:40
johnds1974uk24-Mar-15 0:40 
AnswerRe: Video output data/bit rate Pin
Darko Jurić24-Mar-15 1:38
Darko Jurić24-Mar-15 1:38 
GeneralRe: Video output data/bit rate Pin
johnds1974uk25-Mar-15 0:24
johnds1974uk25-Mar-15 0:24 
GeneralRe: Video output data/bit rate Pin
Darko Jurić25-Mar-15 1:24
Darko Jurić25-Mar-15 1:24 
The VideoWriter class is a wrapper around the ffmpeg dll. FFMpeg depends on installed codecs. So, if you do not have the specified codec, you will get an error.

try using:
https://www.ffmpeg.org/

directly. If you succeed,the C# implementation should also work.

Regards,
Darko
GeneralMy vote of 5 Pin
Fernando E. Braz14-Oct-14 19:43
Fernando E. Braz14-Oct-14 19:43 
GeneralRe: My vote of 5 Pin
Darko Jurić14-Oct-14 20:36
Darko Jurić14-Oct-14 20:36 
QuestionDoes it work in Linux? Pin
William Ivanski14-Oct-14 9:30
professionalWilliam Ivanski14-Oct-14 9:30 
AnswerRe: Does it work in Linux? Pin
Darko Jurić14-Oct-14 9:56
Darko Jurić14-Oct-14 9:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.