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:
visionhdl.GammaCorrector object and set its properties.
Call the object with arguments, as if it were a function.
To learn more about how System objects work, see What Are System Objects?.
a System object™ that applies default gamma correction on a stream of pixels.
corrector = visionhdl.GammaCorrector
returns a gamma correction System object,
corrector = visionhdl.GammaCorrector(
corrector, with properties set using one or more
name-value pairs. Enclose each property name in single quotes.
corrector = visionhdl.GammaCorrector('LinearSegment',false)creates a gamma correction object that does not use a linear segment in the gamma curve.
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.
Correction— Direction of intensity curve adjustment
Direction of intensity curve adjustment, specified as either:
'Gamma' — Apply gamma correction.
'De-gamma' — Remove gamma correction.
Gamma— Target or current gamma value
scalar greater than or equal to 1
LinearSegment— Option to include a linear segment in the gamma curve
Option to include a linear segment in the gamma curve, specified as
false. When you set this property to
true, the gamma curve has a linear portion near the origin.
BreakPoint— Point where the gamma curve and linear segment meet
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
returns the intensity value of a pixel after gamma correction, and the control signals
associated with the pixel. The input,
ctrlOut] = corrector(
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.
pixelIn— Input pixel
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.
types are supported for simulation, but not for HDL code generation.
pixelOut— Output pixel
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
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
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); figure imshow(frmInput,'InitialMagnification',300) title 'Input Image'
Create a serializer object and specify the size of inactive pixel regions.
frm2pix = visionhdl.FrameToPixels(... 'NumComponents',1,... 'VideoFormat','custom',... 'ActivePixelsPerLine',frmActivePixels,... 'ActiveVideoLines',frmActiveLines,... 'TotalPixelsPerLine',frmActivePixels+10,... 'TotalVideoLines',frmActiveLines+10,... 'StartingActiveLine',6,... 'FrontPorch',5);
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
[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)); end
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(... 'NumComponents',1,... 'VideoFormat','custom',... 'ActivePixelsPerLine',frmActivePixels,... 'ActiveVideoLines',frmActiveLines); [frmOutput,frmValid] = pix2frm(pixOut,ctrlOut); if frmValid figure imshow(frmOutput, 'InitialMagnification',300) title 'Output Image' end
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.
The latency of the visionhdl.GammaCorrector object is 2 cycles.
imadjust(Image Processing Toolbox)