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

getting access to pixel data of a special region

Hi guys,
when working on the data() structure by using your code in the forum disscussion "image buffer reading/writing":
Dim ra As McRegionAccess=im.CreateRegionAccess()
ra.RegionMask=im.Aoi
Dim data(,) As Object
ra.GetArea(data)
 I always get the whole image in my data-matrix, but I just want to get a special Region.
How do I have to select a region to get the data-matrix?

Thx,
Svenja

Best Answer

Answers

  • edited January 2018
    Hi Svenya,

    You should open Region Access only for a specified area using aditional parameters of the function, here is the example:

        Public Sub GetROIPixels
            Dim im As McImage=ThisApplication.ActiveImage
            Dim r As Mediacy.IQL.ObjectManager.SINGLERECT=im.Aoi.BoundingRect'get ROI bounding box
            'open only ROI area
            Dim ra As McRegionAccess=im.CreateRegionAccess(,,,r.left,r.top,r.right,r.bottom)
            Dim data(,) As Object
            ra.GetArea(data)
            'process pixels
            '...
            ra.PutArea(data)
        End Sub
    

    Yuri
  • Hi Yuri,thank you for your code. We have adapted it like this:
     ' Region ins Array
           'Dim ra As McRegionAccess=im.CreateRegionAccess()
           'ra.RegionMask=im.Aoi'process only active ROI
           lbOut.Items.Clear
           lbwock.Items.Clear
    
           Dim LOx,LOy,RUx,RUy As Integer
           LOx = CInt(tbLOx.Text)
           LOy = CInt(tbLOy.Text)
           RUx = CInt(tbRUx.Text)
           RUy = CInt(tbRUy.Text)
    
    
    
           Dim R As Mediacy.IQL.ObjectManager.SINGLERECT=im.Aoi.BoundingRect'get ROI bounding box
            'open only ROI area
           Dim ra As McRegionAccess=im.CreateRegionAccess(R.GetType,0,0,LOx,LOy,RUx,RUy)
    and then:
    If ra Is Nothing Then Exit Sub ' wenn es keine ROI gibt, dann raus.
           Dim data(,) As Object
           Dim W,O,C,K,AGV,AGVS,M As Integer
           Dim GRAD As Double
    
           'get 2D array of pixel data
           ra.GetArea(data)
           M=0
           AGVS=0
           tbBreite.Text=CStr(data.GetUpperBound(0))
           tbHoehe.Text=CStr(data.GetUpperBound(1))
    
           ' Durchlaufe Array
           For x As Integer = 0 To data.GetUpperBound(0)
            For y As Integer = 0 To data.GetUpperBound(1)
                lbOut.Items.Add("Data("+CStr(x)+","+CStr(y)+")="+CStr(data(x,y)))
            Next
           Next
    Unfortunately my parameter (mean gradient value) is still calculated of the whole image instead of my rectangle ROI. It seems that ROI and image are completely separated objects. Could you please help us again?
    Kind regards, Svenja
  • Hi Svenja,

    How do you calculate "mean gradient value"? Can you post the macro?

    Yuri

  • Hi Yuri,
    here the complete macro for the grey-scale parameters mean gradient value and contrast:
     
     
    Imports MediaCy.Addins.Scripting
    '------------------------------------------------------------------------------
    '
    'Svenja's Gradient und Constrast
    '
    'BY:
    '
    'Svenja Krause
    '
    '------------------------------------------------------------------------------
    '
    ' Anhand dieses Codes sollen die Formeln für den Gradientenbetrag und den
    ' Kontrast ausgewertet werden.
    ' GRAD=1/M ∑_(i,j∈ROI)▒〖AGV(i,j)〗
    ' GRAD= mittlerer Gradientenbetrag
    '  M= Anzahl der definierten zentralen Bildpunkte
    '  AGV= absoluter Gradientenwert
    '  AGV=√(〖(W-C)〗^2 +〖(O-K)〗^2)
    '  W,C,O,K= dem zentralen Bildpunkt i,j benachbarte Bildpunkte
    '
    ' A B   C   D   E
    ' F G   H   I   J
    ' K L   x(i,j)  N   O
    ' P Q   R   S   T
    ' U V   W   X   Y
    '
    '
    ' Kontrast=1/R ∑_(i,j∈ROI)▒〖〖(i-j)〗^2×p(i,j)〗
    ' R=Anzahl der verschiedenen Pixelpaare
    ' (i-j)²= relative Grauwertdifferenz zwischen Pixel i und j
    ' p(i,j)=Häufigkeit des Auftretens einer Grauwertdifferenz
    '
    '
    '
    '------------------------------------------------------------------------------
    '
    'MODIFICATION LOG:
    '
    '
    '------------------------------------------------------------------------------
    
    Public Class Svenja_Gradient_Contrast
    
        Private Sub MyControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyControl.Load
            ' Initialization code goes here
        End Sub
    
        Protected Overrides Sub Finalize()
            ' Cleanup code goes here
        End Sub
    
        ' Provides access to underlying Control
        Public ReadOnly Property Control As System.Windows.Forms.UserControl
            Get
                Return MyControl
            End Get
        End Property
    
        Private Sub btnGradient_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnGradient.Click
           Dim im As McImage=ThisApplication.ActiveImage
           If im Is Nothing Then Exit Sub
           ' Gradienten bestimmen
    
           ' Region ins Array
           'Dim ra As McRegionAccess=im.CreateRegionAccess()
           'ra.RegionMask=im.Aoi'process only active ROI
           lbOut.Items.Clear
           lbwock.Items.Clear
    
           Dim LOx,LOy,RUx,RUy As Integer
           LOx = CInt(tbLOx.Text)
           LOy = CInt(tbLOy.Text)
           RUx = CInt(tbRUx.Text)
           RUy = CInt(tbRUy.Text)
    
    
    
           Dim R As Mediacy.IQL.ObjectManager.SINGLERECT=im.Aoi.BoundingRect'get ROI bounding box
            'open only ROI area
           Dim ra As McRegionAccess=im.CreateRegionAccess(R.GetType,0,0,R.left,R.top,R.right,R.bottom)
           If ra Is Nothing Then Exit Sub ' wenn es keine ROI gibt, dann raus.
           Dim data(,) As Object
           Dim W,O,C,K,AGV,AGVS,M As Integer
           Dim GRAD As Double
    
           'get 2D array of pixel data
           ra.GetArea(data)
           M=0
           AGVS=0
           tbBreite.Text=CStr(data.GetUpperBound(0))
           tbHoehe.Text=CStr(data.GetUpperBound(1))
    
           ' Durchlaufe Array
           For x As Integer = 0 To data.GetUpperBound(0)
            For y As Integer = 0 To data.GetUpperBound(1)
                lbOut.Items.Add("Data("+CStr(x)+","+CStr(y)+")="+CStr(data(x,y)))
            Next
           Next
    
           Dim i,j As Integer
    
            For i = 3 To data.GetUpperBound(0)-2
               For j = 3 To data.GetUpperBound(1)-2
                   ' Bestimme W,O,C,K
                    K=data(i,j-2)
                    W=data(i+2,j)
                    O=data(i,j+2)
                    C=data(i-2,j)
                   ' Berechne AGV (Wurzel aus (W-C)² + (O-K)²
                    AGV=((W-C)^2+(O-K)^2)^(1/2) ' neu von Svenja und Fritz
                   ' AGV=(((W-C)^2)^(1/2))+(O-K)^2 ' Alt von Papa Andi
                   ' Addiere AGV zur AGV-Summe
                    AGVS=AGVS+AGV
                    'Kontrollausgabe
                    tbAGV.Text = CStr(AGV)
                    tbSumAGV.Text = CStr(AGVS)
                    ' Zähle M hoch
                    M=M+1
                    lbwock.Items.Add("M:"+CStr(M)+"-> "+CStr(data(i,j))+ _
                                     " C:"+CStr(C)+" O:"+CStr(O)+ _
                                     " W:"+CStr(W)+" K:"+CStr(K)+ _
                                     " AGV:"+CStr(AGV))
                    tbM.Text = CStr(M)
                Next
            Next
    
           GRAD = AGVS / M
           tbGrad.Text = CStr(GRAD)
         '   For i = 1 To data.GetUpperBound(0)
         '      For j = 1 To data.GetUpperBound(1)
         '       data(i,j).
         '      Next
         '   Next
            ra.PutArea(data)
    
        End Sub
    
    
        Private Sub btnKontrast_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnKontrast.Click
           Dim im As McImage=ThisApplication.ActiveImage
           If im Is Nothing Then Exit Sub
    
           ' Region ins Array
           lbOut.Items.Clear
           lbwock.Items.Clear
    
           Dim LOx,LOy,RUx,RUy As Integer
           LOx = CInt(tbLOx.Text)
           LOy = CInt(tbLOy.Text)
           RUx = CInt(tbRUx.Text)
           RUy = CInt(tbRUy.Text)
    
           Dim R As Mediacy.IQL.ObjectManager.SINGLERECT=im.Aoi.BoundingRect'get ROI bounding box
            'open only ROI area
           Dim ra As McRegionAccess=im.CreateRegionAccess(R.GetType,0,0,LOx,LOy,RUx,RUy)
           If ra Is Nothing Then Exit Sub ' wenn es keine ROI gibt, dann raus.
           Dim data(,) As Object
    
           'get 2D array of pixel data
           ra.GetArea(data)
    
           Dim i,j As Integer
    
           For i = 0 To 255
            lbOut.Items.Add( "-1")
           Next
    
           Dim GDiff, test As Integer
    
            For i = 1 To data.GetUpperBound(0)-1
               For j = 1 To data.GetUpperBound(1)
                   ' Relative Grauwertdifferenz bestimmen
                   GDiff = Abs(data(i,j) - data(i+1,j))
    
                   lbwock.Items.Add("p("+CStr(i)+","+CStr(j)+"): "+CStr(data(i,j))+ _
                                    " - p("+CStr(i+1)+","+CStr(j)+"): "+CStr(data(i+1,j))+ _
                                    " = "+CStr(GDiff)+ " bisher: "+CStr(lbOut.Items(GDiff)))
                   ' Haben wir diese Differenz schon?
                   test = CInt(lbOut.Items(GDiff))
                   If test < 0 Then test = 1 Else test = test +1
    
                   'Zurückschreiben
                   lbOut.Items(GDiff) = CStr(test)
                Next
            Next
    
           ' Jetzt noch die Summe und die Anzahl verschiedener Pixelpaare bestimmen
           Dim Anz, Kontrastsumme, wert As Integer
           Anz = 0
           Kontrastsumme = 0
           For i = 0 To 255
             wert = CInt(lbOut.Items(i))
             If wert > 0 Then
                Anz = Anz + wert
                Kontrastsumme = Kontrastsumme + ((i^2)*wert)
             End If
           Next
    
           Dim Kontrast As Double = Kontrastsumme / Anz
    
           tbKontrast.Text = CStr(Kontrast)
           tbR.Text = CStr(Anz)
            ra.PutArea(data)
        End Sub
    End Class
    



  • Hi Svenya,

    I converted your code to a macro and it works correctly for me.
    Here is the macro:

        Public Sub btnGradient_Click()
           Dim im As McImage=ThisApplication.ActiveImage
           If im Is Nothing Then Exit Sub
           ' Gradienten bestimmen
    
           ' Region ins Array
           'Dim ra As McRegionAccess=im.CreateRegionAccess()
           'ra.RegionMask=im.Aoi'process only active ROI
           'lbOut.Items.Clear
           'lbwock.Items.Clear
    
    '       Dim LOx,LOy,RUx,RUy As Integer
    '       LOx = CInt(tbLOx.Text)
    '       LOy = CInt(tbLOy.Text)
    '       RUx = CInt(tbRUx.Text)
    '       RUy = CInt(tbRUy.Text)
    
    
    
           Dim R As Mediacy.IQL.ObjectManager.SINGLERECT=im.Aoi.BoundingRect'get ROI bounding box
            'open only ROI area
           Dim ra As McRegionAccess=im.CreateRegionAccess(,,,R.left,R.top,R.right,R.bottom)
           If ra Is Nothing Then Exit Sub ' wenn es keine ROI gibt, dann raus.
           Dim data(,) As Object
           Dim W,O,C,K,AGV,AGVS,M As Integer
           Dim GRAD As Double
    
           'get 2D array of pixel data
           ra.GetArea(data)
           M=0
           AGVS=0
           'tbBreite.Text=CStr(data.GetUpperBound(0))
           'tbHoehe.Text=CStr(data.GetUpperBound(1))
    
           ' Durchlaufe Array
           For x As Integer = 0 To data.GetUpperBound(0)
            For y As Integer = 0 To data.GetUpperBound(1)
         '       lbOut.Items.Add("Data("+CStr(x)+","+CStr(y)+")="+CStr(data(x,y)))
            Next
           Next
    
           Dim i,j As Integer
    
            For i = 3 To data.GetUpperBound(0)-2
               For j = 3 To data.GetUpperBound(1)-2
                   ' Bestimme W,O,C,K
                    K=data(i,j-2)
                    W=data(i+2,j)
                    O=data(i,j+2)
                    C=data(i-2,j)
                   ' Berechne AGV (Wurzel aus (W-C)² + (O-K)²
                    AGV=((W-C)^2+(O-K)^2)^(1/2) ' neu von Svenja und Fritz
                   ' AGV=(((W-C)^2)^(1/2))+(O-K)^2 ' Alt von Papa Andi
                   ' Addiere AGV zur AGV-Summe
                    AGVS=AGVS+AGV
                    'Kontrollausgabe
                    'tbAGV.Text = CStr(AGV)
                    'tbSumAGV.Text = CStr(AGVS)
                    ' Zähle M hoch
                    M=M+1
    '                lbwock.Items.Add("M:"+CStr(M)+"-> "+CStr(data(i,j))+ _
    '                                 " C:"+CStr(C)+" O:"+CStr(O)+ _
    '                                 " W:"+CStr(W)+" K:"+CStr(K)+ _
    '                                 " AGV:"+CStr(AGV))
    '                tbM.Text = CStr(M)
                Next
            Next
    
           GRAD = AGVS / M
          ' tbGrad.Text = CStr(GRAD)
          ThisApplication.Output.Show
          ThisApplication.Output.PrintMessage(String.Format("ROI({0},{1},{2},{3}), Gradient = {4}", R.left,R.top,R.right,R.bottom,GRAD))
         '   For i = 1 To data.GetUpperBound(0)
         '      For j = 1 To data.GetUpperBound(1)
         '       data(i,j).
         '      Next
         '   Next
         '   ra.PutArea(data)
    
        End Sub
    

    When I move ROI around the image and execute the macro, I get different results:



    Please test it with my macro.

    Yuri
  • Hi Yuri,
    could you please give me a download file for your complete macro so I can load it in my Premier? Copy/paste dont work reliable.
    Svenja
  • Dear Yuri,
    that works perfectly. Thank you very much for your help! It is now possible  to read out the data of a ROI :-)
Sign In or Register to comment.