Main Content

visionhdl.HVCounter

Counts active pixel dimensions of streaming video

Description

The visionhdl.HVCounter System object™ analyzes a video stream and returns the current count of lines per frame and pixels per line. The object also delays control signals of the pixel stream to correspond with the count result. Use this object for algorithms that use the location of a pixel in a frame or region of interest.

This waveform shows the pixel-stream control signals and the resulting counter outputs for the first two lines of a video frame that has 10 pixels per line. The visionhdl.HVCounter System object has a latency of two cycles to return the current counter values.

Logic Analyzer waveforms of the input and output signals of the HV Counter block

To count active lines-per-frame and active pixels-per-line of a pixel stream:

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

HVcntr = visionhdl.HVCounter returns a System object that counts the line and pixel location within a frame or region-of-interest. An object that uses the default values for the properties can count frames up to (511 pixels)-by-(355 lines).

HVcntr = visionhdl.HVCounter(Name,Value) returns a System object sets properties using one or more name-value pairs. Enclose each property name in single quotes. For example, HVcnt = visionhdl.HVCounter('ActivePixelsPerLine',1920,'ActiveVideoLines',1080) creates an object that can count frames up to (2047 pixels)-by-(2047 lines).

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.

Maximum pixels per line, specified as a positive integer. The object implements a pixel counter that has ceil(log2(ActivePixelsPerLine)) bits. For example, for a frame with 320 pixels per line, the counter has 9 bits. For a frame with 1024 pixels per line, the counter has 11 bits.

Maximum lines per frame, specified as a positive integer. The object implements a line counter that has ceil(log2(ActiveVideoLines)) bits. For example, for a frame with 240 pixels per line, the counter has 8 bits. For a frame with 1080 pixels per line, the counter has 11 bits.

Usage

Description

[hcount,vcount,ctrlout] = HVcntr(ctrlin) updates horizontal and vertical location counters hcount and vcount based on pixel-stream control signals, ctrlin. The ctrlout output is a delayed version of ctrlin that matches the timing of the counter values.

example

This System object uses a structure for frame control signals associated with each pixel of a pixel stream. This interface enables the object to operate independently of image size and format. All Vision HDL Toolbox™ System objects use the same streaming interface. The object accepts and returns a structure containing 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 System object. For a full description of the interface, see Streaming Pixel Interface.

Input Arguments

expand all

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

Pixel location within a line, returned as a positive integer. The object clears the pixel count at the start of each line. If the input pixels per line exceeds the size of the counter, the object returns a saturated value until the start of the next line.

Data Types: fi(0,ceil(log2(ActiveVideoLines)),0)

Line location within the frame or region of interest, returned as a positive integer. The object clears the line count at the start of each frame. If the input lines per frame exceeds the size of the counter, the object returns a saturated value until the start of the next frame.

Data Types: fi(0,ceil(log2(ActiveVideoLines)),0)

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

Obtain the current pixel location within a region of interest (ROI).

Load an input frame.

frmOrig = imread('coins.png');
[frmActiveLines,frmActivePixels] = size(frmOrig);
imshow(frmOrig)
title 'Input Image'

Create a serializer object and define inactive pixel regions.

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

Create an object to select a small region of interest. Define a rectangular region by the coordinates of the top-left corner and the dimensions.

hPos = 80;
vPos = 60;
hSize = 15;
vSize = 20;
roicoin = visionhdl.ROISelector('Regions',[hPos vPos hSize vSize]);

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

[pixIn,ctrlIn] = frm2pix(frmOrig);

Prepare to process pixels by preallocating output vectors. The output frame from the ROI object is the same size as the input frame, but the control signals indicate a different active region. The counter block returns a delayed version of the control signals that aligns with the counter values.

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

Write a function that creates and calls a visionhdl.HVCounter System object™. This object returns horizontal and vertical count values that represent the current pixel location within the region of interest. You can generate HDL code from this function.

function  [hCount,vCount,ctrlOut] = pixelCount(ctrlIn)
%pixelCount 
% Returns the current active pixel count per line, hCount, and the active
% line count, vCount, based on the pixel-stream control signals.
% ctrlIn and ctrlOut are structures that contain control signals associated
% with the pixel.
% The object implements counters that accommodate the next power-of-two above
% each property value. This configuration can count frames up to (512 
% pixels)-by-(256 lines).
% You can generate HDL code from this function.

  persistent hvcount;
  if isempty(hvcount)
    hvcount = visionhdl.HVCounter(...
      'ActivePixelsPerLine',320,...
      'ActiveVideoLines',240);    
  end
  
 [hCount,vCount,ctrlOut] = hvcount(ctrlIn);
end

For each pixel in the padded frame, apply the region mask, and then call the counter to indicate the active pixel locations. If the input control signals indicate an inactive pixel at that location, the object returns hCount and vCount set to zero.

oldvCount = 0;
for p = 1:numPixelsPerFrame
    [pixROIOut(p),ctrlOut(p)] = roicoin(pixIn(p),ctrlIn(p));
    [hCount,vCount,ctrlOut2(p)] = pixelCount(ctrlOut(p));
    if (vCount ~=0 && vCount ~= oldvCount)
        fprintf('\n Valid pixels in line #%d : ',vCount);
        oldvCount = vCount;
    end
    if hCount ~= 0
        fprintf('#%d ',hCount);
    end
end
 Valid pixels in line #1 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #2 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #3 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #4 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #5 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #6 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #7 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #8 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #9 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #10 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #11 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #12 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #13 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #14 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #15 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #16 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #17 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #18 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #19 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 
 Valid pixels in line #20 : #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 

Extended Capabilities

Version History

Introduced in R2019a

See Also

Blocks