Histogram2 Method

NI Vision for Visual Basic

Histogram2 Method

Syntax

CWIMAQVision.Histogram2 SourceImage, HistogramReport [, HistogramOptions] [, MaskImage]

Return Type

Long

On success, this method returns 0. On failure, this method returns a negative number.

Purpose

Calculates the histogram, or pixel value distribution, of an image.

Remarks

Use this method with U8, I16, and SGL images. MaskImage must be a U8 image.

Parameters

SourceImage As CWIMAQImage

The image the method uses to compute the histogram.

HistogramReport As CWIMAQHistogramReport

On return, a CWIMAQHistogramReport object describing the pixel value classification. The report also contains an array describing the number of pixels that fell into each class.

HistogramOptions As Variant

[Optional] A CWIMAQHistogramOptions object specifying the options for the algorithm to use.

MaskImage As Variant

[Optional] A CWIMAQImage object that specifies the region in which the method computes the histogram. The method processes only those pixels in the image whose corresponding pixels in the mask are non-zero. Do not set this parameter if you want to calculate the histogram for the entire image.

Example

'To run this example, add a picture box to your form, select
'a region of interest on the viewer, and click run.
Private Sub Run_Click()
    Dim HistogramReport As New CWIMAQHistogramReport
    Dim MaskImage As New CWIMAQImage
    
    ' Find the histogram of a portion of the image in Viewer1
    ' defined by the regions on Viewer1.
    CWIMAQVision1.RegionsToMask MaskImage, CWIMAQViewer1.Regions
    
    CWIMAQVision1.Histogram2 CWIMAQViewer1.Image, HistogramReport, , MaskImage
    
    'Draw the histogram results to a picture control
    DrawHistogramResults Picture1, HistogramReport(1).Histogram
End Sub

'Draw the histogram results to a picture control
Private Sub DrawHistogramResults(Picture As PictureBox, Histogram As Variant, Optional ColorArray As Variant)
    Dim I As Long
    Dim N As Long
    Dim Width As Long
    Dim Height As Long
    Dim MaxCount As Long
    
    If IsMissing(ColorArray) Then
        ReDim ColorArray(0 To 255) As OLE_COLOR
        For N = 1 To 255
            ColorArray(N) = RGB(N, N, N)
        Next N
    End If
    
    'Find the peak
    MaxCount = 0
    If Not IsMissing(Histogram) Then
        For N = 1 To 255
            If Histogram(N) > Histogram(MaxCount) Then
                MaxCount = N
            End If
        Next N
        MaxCount = Histogram(MaxCount)
    End If

    'If there are no results, exit the routine
    If MaxCount = 0 Then Exit Sub
    
    'Clear the picture
    Picture.Cls
    
    'Compute the width and height of the picturebox control, in pixels
    Picture.ScaleMode = vbPixels
    Width = Picture1.ScaleWidth
    Height = Picture1.ScaleHeight
    
    'Draw a line the appropriate height and color for each pixel found.
    For I = 0 To Width - 1
        N = (I * 256 / Width)
        Picture.Line (I, Height * (1 - Histogram(N) / MaxCount))-(I, Height), ColorArray(N)
    Next I
End Sub

 Histogram Example 

See Also

ColorHistogram3

CWIMAQHistogramReport