Menu

Channel statistics

Perrotti

Some statistics of channels information are very common and useful in image processing. The TStatsChannel class provides a common set of statistics that can be obtained running the channel once. The channel is scanned at the time that the class is instantiated, so the statistics are always available while the instance exists.

This class calculates the horizontal and vertical projections, the histogram, and other information. All calculated statistics are available through properties. For the moment, only 8-bits channels can be scanned, but future versions will support 16 and 32 bits channels.

ROI support
The statistics can be calculated from entire map, or only in specific ROI (Region Of Interest). The ROI can be defined by a rectangle (TRect) or by a mask with any shape. For mask details see specific topic ahead.

Constructors
To create an instance of TStatsChannel class you can use one of the three constructors listed below.

1) No ROI, use the entire channel
constructor Create(Chn: TfiChannel8U; Calcs: TChannelCalcSet;
Binary: boolean);

Chn is a reference to channel that will be scanned. At the moment, only 8 bits channels can be used.
Calcs inform if the histogram and projections must be created. Histograms and projections are arrays and therefore spend memory to be stored. Because they are not always necessary, creating them is optional. Type TChannelCalcSet is defined as follows:

type
  TChannelCalcs = (ccHist, ccVtProj, ccHzProj);

Binary affects how the projections are be calculated. If is true, the channel will be considered a binary channel and the projections will contain the number of active pixels (with value different from zero) in each row or column. If is false, the projections will contain the sum of values in each row or column.

2) With rectangular ROI
constructor Create(Chn: TfiChannel8U; Calcs: TChannelCalcSet;
Binary: boolean; RtROI: TRect);

RtROI is a rectangle with the region that will be considered to calc the statistics. Projections are being returned with the rectangle dimensions. If the rectangle exceeds the limits of image, it will be clipped. For safety, whenever run the projections, use the HzSize and VtSize properties to get their actual length.

3) With mask ROI
constructor Create(Chn: TfiChannel8U; Calcs: TChannelCalcSet;
Binary: boolean; MaskROI: TfiBinMask; X, Y: integer);

MaskROI is the mask that defines the pixels that will be processed. A mask is essentially a list of points, so there is no shape associated. However, the max and min values of coordinates of points in mask are used to calculate an involving rectangle, and the dimensions of this rectangle are used to create the projections. If the rectangle exceeds the limits of image, it will be clipped. Points out image are ignored. X and Y parameters indicate where the base point of mask will be positioned.

Properties
All statistics calculated are available by the properties of class, described below. All properties are read only.

property ROIrect: TRect;

Return the involving rectangle of region considered to calculations. If no ROI was defined (constructor 1), region is the entire channel.

property Width: integer;

Return the width of region considered to calculations.

property Height: integer;

Return the Height of region considered to calculations.

property Histogram: PHistogram;

Reference to histogram of the region considered to calculations. If the value ccHist was no present in the Calcs parameter, this property returns nil.

property HzProj: PProjection;

Reference to horizontal projection of the region considered to calculations. If the value ccHzProj was no present in the Calcs parameter, this property returns nil.

property HzSize: integer;

Length of horizontal projection returned by HzProj property. If the value ccHzProj was no present in the Calcs parameter, this property returns zero.

property HzMax: integer;

The maximum value on horizontal projection returned by HzProj property. If the value ccHzProj was no present in the Calcs parameter, this property returns zero.

property VtProj: PProjection;

Reference to vertical projection of the region considered to calculations. If the value ccVtProj was no present in the Calcs parameter, this property returns nil.

property VtSize: integer;

Length of vertical projection returned by VtProj property. If the value ccVtProj was no present in the Calcs parameter, this property returns zero.

property VtMax: integer;

The maximum value on vertical projection returned by VtProj property. If the value ccVtProj was no present in the Calcs parameter, this property returns zero.

property TotalArea: integer;

Return the total area of region. Area is the number of pixels in the region considered. If region was defined by a rectangle or is the entire channel, area is Width*Height. If region was defined by mask, area is the number of points in the mask.

property LevelSum: integer;

Return the sum of values in the region considered.

property LevelMax: integer;

The maximum value found in region.

property LevelMin: integer;

The minimum value found in region.

property ZeroCount: integer;

The number of pixels with zero value.

property NonZeroCount: integer;

The number of pixels with non-zero value.

property Average: single;

Average of values in region (LevelSum / TotalArea)

property Density: single;

This property makes more sense to binary images. Is the concentration of active pixels in the considered region (NonZeroCount / TotalArea).

property BinGrav: TFloatPoint;

Return the gravity center of region considering image as binary. Is an average of X and Y coordinates to all active pixels.

property GrayGrav: TFloatPoint;

Return the gravity center of region considering image as gray. Is a weighted average of X and Y coordinates to active pixels. The weight is the gray level of pixel.

Viewing the results
To view the histogram and projections, TStatsChannel implements the methods below to draw graphics on bitmaps.

function HistToBitmap(bmpHeight: integer; LineWidth: byte; 
                      Color: TColor): TBitmap;

Return a bitmap with graphical representation of histogram.

function HzProjToBitmap(bmpWidth: integer; FromLeft: boolean; 
                        Color: TColor): TBitmap;
function VtProjToBitmap(bmpHeight: integer; FromTop: boolean; 
                        Color: TColor): TBitmap;

Return a bitmap with graphical representation of projection. FromLeft and FromTop inform the origin of graphics. These methods are intended to represent projections of gray images (figure 1).

Figure 1 - Example of graphs generated by the methods HistToBitmap (top right corner), HzProjToBitmap (right of image) and VtProjToBitmap (below image).

procedure DrawHzProj(bitmap: TBitmap; LineWidth: integer;   
                     FromLeft: boolean; Color: TColor);
procedure DrawVtProj(bitmap: TBitmap; LineWidth: integer; 
                     FromTop: boolean; Color: TColor);

Draw the projection on bitmap informed. FromLeft and FromTop inform the origin of graphics. These methods are intended to represent projections of color images.

Figure 2 - Example of graphs generated by the methods HistToBitmap (right), DrawHzProj (right of image) and DrawVtProj (below image).


Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.