Jump to content

is there a function for reading images into 2d arrays?


Recommended Posts

Not that I can think of. But I'm pretty sure there is a function to read a specific pixel of an image... Combine that with a loop of the X and Y components of an image, and you've got your image -> 2D array.

Cheers,

Brett

Link to comment
Share on other sites

This is one way of getting the colour pixels in an image file into an array.

#include <Array.au3>
#include <GDIPlus.au3>

Local $filename, $begin, $aArr, $time
Local $filename = FileOpenDialog("Select image", @DesktopCommonDir, "All images (*.jpg;*.png;*.gif;*.bmp;)", 1)
If $filename = "" Then Exit

ShellExecute($filename)

$begin = TimerInit()
_FileImageToArray($filename, $aArr)
$time = Round(TimerDiff($begin) / 1000, 3)

;_ArrayDisplay($aArr)
MsgBox(0, "Check bottom-right element in 2D array", "Pixel colour at coordinates (" & _
        UBound($aArr, 1) & ", " & UBound($aArr, 2) & ") of image = 0x" & _
        $aArr[UBound($aArr, 1) - 1][UBound($aArr, 2) - 1] & @CRLF & @CRLF & _
        "Time taken = " & $time & " secs")


Func _FileImageToArray($sFileName, ByRef $aArray)
    Local $Reslt, $stride, $format, $Scan0, $iIW, $iIH, $hImage
    Local $v_Buffer, $width, $height
    _GDIPlus_Startup()
    $hImage = _GDIPlus_ImageLoadFromFile($sFileName)
    $iIW = _GDIPlus_ImageGetWidth($hImage)
    $iIH = _GDIPlus_ImageGetHeight($hImage)

    ProgressOn("Progress Bar", "Filling a " & $iIW & " x " & $iIH & " size array.", "0 percent")
    $Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iIW, $iIH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)

    ;Get the returned values of _GDIPlus_BitmapLockBits ()
    $width = DllStructGetData($Reslt, "width")
    $height = DllStructGetData($Reslt, "height")
    $stride = DllStructGetData($Reslt, "stride")
    $format = DllStructGetData($Reslt, "format")
    $Scan0 = DllStructGetData($Reslt, "Scan0")

    Dim $aArray[$height][$width]
    For $j = 0 To $iIH - 1
        For $i = 0 To $iIW - 1
            $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
            $aArray[$j][$i] = Hex(DllStructGetData($v_Buffer, 1), 6)
        Next
        ProgressSet(Int(100 * $j / ($iIH)), Int(100 * $j / ($iIH)) & " percent")
    Next
    _GDIPlus_BitmapUnlockBits($hImage, $Reslt)
    ProgressOff()
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
    Return
EndFunc ;==>_FileImageToArray
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...