Jump to content

Help my array script


wizardpug
 Share

Recommended Posts

Hi I am new to this forum,

I'm using autoit for faster video editing. I'm trying to pick a color pixel near my mouse with the lowest x axis value out of several pixels with different colors. 

Here is the part of my code that I'm concerned about, it's very simple and repetitive:

$MousePos = MouseGetPos()

      $pixel = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xC739AF, 30)
      $pixel2 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xE356C6, 30)
      $pixel3 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0x9D2897, 30)
      $pixel4 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xDC47B1, 30)
      $pixel5 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xE255C5, 30)
      $pixel6 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xDB46B0, 30)
      $pixel7 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xDB47B0, 30)
      $pixel8 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xE255C6, 30)
      
If IsArray($pixel) = true then
   if $pixel[0]<$pixel2[0] and $pixel[0]<$pixel3[0] and $pixel[0]<$pixel4[0] and $pixel[0]<$pixel5[0] and $pixel[0]<$pixel6[0] and $pixel[0]<$pixel7[0] and $pixel[0]<$pixel8[0] then
   $track = $pixel
   EndIf
EndIf

If IsArray($pixel2) = true then
   if $pixel2[0]<$pixel[0] and $pixel2[0]<$pixel3[0] and $pixel2[0]<$pixel4[0] and $pixel2[0]<$pixel5[0] and $pixel2[0]<$pixel6[0] and $pixel2[0]<$pixel7[0] and $pixel2[0]<$pixel8[0] then
   $track = $pixel2
   EndIf
EndIf

If IsArray($pixel3) = true then
   if $pixel3[0]<$pixel2[0] and $pixel3[0]<$pixel[0] and $pixel3[0]<$pixel4[0] and $pixel3[0]<$pixel5[0] and $pixel3[0]<$pixel6[0] and $pixel3[0]<$pixel7[0] and $pixel3[0]<$pixel8[0] then
   $track = $pixel3
   EndIf
EndIf

If IsArray($pixel4) = true then
   if $pixel4[0]<$pixel2[0] and $pixel4[0]<$pixel3[0] and $pixel[0]<$pixel4[0] and $pixel4[0]<$pixel5[0] and $pixel4[0]<$pixel6[0] and $pixel4[0]<$pixel7[0] and $pixel4[0]<$pixel8[0] then
   $track = $pixel4
   EndIf
EndIf

If IsArray($pixel5) = true then
   if $pixel5[0]<$pixel2[0] and $pixel5[0]<$pixel3[0] and $pixel5[0]<$pixel4[0] and $pixel5[0]<$pixel[0] and $pixel5[0]<$pixel6[0] and $pixel5[0]<$pixel7[0] and $pixel5[0]<$pixel8[0] then
   $track = $pixel5
   EndIf
EndIf

If IsArray($pixel6) = true then
   if $pixel6[0]<$pixel2[0] and $pixel6[0]<$pixel3[0] and $pixel6[0]<$pixel4[0] and $pixel6[0]<$pixel5[0] and $pixel6[0]<$pixel[0] and $pixel6[0]<$pixel7[0] and $pixel6[0]<$pixel8[0] then
   $track = $pixel6
   EndIf
EndIf

If IsArray($pixel7) = true then
   if $pixel7[0]<$pixel2[0] and $pixel7[0]<$pixel3[0] and $pixel7[0]<$pixel4[0] and $pixel7[0]<$pixel5[0] and $pixel7[0]<$pixel6[0] and $pixel7[0]<$pixel[0] and $pixel7[0]<$pixel8[0] then
   $track = $pixel7
   EndIf
EndIf

If IsArray($pixel8) = true then
   if $pixel8[0]<$pixel2[0] and $pixel8[0]<$pixel3[0] and $pixel8[0]<$pixel4[0] and $pixel8[0]<$pixel5[0] and $pixel8[0]<$pixel6[0] and $pixel8[0]<$pixel7[0] and $pixel8[0]<$pixel[0] then
   $track = $pixel8
   EndIf
EndIf

So you can see that variable $track is the pixel that will be tracked, and it could be any of the 8 different colors ($pixel,$pixel2,$pixel3...$pixel8). But the script checks which of the 8 pixel colors has the lowest X value and assigns this value to variable $track which will later be used for tracking. 

 

The problem here is that not every $pixel variable is present on the screen at the same time, so I get the error "non-accessible variable" when the script is performing the "if var1 x < var2 x". I understand why this results in an error but I've hit a wall in how to continue. Can those variables that are not present be ignored so that there is no error? How to check for which has the smallest x value from those that are present?

 

I also understand that I can do "if isarray($pixel) =true and isarray($pixel2)=true then if $pixel<$pixel2 then $track=$pixel" so split it up into every possibility to include a check for array but that would result in so much at least 64 different checks or more for every possible combination, there must be a simpler way.

 

Thank you, please help

Link to comment
Share on other sites

You could try a workaround : put the pixel coordinates into a 2D array and then sort this array
Pseudo-code :

Local $aResult[8][2], $n = 0

$pixel = PixelSearch(...)
If not @error Then   ; to avoid the "non-accessible variable" error
   $aResult[$n][0] = $pixel[0]
   $aResult[$n][1] = $pixel[1]
   $n += 1
EndIf

;...etc
Redim $aResult[$n][2]

_ArraySort($aResult, ...)  ; sort the array on column 0 (x coordinates)
; $track  should be $aResult[0][0], $aResult[0][1]

 

Link to comment
Share on other sites

I'm not familiar with arraysort and I'm probably using it wrong. I followed your recipe and then to check if it's doing what we want, I did a mousmove to $aResult[0][0], $aResult[0][1] but it doesn't move to the leftest point and stop, instead it jumps around from point to point. Here it is:

$MousePos = MouseGetPos()

Local $aResult[8][2], $n = 0

$pixel = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xC739AF, 30)

If not @error Then   ; to avoid the "non-accessible variable" error
   $aResult[$n][0] = $pixel[0]
   $aResult[$n][1] = $pixel[1]
   $n += 1
EndIf

$pixel2 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xE356C6, 30)

If not @error Then   ; to avoid the "non-accessible variable" error
   $aResult[$n][0] = $pixel2[0]
   $aResult[$n][1] = $pixel2[1]
   $n += 1
EndIf

$pixel3 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0x9D2897, 30)

If not @error Then   ; to avoid the "non-accessible variable" error
   $aResult[$n][0] = $pixel3[0]
   $aResult[$n][1] = $pixel3[1]
   $n += 1
EndIf

$pixel4 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xDC47B1, 30)


If not @error Then   ; to avoid the "non-accessible variable" error
   $aResult[$n][0] = $pixel4[0]
   $aResult[$n][1] = $pixel4[1]
   $n += 1
EndIf

$pixel5 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xE255C5, 30)

If not @error Then   ; to avoid the "non-accessible variable" error
   $aResult[$n][0] = $pixel5[0]
   $aResult[$n][1] = $pixel5[1]
   $n += 1
EndIf

$pixel6 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xDB46B0, 30)

If not @error Then   ; to avoid the "non-accessible variable" error
   $aResult[$n][0] = $pixel6[0]
   $aResult[$n][1] = $pixel6[1]
   $n += 1
EndIf

$pixel7 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xDB47B0, 30)

If not @error Then   ; to avoid the "non-accessible variable" error
   $aResult[$n][0] = $pixel7[0]
   $aResult[$n][1] = $pixel7[1]
   $n += 1
EndIf

$pixel8 = PixelSearch($MousePos[0]-100, $MousePos[1]-200, $MousePos[0]+100, $MousePos[1]+100, 0xE255C6, 30)

If not @error Then   ; to avoid the "non-accessible variable" error
   $aResult[$n][0] = $pixel8[0]
   $aResult[$n][1] = $pixel8[1]
   $n += 1
EndIf


Redim $aResult[$n][2]

_ArraySort($aResult)

 

Link to comment
Share on other sites

May I suggest, have a look at the helpfile to learn how _ArraySort works (parameters !! )
Then use _ArrayDisplay before and after the sorting to check the result and the coordinates with the lowest x. If these coordinates are the correct ones then it's a trouble when using MouseMove


 

Link to comment
Share on other sites

yes the first arraydisplay gives me 7 rows, col0 and col1 columns that are empty, second arraydisplay which is after sort has 7 columns with col0 at 600-605 not in order and col1 all at 230. Col0 is x axis? It looks like arraysort doesn't sort it by x in my script?

Link to comment
Share on other sites

On 20/9/2016 at 4:03 PM, wizardpug said:

the first arraydisplay gives me 7 rows, col0 and col1 columns that are empty, second arraydisplay which is after sort has 7 columns with col0 at 600-605 not in order and col1 all at 230.

Well, I can't see the logic in there. If the first array is empty, the second one would not fill up magically.

Here is the concept I suggested in post #4 :
- pixelsearch returns x and y coordinates in a 1D array
- these coordinates if exist are stored in the $aResult 2D array.  col0 = x, col1 = y, and 8 rows maximum because 8 pixelsearch are done (less rows if some pixelsearch return an error)
- then sort $aResult to get on row 0 the coordinates with the lowest x value

OTOH, you posted a part of the script only and we know nothing about the rest, so impossible to say more. Reason why I suggest some debugs to check if everything is running OK, using _ArrayDisplay, ConsoleWrite etc

And a beer along with the cookie please  :)

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...