Jump to content

Speed up Process (PixelGetColor)


Go to solution Solved by binhnx,

Recommended Posts

Hi, 
I want to know if there exists a way to speed up this process?

Local $update[50][50]

$tStart = TimerInit()
For $x = 493 To 529 Step 1
    For $y = 263 To 271 Step 1
        $Color = PixelGetColor($x, $y)
        $p1 = $x - 493
        $p2 = $y - 263
        $update[$p1][$p2] = $Color
    Next
Next
MsgBox(0, "t", TimerDiff($tStart))
$update = 0
Edited by flatt83
Link to comment
Share on other sites

How long is it taking for you? It took 8 seconds for me. It has to run the loops 288 times, it's going to take some time to do that.

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

on the laptop of my gf it takes also more than 8secs but on my pc less than 6 but thats not the problem.

i'm trying to build an imagesearch-function and this is what i wanted to use to get the specific image saved to an array, which i wanted to search onscreen the same way, but this would need  way more time than it should. i know that there already exists a prebuild ImageSearch.au3 in this forum but that also has a precompiled dll, which is not readable in source this way, that is why i wanted to build this up myself.

Link to comment
Share on other sites

I dont know if I'm doing something wrong, but I can run the above script 1900 times in 8 seconds.

Local $update[50][50]

$tStart = TimerInit()
For $Loop = 1 To 1900
For $x = 493 To 529 Step 1
    For $y = 263 To 271 Step 1
        $Color = PixelGetColor($x, $y)
        $p1 = $x - 493
    ;   ConsoleWrite(TimerDiff($tStart) & " " & $x & " " & $y & " " & $color & @CRLF)
        $p2 = $y - 263
        $update[$p1][$p2] = $Color
    Next
 Next
 Next

;MsgBox(0, "t", TimerDiff($tStart))
ConsoleWrite(TimerDiff($tStart))
$update = 0

7771.99788037979>Exit code: 0    Time: 8.006

i5-750 (2.4ghz)

I'm not sure my computer is 1900 times faster than your guys.

Some guy's script + some other guy's script = my script!

Link to comment
Share on other sites

  • Solution

@Werty, @BrewManNH:

It's not the about the computer or GPU speed. Two you disable the DWM (aka. Aero Glass), don't you? It seems that the OP have an DWM enabled desktop. And PixelGetColor slow speed is totally expected.

PixelGetColor is a wrapper of WinAPI function GetPixel.

When DWM is disabled, Windows maintains the bitmap of all windows in a on-screen buffer, which is displayed later by the graphics card (GPU). GetPixel read the color data from the on-screen buffer. Although reading data from video memory is considered slow, it's still not too slow and you can get a desired performance.

When DWM is enabled, Windows asks each application to draw in a off-screen bitmap. Then all the bitmap will be gathered and "composed" together to the on-screen buffer. After that, GPU display the buffer to your screen. This approach solve the tearing problem and has many advantages, but it also has some problem :

When you call GetPixel then it force rendering all off-screen buffer to the on-screen buffer, and read the data from that bitmap. The extra work of rendering make it's much slower than get data from the on-screen buffer only.

So, the workaround, you should capture your window first, to a memory bitmap, get the HDC of the bitmap, then call GetPixel (using DllCall, not the AutoIt PixelGetColor, since you cannot use PixelGetColor to read color from a specified HDC)

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Link to comment
Share on other sites

If you are comparing one image against several other images, you don't necessarily need to test every pixel of every image. Test one pixel and if no match is found then there are no results. Test the second pixel with all the matching images found if they exist. Use ExitLoop when you hit a mismatch.

Another method might be to use the hash values and test them instead of using pixels - this won't work with different file types though. It's just a thought. You can probably narrow down your search using various tricks: FileGetSize etc...

Edited by czardas
Link to comment
Share on other sites

PixelChecksum might be a better idea than pixel searching every pixel in an area.

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

These Lines Are just Run to Save the colors of the Button for later implementing them into the original program. I was just curious why it Takes so Long to get the colors. Its a picture of a Button in a hwnd_wrapper which i cant get Control of. I wanted to search for the first Pixel, and if found to keep searching for the Rest of the Array.

Sry for Bad english, i'm not naturally speaking english and i'm half asleep on my Way to work.

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