Click here to Skip to main content
13,141,286 members (77,498 online)
Click here to Skip to main content
Add your own
alternative version

Stats

23.7K views
41 bookmarked
Posted 14 Oct 2014

Introducing Portable Imaging IO Library for C#

, 15 Sep 2015
Rate this:
Please Sign up or sign in to vote.
Portable video IO library for C#
All image stream readers in a single portable library.

Contents

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

Introduction

.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.
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
    //capture from camera
    ImageStreamReader reader = new CameraCapture(cameraIndex: 0);
  2. video
    //capture from video
    ImageStreamReader reader = new FileCapture("file name.mp4"); 
  3. directory image stream
    //capture from folder(s)
    ImageStreamReader reader = new ImageDirectoryCapture("Image folder", "*.jpg", recursive: false); 
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.

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.

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.

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)

Share

About the Author

Darko Jurić
Software Developer Faculty of Electrical Engineering and Computing
Croatia Croatia
Darko Jurić received his BSc and MSc degree from the Faculty of Electrical Engineering and Computing in Zagreb. He finished his undergraduate study program in Computing in 2010 and in 2012 his master study program in Computer Science with a specialization in Image processing and analysis. Currently, he is employed at the Faculty of Electrical Engineering and Computing, University of Zagreb, as a research engineer at the Department of Electronic Systems and Information Processing within Image Processing Group, where he is pursuing his PhD degree. His research interests are in the areas of image processing and machine learning which is used for solving problems in computer vision. Author of the Accord.NET Extensions Framework.

You may also be interested in...

Comments and Discussions

 
QuestionList of connected webcams Pin
Member 856658218-Oct-15 4:59
memberMember 856658218-Oct-15 4:59 
AnswerRe: List of connected webcams Pin
Darko Jurić18-Oct-15 10:12
memberDarko Jurić18-Oct-15 10:12 
QuestionWPF Source to Image Pin
Member 856658223-Sep-15 1:29
memberMember 856658223-Sep-15 1:29 
AnswerRe: WPF Source to Image Pin
Darko Jurić23-Sep-15 15:53
memberDarko 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
memberDarko Jurić16-Sep-15 2:49 
GeneralMy vote of 5 Pin
joyhen12312315-Sep-15 17:04
memberjoyhen12312315-Sep-15 17:04 
QuestionVideo output data/bit rate Pin
johnds1974uk24-Mar-15 0:40
memberjohnds1974uk24-Mar-15 0:40 
AnswerRe: Video output data/bit rate Pin
Darko Jurić24-Mar-15 1:38
memberDarko Jurić24-Mar-15 1:38 
GeneralRe: Video output data/bit rate Pin
johnds1974uk25-Mar-15 0:24
memberjohnds1974uk25-Mar-15 0:24 
GeneralRe: Video output data/bit rate Pin
Darko Jurić25-Mar-15 1:24
memberDarko Jurić25-Mar-15 1:24 
GeneralMy vote of 5 Pin
Fernando E. Braz14-Oct-14 19:43
memberFernando E. Braz14-Oct-14 19:43 
GeneralRe: My vote of 5 Pin
Darko Jurić14-Oct-14 20:36
memberDarko Jurić14-Oct-14 20:36 
QuestionDoes it work in Linux? Pin
William Ivanski14-Oct-14 9:30
memberWilliam Ivanski14-Oct-14 9:30 
AnswerRe: Does it work in Linux? Pin
Darko Jurić14-Oct-14 9:56
memberDarko 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 15 Sep 2015
Article Copyright 2014 by Darko Jurić
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid