Color Correction

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Revision as of 13:20, 7 January 2013 by Chris Johnson (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Color Correction




EMGU Reference
OpenCV Reference


Source Code V1.0


The following example shows the use of different image colour depths. The function of different colour depths allow the correction of image data though adjusting parameters.

Software NameName


The code provided should run straight out of the Emgu.Example folder (V2.4.2), extract it to this location. If the code fails to execute re-reference the EMGU libraries and include the required opencv dlls in the bin directory. Note that the project is set to build to the output path "..\..\..\bin\" you may wish to change this if you don't extract to the EMGU.Example folder.

EMGU Coding Level: While the coding is not advanced the rated level for this example is Beginer/Intermediate. There is a lot of code within the example that can be intimidating to new comers but it's use is simple.

The Code

The code provided in this sample is basic there is little or no error checking. Support is available through the Forums but please try and examine the code before saying it doesn't work for you. The code is not optimised instead is better formatted to provided an understanding of the stages involved.

The programs operation is fairly simple, when a tab is selected the original image or current image being worked on is converted to the appropriate colour type using the Convert<>() method call. Image correction or adjustment is simply applied by adding a constant supplied from the track-bar values to the appropriate depth of the image array. For example the image type BGR has 3 arrays of image data, the Blue, Green, and Red respectively. To adjust the blue colour intensity the values in the 0th array are adjusted, for Green the 1st, and Red the 3rd. The same method is applied to all colour type available.

By default changes in one colour space are not passed through to others, this allows you to evaluate how changes effect the image in different ways. To enable changes to be passed select the check-box in the lower right pane marked "Pass Changes Between Types".

The Code: Variables

Each image type is given it's own variable in which the contents of the image is copied to. From these variable individual image adjustment is allowed.

        Image<Bgr, Byte> Image_BGR;
        Image<Bgra, Byte> Image_BGRA;
        Image<Gray, Byte> Image_GRAY;
        Image<Hls, Byte> Image_HLS;
        Image<Hsv, Byte> Image_HSV;
        Image<Lab, Byte> Image_LAB;
        Image<Luv, Byte> Image_LUV;
        Image<Xyz, Byte> Image_XYZ;
        Image<Ycc, Byte> Image_YCC;

To track which image type is in use, so that the program operates correctly when saving the image or copying it between colour depths, a public enum is employed. This is stored in the variable Depth as is changed to the correct image depth whenever a different tab is selected.

        Imagetype Depth = Imagetype.Bgr; //stores the image depth
        public enum Imagetype
            Bgr, //Blue, green, red
            Bgra, //blue, green, red, alpha
            Gray, //grayscale
            Hls, //aka HSL Hue, lightness, saturation
            Hsv, // Hue, saturation, Value
            Lab, //Lightness,  a and b color-opponent dimensions
            Luv, //Lightness, u and v chromaticity coordinates,
            Rgb, //Red, green, blue
            Rgba, //Red, green, blue, aplha
            Xyz, //
            Ycc //aka YCbCr, Luma,  chroma component Blue-Difference,  chroma component Red-Difference

The Code: Methods

What a method does...


Methods Available


  • name()


  • name()


  1. Numbered list If any