Click here to Skip to main content
15,868,054 members
Articles / Multimedia / Image Processing

Introducing Portable Generic Image Library for C#

Rate me:
Please Sign up or sign in to vote.
4.96/5 (23 votes)
15 Sep 2015LGPL33 min read 53.1K   47   15
Portable generic image library for C#

The .NET does not provide the truly portable image format. System.Drawing.Bitmap and does not provide generic interface which imposes compile-time constraints. EmguCV's generic image provides the generic interface but is heavily coupled with OpenCV and does not provide non-generic interface which has basic data manipulation methods necessary for many algorithms. All mentioned image types do not provide a unified interoperability needed when mixing image-processing algorithms from different libraries.

The DotImaging Framework approach is to provide color types which is used by a 2D array which represents an managed image. This way an already known built-in array type is leveraged which provides the base for many image-processing algorithms which can be written as extensions. The interoperability with other image types is achieved through extension assemblies such as DotImaging.BitmapInterop. The reasons why the library is appealing are the following:

  • leverages existing .NET structures
    The primary imaging structure is the built-in .NET array (e.g. Bgr<byte>[,] - a Bgr 8-bit image)

  • portable - designed for the Web
    It is not tied with any platform-specific API thus enabling simple multi-platform usage.

  • lightweight (no 3rd party dependencies), but powerful
    There are no 3rd-party dependencies and packages are small.

  • so simple, you don't need a help file
    Upon a NuGet package installation an appropriate read-me file is shown which provides short samples which demonstrate what you can do with it.

 

Components / NuGet packages

DotImaging.GenericImage
.NET image array extensions. Color and depth conversions. Slim unmanaged structure for fast pixel manipulation.

C#
//convert to grayscale and flip
Bgr<byte>[,] image = ImageIO.LoadColor("sample.jpg").Clone();
Gray<byte>[,] grayIm = image.ToGray()
                            .Flip(FlipDirection.Horizontal);

 

DotImaging.IO
A unified API for IO image access (camera, file, image directory). Portable image loading/saving.

C#
//create camera (file or image-directory) capture
var reader = new CameraCapture();
reader.Open();
//read single frame
var frame = reader.ReadAs<Bgr<byte>>();
reader.Close();

 

DotImaging.Linq
2D array Linq extensions

C#
//create a managed image
Bgr<byte>[,] image = ...;
//get the modified blue channel
var modifiedImage = image.AsEnumerable()
                     .Select(x => x.B / 2)
                         .ToArray2D(image.Size());

 

DotImaging.Drawing
.NET image drawing array extensions.

C#
//create a managed image
var image = new Bgr<byte>[480, 640];
//draw something
image.Draw(new Rectangle(50, 50, 200, 100), Bgr<byte>.Red, -1);
image.Draw(new Circle(50, 50, 25), Bgr<byte>.Blue, 5);

 

DotImaging.BitmapInterop
Interoperability extensions between .NET array and Bitmap.

C#
var image = new Gray<byte>[240, 320];
var bmp = image.ToBitmap(); //to Bitmap
var imageFromBmp = bmp.ToArray() as Bgr<byte>[,]; //from Bitmap

 

DotImaging.Primitives2D
Portable 2D drawing primitives (Point, Size, Rectangle, ...)

Fast pixel manipulation

Sometimes the pixel access speed through the built-in 2D .NET array is not enough. The boundary checking can be eliminated by using the unsafe code. Also, by having the image representation as unmanaged pointer and size, the managed array can be easily converted to various 3rd party image structure formats. The built in Image<TColor> provides an unmanaged pixel access. An example usage is shown below:

Bgr<byte>[,] image = ...

using(Image<Bgr<byte>> uIm = image.Lock())
{
   Bgr8* ptr = (Bgr8*)uIm.ImageData;
   ptr->B = 8; //set the image[0, 0].B = 8;
}

A reverse transformation acquires data-copy which can be done as:

C#
Image<Bgr<byte>> uIm = ...
Bgr<byte>[,] image = uIm.Clone();

An unmanaged image also supports OpenCV's IplImage interoperability, which can be useful is using some OpenCV's functions directly or via EmguCV library.

C#
Image<Bgr<byte>> uIm = ...
IplImage iplImage = uIm.AsOpenCvImage();

The unmanaged class also implements the non-generic interface IImage which enables passing generic image instance where generic type is not possible or not desirable. The interface itself contains numerous extensions so even the unmanaged non-generic representation is quite useful. Please bare in mind that unmanaged class is here primary for interoperability and fast pixel access. .NET built-in array is the primary imaging object.

 

Conclusion

This article presents the portable generic image library which offers interoperability with image formats from other libraries: EmguCV, OpenCV and the standard .NET types (Bitmap, BitmapData, 2D and 3D array). The framework is the foundation for the Accord.NET Extensions Framework - framework for image processing and computer vision. The DotImaging sets focus on .NET native array as primary imaging object, offers extensibility support via extensions, and provides unified platform-abstract imaging IO API, so do not forget to take a peek :).

History

  • 13 October 2014 - First version released
  • 28 April 2015 - Updated
  • 15 September 2015 - Major revision

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

 
QuestionTrying to streaming a youtube video and nothing happens Pin
Member 1303070230-Nov-17 22:16
Member 1303070230-Nov-17 22:16 
AnswerRe: Trying to streaming a youtube video and nothing happens Pin
Member 1303070230-Nov-17 22:34
Member 1303070230-Nov-17 22:34 
If i see the content of pipeName variable i get this:
\\.\pipe\youtubeVideoPipe


Nothing related with the video... Cry | :((
GeneralRe: Trying to streaming a youtube video and nothing happens Pin
Darko Jurić28-Dec-17 11:20
Darko Jurić28-Dec-17 11:20 
GeneralRe: Trying to streaming a youtube video and nothing happens Pin
Member 1303070231-Jan-18 11:09
Member 1303070231-Jan-18 11:09 
GeneralMy vote of 5 Pin
Franc Morales29-Oct-16 8:57
Franc Morales29-Oct-16 8:57 
QuestionPCL / Xamarin Android/iOS Pin
Mathieu Jack30-Jan-16 1:12
Mathieu Jack30-Jan-16 1:12 
AnswerRe: PCL / Xamarin Android/iOS Pin
Darko Jurić31-Jan-16 7:34
Darko Jurić31-Jan-16 7:34 
QuestionVery nice idea Pin
hzawary15-Aug-15 21:13
hzawary15-Aug-15 21:13 
AnswerRe: Very nice idea Pin
Darko Jurić27-Aug-15 9:44
Darko Jurić27-Aug-15 9:44 
QuestionNice project Pin
Jaime Fontanella Rios31-Mar-15 8:41
Jaime Fontanella Rios31-Mar-15 8:41 
AnswerRe: Nice project Pin
Darko Jurić2-Apr-15 10:40
Darko Jurić2-Apr-15 10:40 
QuestionSupported Platforms, dependancies Pin
Nathan Packard3-Mar-15 16:24
Nathan Packard3-Mar-15 16:24 
AnswerRe: Supported Platforms, dependancies Pin
Darko Jurić3-Mar-15 21:27
Darko Jurić3-Mar-15 21:27 
GeneralComments Pin
M.Farrukh Abbas15-Oct-14 1:43
M.Farrukh Abbas15-Oct-14 1:43 
GeneralRe: Comments Pin
Darko Jurić15-Oct-14 4:23
Darko Jurić15-Oct-14 4:23 

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.