Forgot password?

Create an account!

Forum

« back

RhinoScript – image RGB to Array?

Messages

Please log in to write a message.

  • 2. Hanno (Apr 20, 2010 19.07):

    Hi Josh,

    you are mixing up different ways to treat arrays. Have a look at your ArrayAdd function: it needs the whole array as first parameter and appends the second parameter to it. So you do not need to call ReDim Preserve before, and you have to put in your whole columns array as first parameter.
    Furthermore, you are filling your arrColorRows array for each row, and then trying to add it to the Columns array within your inner loop. So in fact each time you process a pixel, a whole new row is added.

    Hope that helps!

    Hanno

  • 1. drape (Apr 19, 2010 01.00):

    I'm trying to get the RGB values of every pixel in an image in to a nested array. I'm using the GDI Bitmap extension by David Rutten - wiki.mcneel.com/developer/rhinoscriptimageextension

    What am I doing wrong? Code is below and sample image is ttached

    Thanks,
    Josh Draper

    Option Explicit

    Call imageRGBtoArray()
    Sub imageRGBtoArray()

            Dim i,j, r,b,g, xWidth, yHeight
           
            Rhino.ClearCommandHistory

            'Instantiate the RhPicture Object
            Dim RhPicture : Set RhPicture = Rhino.GetPlugInObject("RhPicture")
            If IsNull(RhPicture) Then Exit Sub
           
            'Load an arbitrary image
            If Not RhPicture.LoadImage() Then
                    Call Rhino.Print("Image not loaded")
                    Exit Sub
            End If
                   
            xWidth = RhPicture.Width()
            yHeight = RhPicture.Height()
           
            Dim arrColorRows()
            Dim arrColorRowsColumns()
                   
            'Create the array to hold RGB values for the whole image       
            For i=0 To yHeight - 1         
                    For j=0 To xWidth  - 1 
                           
                            r = RhPicture.Red(j,abs(i-(yHeight-1))) '(x,abs(y-(yHeight-1)))
                            g = RhPicture.Green(j,abs(i-(yHeight-1)))
                            b = RhPicture.Blue(j,abs(i-(yHeight-1)))

                            ReDim Preserve arrColorRows(j) 
                            arrColorRows(j)= Array(r,g,b)
                            Rhino.Print "arrColorRows(" & j & "): " & arrColorRows(j)(0) & "," & arrColorRows(j)(1) & "," & arrColorRows(j)(2)

                            ReDim Preserve arrColorRowsColumns(i)
                            Call ArrayAdd (arrColorRowsColumns(i),arrColorRows(j))
                            'This is where I'm having the problem. Am I nesting the arrays properly? How am I printing the nested arrays wrong?
                            'If the below line is commented out, the scripts runs.
                            Rhino.Print "arrColorRowsColumns(" & i & "," & j & "): " & arrColorRowsColumns(i)(j)(0) & "," & arrColorRowsColumns(i)(j)(1) & "," & arrColorRowsColumns(i)(j)(2)
                           
                    Next
            Next
    End Sub

    Sub ArrayAdd(ByRef arr, ByVal val)
            Dim ub
            If IsArray(arr) Then
                    On Error Resume Next
                    ub = UBound(arr)
                    If Err.Number <> 0 Then ub = -1
                    ReDim Preserve arr(ub + 1)
                    arr(UBound(arr)) = val
            End If
    End Sub
    image RGB to Array?
Recommend

Why are these buttons gray?