Home Image-Pro General Discussions

Get total surface of pixels over threshold without counting objects

Hi,
Is there a way to just get the total amount of pixels thresholded in an image without having to create and count all the objects.
Thank you

Answers

  • edited September 2014
    You can use Histogram to calculate total object area and percentage area.
    The macro below assumes that the background intensity is 0 and objects have intensities above 0 (as on thresholded image).

        '
        'Calculates object area on image using histogram
        'objects are any pixels with values above 0
        '
        Public Sub GetObjectAreaUsingHist
            Dim im As McImage=ThisApplication.ActiveImage
            If im Is Nothing Then Exit Sub
            Dim pixSize As Double = 1
            If im.SpatialCalibration IsNot Nothing Then
                'calibrated pixel size
                pixSize = im.SpatialCalibration.PixelSizeX * im.SpatialCalibration.PixelSizeY
            End If
            Dim hist As MediaCy.IQL.Operations.McHistogram=im.Histogram
            'calculate areas using histogram
            'get all bin values
            Dim allVals As Double() = hist.Values(-1, 0)'use mono image or channel 1
            Dim areaPix As Double = 0
            For bin As Integer = allVals.GetLowerBound(0) To allVals.GetUpperBound(0)
                areaPix = areaPix + allVals(bin)
            Next
            Dim zeroArea As Double = allVals(allVals.GetLowerBound(0)) 'number of pixels in 0 bin
            'Area in calibrated units
            Debug.Print "Total Object Area = " & (areaPix - zeroArea) * pixSize
            'AreaPercent
            If areaPix > 0 Then
                Debug.Print "Area Percentage = " & ( 100.0 * (areaPix - zeroArea) / areaPix)
            End If
        End Sub
    

    Yuri
Sign In or Register to comment.