Click or drag to resize

IMcRegionAccessGetPixel Method

Read a single pixel from an image.

Namespace:  MediaCy.IQL.Engine
Assembly:  MediaCy.IQL.Engine (in MediaCy.IQL.Engine.dll) Version: 10.0.6912.0
Syntax
VB
Function GetPixel ( 
	<OutAttribute> ByRef pvPixel As Object,
	x As Integer,
	y As Integer
) As Integer

Parameters

pvPixel
Type: SystemObject
VARIANT* : A pointer to a VARIANT receiving the pixel value. If the VARIANT is empty, the function will return one of a type compatible with the type passed to IMcImage.CreateRegionAccess. For an McRegionAccess with more than one channel, pvPixel will contain a SAFEARRAY containing one value per channels. For single channel McRegionAccess, pvPixel will contain a single numerical value.
x
Type: SystemInt32
long : The x-location of the pixel.
y
Type: SystemInt32
long : The y-location of the pixel.

Return Value

Type: Int32
long - the number of values returned in pvPixel: One value per channel in the McRegionAccess. The number channels in the McRegionAccess is determined by the channel argument of McImage.CreateRegionAccess as well as the McImageType.
Remarks
The x and y coordinates are relative to the McRegionAccess ROI, not the image. The pixel values returned are in the requested color model, which may differ from the image's native color model. When the routine itself creates the variant it is typed after the image type, which guarantees that the pixel values will fit into it. If pvPixel is already typed, it must be of the right data type and size. For languages that do not support the unsigned integer type (like Visual Basic or VBA), 16 bits values bigger than 32767 will be reported as negative because their sign bit is set. The best way to work around this limitation is to use a "long" McRegionAccess, which will then return long positive values (4 bytes) instead of unsigned integers (2 bytes). If this McRegionAccess instance is created with the McImage.CreateRegionAccessEx with the mcRegionAccessMode.mcramUnsignedGetFlag set in the AccessMode parameter, then the default return type for 12, 16 and 32 bit integral pixel types will be an unsigned integral type; if this flag is not set, then these types are signed for back-compatibility with VB6 as discussed above.
Examples
VB
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} SurfacePlot 
   Caption         =   "SurfacePlot"
   ClientHeight    =   7245
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   8250
   OleObjectBlob   =   "SurfacePlot.frx":0000
   ShowModal       =   0   'False
   StartUpPosition =   1  'CenterOwner
End






'******************************************************************
' SurfacePlot.frm demonstarting McRegionAccess.GetPixel method
'******************************************************************
Option Explicit
'limit maximum plot size
Const MaxPlotSize = 50
'we use this object to track changes of active image
Private WithEvents m_Images As McImages
Attribute m_Images.VB_VarHelpID = -1

Private Sub SetImageToPlot(Image As McImage)
    If Image Is Nothing Then
       SurfacePlot.Caption = "Surface Plot - No Image"
       MSChart1.Visible = False
       Exit Sub
    End If
    SurfacePlot.Caption = "Surface Plot of " & Image.DisplayName

    Dim srcImage As McImage, MaxSize As Long, ResizeScale As Double
    MaxSize = McMax(Image.Width, Image.Height)
    If MaxSize > MaxPlotSize Then
        'resize the image
        ResizeScale = MaxPlotSize / MaxSize
        'create a new small hidden image
        Set srcImage = Image.Geometry.Resize(Image.Width * ResizeScale, Image.Height * ResizeScale, mcsmBicubic, mcicfNoAddToCollection Or mcicfNotVisible Or mcicfNoInit, Image)
    Else
        Set srcImage = Image
    End If

    Dim bUseGetPixel As Boolean
    'set bUseGetPixel=false to use fast method (ChartData)
    bUseGetPixel = False

    If bUseGetPixel Then
        Dim srcRA As McRegionAccess
        'get src McRegionAccess as 8-bit gray
        Set srcRA = srcImage.CreateRegionAccess(mciqtGray)
        Dim Row As Long, Column As Long, val
        With MSChart1
           .ColumnCount = srcRA.Width
           .RowCount = srcRA.Height
           For Column = 1 To .ColumnCount
              .Column = Column
              For Row = 1 To .RowCount
                 .Row = Row
                 'get data value from pixel position
                 srcRA.GetPixel val, Column - 1, Row - 1
                 'set data value to plot
                 .Data = val
              Next Row
           Next Column
        End With
    Else
        'plot image using fast method
        PlotImageFast srcImage
    End If

    'set color for every serie
    Dim vCol As Variant
    For Each vCol In MSChart1.Plot.SeriesCollection
        With vCol.DataPoints(-1)
            'set red color to plot
            .Brush.FillColor.Set 255, 0, 0 'red color
            .Brush.Style = IIf(chTransparent.value, 0, 1) 'MSChart20Lib.VtBrushStyleNull, MSChart20Lib.VtBrushStyleSolid
        End With
    Next
    'show graph
    MSChart1.Visible = True

    Set srcRA = Nothing
End Sub

'fast method of plot image
'using McRegionAccess.GetArea and MSChart1.ChartData
Private Function PlotImageFast(srcImage As McImage)
    Dim srcRA As McRegionAccess
    'get src McRegionAccess as floating point to be compatible with MSChart data type (Byte can not be used with MSChart1.ChartData )
    Set srcRA = srcImage.CreateRegionAccess(mciqtFloat)
    Dim vAr() As Single
    'allocate data array
    ReDim vAr(1 To srcRA.Width, 1 To srcRA.Height) As Single
    'get all pixels within area
    srcRA.GetArea vAr
    'set data array to chart
    MSChart1.ChartData = vAr
End Function


Private Sub chTransparent_Click()
    'rebuild graph with new transparency
    SetImageToPlot ActiveImage
End Sub

'new image is activated
Private Sub m_Images_Activate(ByVal Image As McImage)
    SetImageToPlot Image
End Sub

'image is deactivated
Private Sub m_Images_Deactivate(ByVal Image As McImage)
    If Images.Count = 0 Then
        'it is the last image, clean up the form
        SetImageToPlot Nothing
    End If
End Sub

'example demonstrates using McRegionAccess.GetPixel method
Private Sub UserForm_Initialize()
    'load demo image
    Images.Open (Path & "Images\AFM_SURF.tif")
    'sync McImages collection to monitor the events
    Set m_Images = Images
    SetImageToPlot ActiveImage
End Sub
See Also