Click here to Skip to main content
13,902,633 members
Click here to Skip to main content
Add your own
alternative version

Stats

34.3K views
46 bookmarked
Posted 14 Oct 2014
Licenced LGPL3

Introducing Portable Generic Image Library for C#

, 15 Sep 2015
Rate this:
Please Sign up or sign in to vote.
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.

//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.

//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

//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.

//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.

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:

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.

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)

Share

About the Author

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

You may also be interested in...

Pro

Comments and Discussions

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

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