Main Content

visionhdl.ColorSpaceConverter

Convert color information between color spaces

Description

The visionhdl.ColorSpaceConverter System object™ converts between R'G'B' and Y'CbCr color spaces, and also converts R'G'B' to intensity.

Note

The ColorSpaceConverter System object operates on gamma-corrected color spaces. However, to simplify use of the System object, the property arguments do not include the prime notation.

To convert color information between color spaces:

  1. Create the visionhdl.ColorSpaceConverter 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?

Creation

Description

CSC = visionhdl.ColorSpaceConverter(Name,Value) returns a System object that converts between color spaces. Set properties using one or more name-value pairs. Enclose each property name in single quotes.

example

Properties

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.

Type of color space conversion. The object accepts input as a vector of three values representing a single pixel. If you choose 'RGB to intensity', the output is a scalar value. Otherwise, the output is a vector of three values.

Conversion equation to use on the input video stream.

Dependencies

This property does not apply when you set Conversion to 'RGB to intensity'.

Scanning standard to use for HDTV conversion.

Dependencies

This property applies when you set ConversionStandard to 'Rec. 709 (HDTV)'.

Usage

Description

[pixelout,ctrlout] = CSC(pixelin,ctrlin) converts a single pixel from one color space to another. The input pixelin is a vector of three values representing one pixel in R'G'B' or Y'CbCr color space. If the Conversion property is set to 'RGB to YCbCr' or 'YCbCr to RGB', then the output pixelout is a vector of three values representing one pixel. If the Conversion property is set to 'RGB to intensity', then the output pixelout is a scalar value representing one pixel.

example

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 three-component vector that represents a single pixel and a structure that contains five control 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

Input pixel in gamma-corrected R'G'B' or Y'CbCr color space, specified as a vector of three unsigned integer values.

You can simulate System objects with a multipixel streaming interface, but you cannot generate HDL code for System objects that use multipixel streams. To generate HDL code for multipixel algorithms, use the equivalent Simulink® blocks.

The software supports double and single data types for simulation, but not for HDL code generation.

Data Types: uint8 | uint16 | fixdt(0,N,0), where N = 8,9,...,16 | single | double

Control signals accompanying the 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

Output pixel returned as a vector of three unsigned integer values, or a scalar unsigned integer value. The data type of the output pixel is the same as the input pixel.

  • If you set the Conversion property to 'RGB to YCbCr' or 'YCbCr to RGB', then pixelout is a vector representing the pixel in gamma-corrected color space.

  • If you set the Conversion property to 'RGB to intensity', then pixelout is a scalar representing pixel intensity.

The software supports double and single data types for simulation, but not for HDL code generation.

Data Types: uint8 | uint16 | fixdt(0,N,0), where N = 8,9,...,16 | single | double

Control signals accompanying the output 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:

release(obj)

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

Examples

collapse all

This example shows how to convert pixel stream data to a different color space.

Set the dimensions of the test image and load a color source image. Select a portion of the image matching the desired test size.

frmActivePixels = 64;
frmActiveLines = 48;
frmOrig = imread('fabric.png');
frmInput = frmOrig(1:frmActiveLines,1:frmActivePixels,:);
figure
imshow(frmInput,'InitialMagnification',300)
title 'Input Image'

Figure contains an axes object. The hidden axes object with title Input Image contains an object of type image.

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

frm2pix = visionhdl.FrameToPixels( ...
      'NumComponents',3, ...
      'VideoFormat','custom', ...
      'ActivePixelsPerLine',frmActivePixels, ...
      'ActiveVideoLines',frmActiveLines, ...
      'TotalPixelsPerLine',frmActivePixels+10, ...
      'TotalVideoLines',frmActiveLines+10, ...
      'StartingActiveLine',6, ...     
      'FrontPorch',5);

Create a color space converter object. Select a conversion from R'G'B' to grayscale.

convertrgb2gray = visionhdl.ColorSpaceConverter( ...
      'Conversion','RGB to intensity');

Serialize the test image. pixIn is a numPixelsPerFrame-by-3 matrix. ctrlIn is a vector of control signal structures.

[pixIn,ctrlIn] = frm2pix(frmInput);

Set up variables, and convert each pixel in the stream to the new color space.

[~,~,numPixelsPerFrame] = getparamfromfrm2pix(frm2pix);
pixOut = zeros(numPixelsPerFrame,1,'uint8');
ctrlOut = repmat(pixelcontrolstruct,numPixelsPerFrame,1);
for p = 1:numPixelsPerFrame  
    [pixOut(p),ctrlOut(p)] = convertrgb2gray(pixIn(p,:),ctrlIn(p));
end

Create a deserializer object with a format that matches the format of the serializer. Convert the pixel stream to an image frame, and display the grayscale output image.

pix2frm = visionhdl.PixelsToFrame( ...
      'NumComponents',1, ...
      'VideoFormat','custom', ...
      'ActivePixelsPerLine',frmActivePixels, ...
      'ActiveVideoLines',frmActiveLines, ...
      'TotalPixelsPerLine',frmActivePixels+10);
[frmOutput,frmValid] = pix2frm(pixOut,ctrlOut);
if frmValid
    figure
    imshow(frmOutput,'InitialMagnification',300)
    title 'Output Image'
end

Figure contains an axes object. The hidden axes object with title Output Image contains an object of type image.

Algorithms

This System object implements the algorithms described on the Color Space Converter block reference page.

Extended Capabilities

Version History

Introduced in R2015a

expand all

See Also

| (Image Processing Toolbox) | (Image Processing Toolbox) | |