Home Image-Pro Automation (Macros, Apps, Reports)

Programmatically Creating measured lines from bounding box measurements from count/size

The app IDs given image structures and returns dimensional info using Count/Size and bounding box metrics. The app displays the detected region and returns either the Box Height or Width depending on the measured structure. This works well. However, I would prefer to draw a single line that would represent the measured length as measured by the bounding box. I thought I could use the Count/Size Count Option, Longest Line feature. This worked but it is not constrained to the X or Y axis of the Bounding Box. I see the value of the current behavior but it would be nice to offer an X or Y constraint option, the line drawn would track the width or height axis of the box so to speak.

 

Given this, in order to programmatically draw the line I thought that I could acquire the bounding box upper left and lower right X/Y coordinates and draw an annotation line using this data relative to the bounding box width or height result. I found the GetBoundingBox Method that will return the point data needed to draw an anno line but this seems tied to manual Measurements as the documentation indicates and not available from a Count/Size measurement. How do I grab the bound box coordinates, is there a better method for annotating the measured dimension constrained to the X/Y of the box?

 

For clarity, I am using Premier 9.0.3.

 

Best Answer

  • edited July 2013 Answer ✓
    Hi Rod,

    Here is the macro that will draw bounding box with width/height and angle of the objects.

        Public Sub DrawWidthHeightBoxes()
            If ThisApplication.ActiveImage Is Nothing Then OpenDemoImage
            Dim image1 As McImage=ThisApplication.ActiveImage
            Dim md As McMMData=image1.MeasurementsData
            If md.Count=0 Then Exit Sub
            Dim pixelSize As Double=1
            If image1.SpatialCalibration IsNot Nothing Then
                pixelSize=image1.SpatialCalibration.PixelSizeX
            End If
            Dim ovl As MediaCy.IQL.Display.Overlays.McGraphOverlay=image1.AnnotationOverlay
            For Each sf As McMMSubFeature In md.SubFeatures
                If sf.FeatureType And McMMSubFeature.mcmmsfTypes.mcmmsfRegion = 0 Then Continue For
                'get size in pixels
                Dim width As Double =sf.Value(eMeasures.RgnWidth)/pixelSize
                Dim height As Double =sf.Value(eMeasures.RgnLength)/pixelSize
                Dim angle As Double=sf.Value(emeasures.RgnDirection)
                Dim center As System.Drawing.PointF
                center.X=sf.Value(eMeasures.RgnCentroidX)/pixelSize
                center.Y=sf.Value(eMeasures.RgnCentroidY)/pixelSize
                Dim box As MediaCy.IQL.Display.Overlays.McGraphObjRect= ovl.Add("McGraphObjRect")
                box.SetHandle(1,center.x-width/2,center.y-height/2)
                box.SetHandle(5,center.x+width/2,center.y+height/2)
                box.AngleOfRotation=angle+90
                box.NotifyCreationComplete
            Next
        End Sub

Answers

  • Hi Rod,

    If you want to use low-level functions to draw bounding box, the Measurements module exposes this functionality. So you can get McFeatures object of any measurement element and all corresponded properties.

    The macro below with draw bounding box in annotation overlay for every measurement feature on the active image (it will open a demo image if no image active):

    Imports MediaCy.Addins.Measurements
    Public Module Module1
    
        '
        'Draws bounding box around every measurement feature on active image
        '
        Public Sub DrawBoundingBoxes()
            If ThisApplication.ActiveImage Is Nothing Then OpenDemoImage
            Dim image1 As McImage=ThisApplication.ActiveImage
            Dim md As McMMData=image1.MeasurementsData
            If md.Count=0 Then Exit Sub
            Dim ovl As MediaCy.IQL.Display.Overlays.McGraphOverlay=image1.AnnotationOverlay
            For Each sf As McMMSubFeature In md.SubFeatures
                Dim bbox As Mediacy.IQL.ObjectManager.SINGLERECT=sf.GetFeatures.BoundingRect(sf.FeatureIndex)
                Dim box As MediaCy.IQL.Display.Overlays.McGraphObjRect= ovl.Add("McGraphObjRect")
                box.SetHandle(1,bbox.Left,bbox.Top)
                box.SetHandle(5,bbox.Right,bbox.Bottom)
                box.NotifyCreationComplete
            Next
        End Sub
    
    
    
        Public Function OpenDemoImage() As SimpleScript
            OpenDemoImage = New SimpleScript
            Dim docList1 = New List(1), doc1
    
            With Application.DocumentCommands.OpenImage(OpenDemoImage)
                .Filenames = New String() {ThisApplication.Path(mcPathType.mcptSampleImages) & "Count and Size\SPOTS.TIF"}
                .Run(docList1)
            End With
    
            With Application.DocumentCommands.Activate(OpenDemoImage)
                .Run(docList1(0), doc1)
            End With
    
            With Measure.MeasurementsCommands.Options(OpenDemoImage)
                .Segmentation.AutoFindPhase = MediaCy.IQL.Features.mcFindPhase.mcfpDarkest
                .Segmentation.SegmentationType = McMMOptions.mcmmSegmentationType.mcmmstThresholdSegmentation
                .Run(doc1)
            End With
    
            With Measure.MeasurementsCommands.ExecuteCount(OpenDemoImage)
                .Run(doc1)
            End With
        End Function
    
    End Module
    
    I've also attached the complete project.
    Yuri

  • Sorry for the confusion. Using count/size I have the bounding box measurement as a result of a detected object. I would like to draw an annotation line that represents either the width or height dimension of the bounding box that represents the measurement, either height or width.
  • rdbunn said:
     
    Thank you very much Yuri. However, the message is incomplete.
  • It should be complete now.
  • And here is the updated project with new sub.
  • Yuri:

    I added the Box sub to the original code and called. Works fine with your sample1. For testing purposes I added the draw box sub to my app and called after I have counted the objects. However, when I attempt to load my app I receive" type identifier is invalid" and is referencing McMMSubFeature. It thought I had not included all the references required. I compared the working app with mine and all was the same. I am overlooking something simple I suspect. Any thoughts? 

  • Never mind, I found the problem!
Sign In or Register to comment.