Jump to content
Sign in to follow this  
dumbledor

Pixel counter

Recommended Posts

dumbledor

I was searching for 2 hours for some simple script, which would allow me to count pixels on my screen. Didn't find it so here it is.

I must warn you it is very slow but it does the job.

USE: You input the color (default is black), then you move mouse to the upper left corner of rectangle you want to get and press Enter, then you move mouse to the bottom right corner and press enter again. Wait for few seconds and the program will tell you the number of pixels in the rectangle.

THANKS: to the author who made mouse click coordiantes.

#include <Array.au3>

; Made by Dumbledore.
Global $trial[2] = [8 , 4], $color = 0, $number = 0
Global $x = 0, $y= 0
Global $left = 0, $top = 0, $right= 0, $bottom = 0

$color = InputBox("Color (default is black)","Input color of pixels you want to count (hex or decimal):", "0")


Global $pos = 0;reset position indicator
HotKeySet("{ENTER}", "getPos");set ENTER hotkey
While $pos = 0;does the loop while the coordinates have not been set
 ToolTip("Move mouse to the upper-left corner of rectangle and press ENTER")
 Sleep(50)
WEnd
ToolTip("");remove the tooltip
HotKeySet("{ENTER}");turn off the hotkey
$left = $pos[0] 
$top = $pos[1]

Global $pos = 0;reset position indicator
HotKeySet("{ENTER}", "getPos");set ENTER hotkey
While $pos = 0;does the loop while the coordinates have not been set
 ToolTip("Move mouse to the down-right corner of rectangle and press ENTER")
 Sleep(50)
WEnd
ToolTip("");remove the tooltip
HotKeySet("{ENTER}");turn off the hotkey
$right = $pos[0] 
$bottom= $pos[1]

$trial = PixelSearch( $left, $top, $right, $bottom, $color);checks if its black
    if $trial <> 1 Then
        $number = $number+ 1
        $x = $trial[0] + 1
        $y = $trial[1]
    EndIf
Do
Do
$trial = PixelSearch( $x, $y, $x, $y, $color);checks if its black
    
    
    if $trial <> 1 Then
        $number = $number+ 1
        ConsoleWrite ("tevilo pixlov:"& $number &@CR)
    EndIf
    $x = $x +1
;ConsoleWrite ("Vrednost x:"& $x &@CR)
;ConsoleWrite ("Vrednost y:"& $y &@CR&@CR)
Until $x = $right
$x = 60
$y = $y + 1
Until $y = $bottom
    
MsgBox(0,"Number of pixels in square:", $number)




Func getPos();get position function
 $pos = MouseGetPos();set $pos to escape the loop and give coordinates
EndFunc

Share this post


Link to post
Share on other sites
oMBRa

your script is wrong, look at this:

$trial = PixelSearch( $x, $y, $x, $y, $color);checks if its black
    
    
    if $trial <> 1 Then
        $number = $number+ 1
        ConsoleWrite ("tevilo pixlov:"& $number &@CR)
    EndIf

PixelSearch returns an array so the expression $trial <> 1 is always true

Share this post


Link to post
Share on other sites
dumbledor

your script is wrong, look at this:

$trial = PixelSearch( $x, $y, $x, $y, $color);checks if its black
    
    
    if $trial <> 1 Then
        $number = $number+ 1
        ConsoleWrite ("tevilo pixlov:"& $number &@CR)
    EndIf

PixelSearch returns an array so the expression $trial <> 1 is always true

Nope it is not true. Function returns 1 if the pixel is not the right color. It returns coordinates only when it is right color. And script is working.

Share this post


Link to post
Share on other sites
oMBRa

Nope it is not true. Function returns 1 if the pixel is not the right color. It returns coordinates only when it is right color. And script is working.

yes sorry, you are right

Share this post


Link to post
Share on other sites
MystikSpiral

This works on autoit-v3.3.0.0 (though not the most recent version, since it suffers from a weird bug with arrays and PixelSearch). This was awesome as it saved me from having to make this exact same thing. Kinda sad this has so few replies.

It's really quite handy if you think about it. I can use this for image recognition. Pick three colors, see how many times they appear in the image. The odds of the same three colors occurring the same number of times is minuscule.

Edit:

I see my last post was flamed for necro posting because even though it took me a good two hours of searching the same question multiple times, it should be buried because age. I suppose it's better to just reinvent the wheel/ask the same question yet again, than bring an actual solution to the top. 

Never mind that Google ranks fresh content higher than old content and by bringing up an old post, you're actually helping the right solution come up for other people. Screw that, right? Why not just set up an autolock feature if improving GR on old posts is a terrible sin violating some type of forum OCD?

Edited by MystikSpiral

Share this post


Link to post
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
Sign in to follow this  

×