Jump to content

_PixelSearchAdvanced


Kohr
 Share

Recommended Posts

I made this function because when I first started using PixelSearch I didn't like the way it stopped the search on the 1st match. Quote from help: "The search is performed left-to-right, top-to-bottom and the first match is returned."

I wanted it to return all matches in the area so here is what I came up with. I also modified how the step was performed so you can step either using the X or Y axis. I wasn't exactly sure how to do shades so I left that part out so if anyone wants to modify this feel free.

Forgive me if something like this has already been done.

Kohr

Function with example:

#include <Array.au3>
#Include <Date.au3>

ConsoleWrite("Start:" & _NowTime() & @CRLF)
$ret = _PixelSearchAdvanced(0, 0, 280, 1024, "EDE211", -1, 0, 0, 1)
ConsoleWrite("End:" & _NowTime() & @CRLF)
_ArrayDisplay($ret, "")
;Example below to move mouse to X/Y
;~ If $ret <> "" Then
;~  For $i = 1 To $ret[0]
;~      Dim $sArrayString[1]
;~      $ArrayString = StringSplit($ret[$i], ",")
;~      MouseMove($ArrayString[1],$ArrayString[2])
;~  Next
;~ EndIf

#cs
    _PixelSearchAdvanced ( left, top, right, bottom, color [,max] [, stepX] [, stepY] [, checker] )
    
    Parameters:
    left - left coordinate of rectangle.
    top - top coordinate of rectangle.
    right - right coordinate of rectangle.
    bottom - bottom coordinate of rectangle.
    color - Colour value of pixel to find (in hex).
    max [optional] - Maximum pixels to find (-1 for ALL).
    stepX [optional] - Instead of searching each pixel use a value larger than 0 to skip pixels along the X axis.
    stepY [optional] - Instead of searching each pixel use a value larger than 0 to skip pixels along the Y axis.
    checker [optional] - This will move the X starting point for every other Y axis so it gets a "checkered" effect.
            Example:                StepX=0     StepX=1             StepX=2
                    Normally it is  xxxxxxx     x x x x x       x  x  x  x
                                    xxxxxxx     x x x x x       x  x  x  x
                                    
                    Checkered is             x x x x          x  x  x  x
                                              x x x x            x  x  x
                                             x x x x          x  x  x  x

#ce

Func _PixelSearchAdvanced($left, $top, $right, $bottom, $color, $max = -1, $stepX = 0, $stepY = 0, $checker = 0)
    Dim $found[1]
    $checkerAlt = 0
    For $y = $top To $bottom
        For $x = $left To $right
            $ret = Hex(PixelGetColor($x, $y), 6)
            If $ret = $color Then
                _ArrayAdd($found, $x & "," & $y)
            EndIf
            $x += $stepX
            If UBound($found)-1 = $max Then
                ExitLoop
            EndIf
        Next
        If $checker <> 0 Then
            If $checkerAlt = 0 Then
                $checkerAlt = 1
                $left += $stepX
            Else
                $checkerAlt = 0
                $left -= $stepX
            EndIf
        EndIf
        $y += $stepY
    Next
    $found[0] = UBound($found) - 1
    If $found[0] = 0 Then
        Return 0
    Else
        Return $found
    EndIf
EndFunc   ;==>_PixelSearchAdvanced

EDIT:: Added max and checker parameters

Edited by Kohr
Link to comment
Share on other sites

  • Moderators

Cant test this now, Im about to go to bed. But does it go the same speed as PixelSearch? Or slower?

Multiple PixelGetColor vs. 1 PixelSearch ... what do you think?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

This might work a tad faster... and it returns a 2dim array of all the coords where the color was found. Standard [0] = x coord [1] = y coord. Ubound($array, 1) returns how many times the color was found.

$array = _PixelSearchEx(180, 31, 182, 41, 0x000000)
_ArrayDisplay2D($array)

Func _PixelSearchEx($xTop, $yTop, $xBottom, $yBottom, $nColor, $iShade = 0, $iStep = 1)
    Local $aPix, $aCoords, $nYAdd, $iAdd
    For $xCC = $xTop To $xBottom
        $nYAdd = 0
        While $nYAdd <= $yBottom
            $aPix = PixelSearch($xCC, $yTop + $nYAdd, $xCC, $yBottom, $nColor, $iShade, $iStep)
            If Not IsArray($aPix) Then ExitLoop
            If Not IsArray($aCoords) Then Local $aCoords[1][2]
            $nYAdd += ($aPix[1] - $yTop) + 1
            $iAdd += 1
            ReDim $aCoords[$iAdd + 1][2]
            $aCoords[$iAdd][0] = $aPix[0]
            $aCoords[$iAdd][1] = $aPix[1]
        WEnd
    Next
    If IsArray($aCoords) Then Return $aCoords
    Return SetError(1, 0, 0)
EndFunc

Func _ArrayDisplay2D($aArray, $sTitle = 'Array Display 2Dim', $iBase = 1, $sToConsole = 0)
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    Local $sHold = 'Dimension 1 Has:  ' & UBound($aArray, 1) -1 & ' Element(s)' & @LF & _
            'Dimension 2 Has:  ' & UBound($aArray, 2) - 1 & ' Element(s)' & @LF & @LF
    For $iCC = $iBase To UBound($aArray, 1) - 1
        For $xCC = 0 To UBound($aArray, 2) - 1
            $sHold &= '[' & $iCC & '][' & $xCC & ']  = ' & $aArray[$iCC][$xCC] & @LF
        Next
    Next
    If $sToConsole Then Return ConsoleWrite(@LF & $sHold)
    Return MsgBox(262144, $sTitle, StringTrimRight($sHold, 1))
EndFunc

Edit:

Searches top to bottom then left to right

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

this is definitely faster than the normal pixel search

great work

Faster? I doubt that, as I'm using PixelSearch() as the cohesive to mend my UDF together. It does however do something that PixelSearch() alone doesn't do... Your statement therefore is a bit confusing. (Unless you meant the original posters UDF, then I'd have the throw the BS flag on that one :whistle: )

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

this is definitely faster than the normal pixel search

great work

I doubt it is. Speed with PixelSearch is based on when it finds the color. For example a 10 X 10 grid will have 100 pixels. Finding the color on the 1st pixel will be faster than it being at the 100 location.

This udf finds all the pixels in the 10 X 10 grid so it will always search every pixel. The only way PixelSearch will do that is if it doesn't actually find it.

Kohr

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