Jump to content

Recommended Posts

I am coming across a certain error in a certain script

#comments-start
  Augmented Reality Setup
  Pre-Alpha testing
  Himanshu Goel
  23rd March 2012- Friday
#comments-end
#include <guiconstantsex.au3>
#include <windowsconstants.au3>
#include <gdiplus.au3>
#include <array.au3>
#include <winapi.au3>
#include "_Fann.au3"
Dim $pixelarray
Local $file_in="marker.jpg"
Local $file_out="detect1.jpg"
_GDIPlus_Startup()
_FileImageToArray($file_in, $pixelarray)
for $x=1 to $width
for $y=1 to $height
  if $pixelarray[$x][$y]="000000" Then
   $pixelarray[$x][$y]="FFCC33"
  EndIf
Next
Next
_FileArrayToImage($file_out, $pixelarray)
MsgBOx(0,"Done", "Done!")
_GDIPlus_Shutdown()
;Functions
;Convert Image to pixel color information array
Func _FileImageToArray($filename, ByRef $aArray)
    Local $Reslt, $stride, $format, $Scan0, $iW, $iH, $hImage
    Local $v_Buffer, $width, $height
    Local $i, $j
    $hImage = _GDIPlus_ImageLoadFromFile($filename)
    $iW = _GDIPlus_ImageGetWidth($hImage)
    $iH = _GDIPlus_ImageGetHeight($hImage)
    $Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $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[$width][$height]
    For $i = 0 To $iW - 1
        For $j = 0 To $iH - 1
            $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
            $aArray[$i][$j] = Hex(DllStructGetData($v_Buffer, 1), 8)
        Next
    Next
    _GDIPlus_BitmapUnlockBits($hImage, $Reslt)
    _GDIPlus_ImageDispose($hImage)
    Return
EndFunc   ;==&gt;_FileImageToArray
;Convert pixel information array to Image
Func _FileArrayToImage($filename, $aArray)
    Local $iW = UBound($aArray, 1), $iH = UBound($aArray, 2), $sResult = ""
    Local $hBMP, $hImage1, $Reslt, $width, $height, $stride, $format, $Scan0
    Local $sResult, $v_BufferA
    Local $i, $j
    $hBMP = _WinAPI_CreateBitmap($iW, $iH, 1, 32)
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
    $Reslt = _GDIPlus_BitmapLockBits($hImage1, 0, 0, $iW, $iH, $GDIP_ILMWRITE, $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")
    $v_BufferA = DllStructCreate("DWORD[" &amp; $height * $width &amp; "]", $Scan0)
    For $j = 0 To $height - 1
        For $i = 0 To $width - 1
            DllStructSetData($v_BufferA, 1, Execute("0x" &amp; $aArray[$i][$j]), ($j * $width) + $i + 1) ; "+1" - base 1.
        Next
    Next
    _GDIPlus_BitmapUnlockBits($hImage1, $Reslt)
    _GDIPlus_ImageSaveToFile($hImage1, $filename)
    _GDIPlus_ImageDispose($hImage1)
    _WinAPI_DeleteObject($hBMP)
    Return
EndFunc   ;==&gt;_FileArrayToImage

It gives me an invalid subscript error

where as this code works perfectly

Error Code

C:Documents and SettingsOwner.HOMEMy DocumentsAugmented Reality ResearchPattern RecognitionNeural Network WORKINGpre-alpha 1.au3 (48) : ==> Array variable subscript badly formatted.:
Dim $aArray[$width][$height]
Dim $aArray[^ ERROR
>Exit code: 1   Time: 2.

#include <gdiplus.au3>
#include <array.au3>
#include <winapi.au3>
$file=FileOpenDialog("Choose Your Image", @WorkingDir , "JPG (*.jpg)")
Opt("MustDeclareVars", 0)
_GDIPlus_Startup()
Dim $pixelarray
Local $file_in = $file ; "image1-before.jpg" ;
Local $file_out ="test.jpg"; "image1-after.jpg"
$save=FileOpen("information.txt",1)
_FileImageToArray($file_in, $pixelarray)
$x_size=UBound($pixelarray,1)
$y_size=UBound($pixelarray,2)
$x_size-=1
$y_size-=1
for $x=1 to $x_size
for $y=1 to $y_size
  FileWrite($save,$pixelarray[$x][$y])
Next
Next
FileClose($save)
FileCLose($file)
;FUNCTIONS
  ; code by Malkey: thanks man!
Func _FileImageToArray($filename, ByRef $aArray)
    Local $Reslt, $stride, $format, $Scan0, $iW, $iH, $hImage
    Local $v_Buffer, $width, $height
    Local $i, $j
    $hImage = _GDIPlus_ImageLoadFromFile($filename)
    $iW = _GDIPlus_ImageGetWidth($hImage)
    $iH = _GDIPlus_ImageGetHeight($hImage)
    $Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $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[$width][$height]
    For $i = 0 To $iW - 1
        For $j = 0 To $iH - 1
            $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
            $aArray[$i][$j] = Hex(DllStructGetData($v_Buffer, 1), 8)
        Next
    Next
    _GDIPlus_BitmapUnlockBits($hImage, $Reslt)
    _GDIPlus_ImageDispose($hImage)
    Return
EndFunc   ;==&gt;_FileImageToArray
; code by Malkey: thanks again ;)
Func _FileArrayToImage($filename, $aArray)
    Local $iW = UBound($aArray, 1), $iH = UBound($aArray, 2), $sResult = ""
    Local $hBMP, $hImage1, $Reslt, $width, $height, $stride, $format, $Scan0
    Local $sResult, $v_BufferA
    Local $i, $j
    $hBMP = _WinAPI_CreateBitmap($iW, $iH, 1, 32)
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
    $Reslt = _GDIPlus_BitmapLockBits($hImage1, 0, 0, $iW, $iH, $GDIP_ILMWRITE, $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")
    $v_BufferA = DllStructCreate("DWORD[" &amp; $height * $width &amp; "]", $Scan0)
    For $j = 0 To $height - 1
        For $i = 0 To $width - 1
            DllStructSetData($v_BufferA, 1, Execute("0x" &amp; $aArray[$i][$j]), ($j * $width) + $i + 1) ; "+1" - base 1.
        Next
    Next
    _GDIPlus_BitmapUnlockBits($hImage1, $Reslt)
    _GDIPlus_ImageSaveToFile($hImage1, $filename)
    _GDIPlus_ImageDispose($hImage1)
    _WinAPI_DeleteObject($hBMP)
    Return
EndFunc   ;==&gt;_FileArrayToImage

I can't seem to figure out my mistake

Any Ideas?

Edited by HimashuGoel
Link to comment
Share on other sites

What does $width and $height contain just before the Dim? I know what you want it to contain, but what are the actual values in those variables when you run this? I'm guessing one or both are 0 which is why you get the error.

Also, don't use Dim to declare variables, use Global or Local depending upon which scope the variable needs to live, Dim should be avoided at all costs.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

thanks guys,I was able to fix it I threw in Ubound to get the dimensions of the array and then subtracted 1 from each side so that I wouldn't exceed the array size.

Changed Code:

#comments-start
Augmented Reality Setup
Pre-Alpha testing
Himanshu Goel
23rd March 2012- Friday
#comments-end

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Array.au3>
#include <WinAPI.au3>
#include "_Fann.au3"

Global $pixelarray
Local $file_in="marker.jpg"
Local $file_out="detect1.jpg"
_GDIPlus_Startup()
_FileImageToArray($file_in, $pixelarray)
Global $width=UBound($pixelarray,1)                                  ;Changes From Here
Global $height=UBound($pixelarray,2)
$width-=1                                                                               
$height-=1                                                                             ;Till Here
for $x=1 to $width
for $y=1 to $height     
if $pixelarray[$x][$y]="000000" Then
$pixelarray[$x][$y]="FFCC33"
EndIf
Next
Next
_FileArrayToImage($file_out, $pixelarray)
MsgBOx(0,"Done", "Done!")
_GDIPlus_Shutdown()
;Functions
;Convert Image to pixel color information array
; code by Malkey: thanks man!
Func _FileImageToArray($filename, ByRef $aArray)
Local $Reslt, $stride, $format, $Scan0, $iW, $iH, $hImage
Local $v_Buffer, $width, $height
Local $i, $j

$hImage = _GDIPlus_ImageLoadFromFile($filename)
$iW = _GDIPlus_ImageGetWidth($hImage)
$iH = _GDIPlus_ImageGetHeight($hImage)
$Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $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[$width][$height]
For $i = 0 To $iW - 1
For $j = 0 To $iH - 1
$v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
$aArray[$i][$j] = Hex(DllStructGetData($v_Buffer, 1), 8)
Next
Next
_GDIPlus_BitmapUnlockBits($hImage, $Reslt)
_GDIPlus_ImageDispose($hImage)
Return
EndFunc ;==>_FileImageToArray
;Convert pixel information array to Image
Func _FileArrayToImage($filename, $aArray)
Local $iW = UBound($aArray, 1), $iH = UBound($aArray, 2), $sResult = ""
Local $hBMP, $hImage1, $Reslt, $width, $height, $stride, $format, $Scan0
Local $sResult, $v_BufferA
Local $i, $j

$hBMP = _WinAPI_CreateBitmap($iW, $iH, 1, 32)
$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
$Reslt = _GDIPlus_BitmapLockBits($hImage1, 0, 0, $iW, $iH, $GDIP_ILMWRITE, $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")

$v_BufferA = DllStructCreate("DWORD[" & $height * $width & "]", $Scan0)

For $j = 0 To $height - 1
For $i = 0 To $width - 1
DllStructSetData($v_BufferA, 1, Execute("0x" & $aArray[$i][$j]), ($j * $width) + $i + 1) ; "+1" - base 1.
Next
Next

_GDIPlus_BitmapUnlockBits($hImage1, $Reslt)
_GDIPlus_ImageSaveToFile($hImage1, $filename)

_GDIPlus_ImageDispose($hImage1)
_WinAPI_DeleteObject($hBMP)
Return
EndFunc ;==>_FileArrayToImage

But I had borrowed the functions from Malkey's code and well the image isn't saved with the changed colors.

I am working on a Augmented Reality script ,This is just testing, I plan to slowly increase functionality, as a start I want to be able to find all pixels of a certain color and highlight them , then detect edges, find corners, and eventually piece together the detections to find the pattern

Edit: The FileArrayToImage() function saves a blank image, and I can't understand whats wrong :oops::bye:

Edited by HimashuGoel
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

×
×
  • Create New...