Jump to content

PixelSearch for all occurrences


Recommended Posts

I'm trying to find all occurrences of a pixel colour in the search rectangle. PixelSearch only does rectangles and finishes as soon as one is found, so this how my macro is intended to work:

Find first occurrence (and report)

Treat rest of horizontal line as a rectangle

Find next occurrence (and report)

Repeat until end of line reached and nothing else found

Start search in rectangle remaining below the line

etc

First of all, is there an easier way to do this?

My macro works up to a point. I have a problem when the end of the horizontal line is reached having found no more matches. As it leaves the "SINGLE LINE SEARCH" the value of x is apparently in error (which presumably ends the search and should set @error to 1). It should jump down to the bottom and set $partwayline to False and next time through do a Rectangle search. Instead it comes up with an error for $coordxy[0] at "THIS IS THE PROBLEM LINE".

The only idea I have at the moment is that because the top and bottom of the search rectangle are the same value, PixelSearch does not like it when nothing found. If that is so I will have to do a pixel by pixel search for the rest of the line using PixelGetColor.

;This is preceded by a While loop that repeatedly calls SearchLoop()

Func SearchLoop()

If $partwayline = True Then
    ;Finish rest of line after found
    $coordxy = PixelSearch( $partx, $parttop, $right, $parttop, $pixelcolor, $colorvar, $step)  ;SINGLE LINE SEARCH
Else
    ;Rectangular Search
    $coordxy = PixelSearch( $left, $top, $right, $bottom, $pixelcolor, $colorvar, $step)
EndIf
If Not @error Then
    IniWrite("C:\Temp\pixelsearch.ini", "Parameters", "X", $coordxy[0]) ;THIS IS THE PROBLEM LINE
    IniWrite("C:\Temp\pixelsearch.ini", "Parameters", "Y", $coordxy[1])
    IniWrite("C:\Temp\pixelsearch.ini", "Parameters", "RunAgain", "True")   ;true for testing only
        If $coordxy[0] < $right Then    ;not at end of line
            $partwayline = True
            $partx = $coordxy[0] + 1
            $parttop = $coordxy[1]
        Else
            $partwayline = False
            $top = $coordxy[1] + 1  ;move top down for next full rectangle
        EndIf
Else
        If $partwayline = False Then IniWrite("C:\Temp\pixelsearch.ini", "Parameters", "Error", "SearchFailed")
        Exit
        EndIf
        $partwayline = False
EndIf
EndFunc
Link to comment
Share on other sites

Heres my quick take (untested)

HotKeySet("{ESC}", "_exit")

$x1 = 0
$y1 = 0
$x2 = @DesktopWidth - 1
$y2 = @DesktopHeight - 1

For $j = $y1 To $y2
    For $i = $x1 To $x2
        $pos = PixelSearch($x1, $y1, $i, $j, 0xFFFFFF, 50)
        If IsArray($pos) Then
            ConsoleWrite($pos[0] & " x " & $pos[1] & @CRLF)
            $x1 = $pos[0] + 1
        EndIf
    Next
    If $i >= $x2 Then
        $x1 = 0
        $y1 += 1
    EndIf
Next

Func _exit()
    Exit
EndFunc   ;==>_exit

Edit: edited code

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Thanks for the suggestion. The problem with doing the increment x and y is time. Using that method it takes about 13 secs to scan my screen. With PixelSearch doing the whole thing, it's less than 1 sec. All is not lost though - I'll see if I can use the incrementing x with PixelSearch as you did to finish off the part-checked line. That enables me to continue incorporating colour variation etc. Your mention of arrays also gave me some ideas for reporting the data.

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