Main Content


Apply or remove gamma correction


visionhdl.GammaCorrector applies or removes gamma correction on a stream of pixels. Gamma correction adjusts linear pixel values so that the modified values fit a curve. The de-gamma operation performs the opposite operation to obtain linear pixel values.

To apply or remove gamma correction:

  1. Create the visionhdl.GammaCorrector object and set its properties.

  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects?.



corrector = visionhdl.GammaCorrector returns a System object™ that applies default gamma correction on a stream of pixels.

corrector = visionhdl.GammaCorrector(Name,Value) returns a gamma correction System object, corrector, with properties set using one or more name-value pairs. Enclose each property name in single quotes.

Example: corrector = visionhdl.GammaCorrector('LinearSegment',false) creates a gamma correction object that does not use a linear segment in the gamma curve.


corrector = visionhdl.GammaCorrector(operation,gammaValue,Name,Value) returns a gamma correction System object with the Correction property set to operation, the Gamma property set to gammaValue, and additional options specified by one or more name-value pairs.


expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the release function unlocks them.

If a property is tunable, you can change its value at any time.

For more information on changing property values, see System Design in MATLAB Using System Objects.

Direction of intensity curve adjustment, specified as either:

  • 'Gamma' — Apply gamma correction.

  • 'De-gamma' — Remove gamma correction.

Target or current gamma value, specified as a scalar value greater than or equal to 1.

  • When you set Correction to 'Gamma', set this property to the target gamma value of the output video stream.

  • When you set Correction to 'De-gamma', set this property to the gamma value of the input video stream.

Option to include a linear segment in the gamma curve, specified as true or false. When you set this property to true, the gamma curve has a linear portion near the origin.

Point where the gamma curve and linear segment meet, specified as a scalar pixel value between 0 and 1, exclusive.


To enable this property, set LinearSegment to true.



[pixelOut,ctrlOut] = corrector(pixelIn,ctrlIn) returns the intensity value of a pixel after gamma correction, and the control signals associated with the pixel. The input, pixelIn, and output, pixelOut, are scalar values representing a single pixel.

This object uses a streaming pixel interface with a structure for frame control signals. This interface enables the object to operate independently of image size and format, and to connect with other Vision HDL Toolbox™ objects. The object accepts and returns a scalar pixel value and control signals as a structure containing five signals. The control signals indicate the validity of each pixel and its location in the frame. To convert a pixel matrix into a pixel stream and control signals, use the visionhdl.FrameToPixels object. For a full description of the interface, see Streaming Pixel Interface.

Input Arguments

expand all

Intensity of a single pixel, specified as a scalar value. For fixed-point data types, the input word length must be less than or equal to 16.

double and single data types are supported for simulation, but not for HDL code generation.

Data Types: single | double | int8 | int16 | uint8 | uint16 | fi

Control signals accompanying input pixel stream, specified as a pixelcontrol structure containing five logical data type signals. The signals describe the validity of the pixel and its location in the frame. For more details, see Pixel Control Structure.

Data Types: struct

Output Arguments

expand all

Gamma-corrected intensity of a single pixel, specified as a scalar value. The data type of the output pixel is the same as the data type of pixelIn.

Control signals accompanying output the pixel stream, returned as a pixelcontrol structure containing five logical data type signals. The signals describe the validity of the pixel and its location in the frame. For more details, see Pixel Control Structure.

Data Types: struct

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj, use this syntax:


expand all

stepRun System object algorithm
releaseRelease resources and allow changes to System object property values and input characteristics
resetReset internal states of System object


collapse all

This example performs gamma correction on a thumbnail image.

Load the source image from a file. Select a portion of the image matching the desired test size.

frmOrig = imread('rice.png');
frmActivePixels = 64;
frmActiveLines = 48;
frmInput = frmOrig(1:frmActiveLines,1:frmActivePixels);
title 'Input Image'

Create a serializer object and specify the size of inactive pixel regions.

frm2pix = visionhdl.FrameToPixels(...

Create a gamma corrector object.

 gammacorr = visionhdl.GammaCorrector(...
      'Gamma', 1.75);

Serialize the test image by calling the serializer object. pixIn is a vector of intensity values. ctrlIn is a vector of control signal structures.

Note: This object syntax runs only in R2016b or later. If you are using an earlier release, replace each call of an object with the equivalent step syntax. For example, replace myObject(x) with step(myObject,x).

[pixIn,ctrlIn] = frm2pix(frmInput);

Prepare to process pixels by preallocating output vectors.

[~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix);
pixOut = zeros(numPixelsPerFrame,1,'uint8');
ctrlOut  = repmat(pixelcontrolstruct,numPixelsPerFrame,1);

For each pixel in the stream, compute the gamma corrected pixel value.

for p = 1:numPixelsPerFrame  
    [pixOut(p),ctrlOut(p)] = gammacorr(pixIn(p),ctrlIn(p));

Create a deserializer object with a format matching that of the serializer. Convert the pixel stream to an image frame by calling the deserializer object. Display the resulting image.

pix2frm = visionhdl.PixelsToFrame(...
[frmOutput,frmValid] = pix2frm(pixOut,ctrlOut);
if frmValid
    imshow(frmOutput, 'InitialMagnification',300)
    title 'Output Image'


expand all

For the equations used for gamma correction, see Gamma Correction (Computer Vision Toolbox).

To save hardware resources, the object implements the gamma correction equation as a lookup table. The lookup table maps each input pixel value to a corrected output value.

See Also




Introduced in R2015a