Jump to content

How to search a rectangular of certain color?


 Share

Recommended Posts

Hi Realm:

I know PixelSearch(), but it only searches the position of ONE single pixel, but I want to get the coordinate of an area of pixel.

For example get the position of a black box or a red circle.

Is there a way to do that?

thanks

Edited by longxx
Link to comment
Share on other sites

You could use PixelChecksum(), but personally I prefer using PixelSearch() or PixelGetColor() and searching 4 or more points of reference.

In your case I might suggest PixelGetColor(). Find 4-5 points of the object you wish find, and use PixelGetColor() to find the pixel colors for those points. Generally I use 5 points, top,left / top,right / Bottom,left / bottom,right / center for boxes, and top / left / right / bottom / center for circles.

Again use PixelGetColor() to find the top,left location, than check the other 4 points to ensure you have the object. These 2 examples, will work in most instances, but not all.

This is how I use PixelGetColor() to find a box:

$xBegin=1 ;starting width point
$yBegin=1 ;starting depth point
$width=300; for a region 300 pixels wide
$depth=300; for a region 300 pixels in depth
$color=0x000000;color to search for, 0x000000=black
$oWidth=10;change to actual width of the black box object
$oDepth=10;change to actual depth of the black box object

$cWidth = Ceiling($oWidth/2);creates center width checkpoint
$cDepth = Ceiling($oDepth/2);creates center depth checkpoint
$oWidth -=1 ;adjusts for later calculations
$oDepth -=1 ;adjusts for later calculations
$x=$xBegin
$y=$yBegin
While $y<=$depth+$yBegin
    Do
        If PixelGetColor($x,$y)=$color Then ;locates the first black pixel, top left of box
            If PixelGetColor($x+$oWidth,$y)=$color Then ;checks if top right of box is present
                If PixelGetColor($x+$cWidth,$y+$cDepth)=$color Then ;checks if center point of box is present
                    If PixelGetColor($x,$y+$oDepth)=$color Then ;checks if bottom left of box is present
                        If PixelGetColor($x+$oWidth,$y+$oDepth)=$color Then ;checks if bottom right of box is present
                            MsgBox(0,"Box","Box was located at: " & @CRLF _
                                & "Left =" & $x & @CRLF _
                                & "Top ="& $y & @CRLF _
                                & "Right ="& $x+$oWidth & @CRLF _
                                & "Bottom =" & $y+$oDepth & @CRLF _
                                & "Center =" & $x+$cWidth &","& $y+$cDepth)
                            ExitLoop
                        EndIf
                    EndIf
                EndIf
            EndIf
        EndIf
        $x +=1
    Until $x>$width+$xBegin
    $y +=1
WEnd

This is how I use PixelGetColor() to find a circle Circle:

$xBegin=1 ;starting width point
$yBegin=1 ;starting depth point
$width=300; for a region 300 pixels wide
$depth=300; for a region 300 pixels in depth
$color=0xff0000;color to search for, 0xff0000=Red
$oDiameter=10;change to actual diameter of the object Circle

$cDepth = Floor($oDiameter/2);creates center depth checkpoint
$oDiameter -=1 ;adjusts for later calculations
$x=$xBegin
$y=$yBegin
While $y<=$depth+$yBegin
    Do
        If PixelGetColor($x,$y)=$color Then ;locates the first black pixel, top of circle
            $xx=$x
            Do ;This loop will check how wide top of circle is.
                $xx +=1
                $check=PixelGetColor($xx,$y)
            Until $check<>$color
            $x +=Ceiling(($xx-$x)/2);moves checkpoint to center of circle in width
            If PixelGetColor($x,$y+$cDepth)=$color Then ;checks if center point of circle is present
                If PixelGetColor($x-$cDepth,$y+$cDepth)=$color Then ;checks if left point of circle is present
                    If PixelGetColor($x+$oDiameter-$cDepth,$y+$cDepth)=$color Then ;checks if right point of circle is present
                        If PixelGetColor($x,$y+$oDiameter)=$color Then ;checks if bottom of circle is present
                            MsgBox(0,"Box","Box was located at: " & @CRLF _
                                & "Left =" & $x-$cDepth & @CRLF _
                                & "Top ="& $y & @CRLF _
                                & "Right ="& $x+$oDiameter-$cDepth & @CRLF _
                                & "Bottom =" & $y+$oDiameter & @CRLF _
                                & "Center =" & $x &","& $y+$cDepth)
                            ExitLoop
                        EndIf
                    EndIf
                EndIf
            EndIf
        EndIf
        $x +=1
    Until $x>$width+$xBegin
    $y +=1
WEnd

I took a moment to write these examples, from my actual code in a way I hope is best understood. There might be an error or 2, but I hope this points you in the right direction.

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

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