If the object is a solidly colored rectangle this will get the exact bounding box. If it is a polygon then it should get close. It is kinda slow but it will work.
If speed is ultimate goal you will probably have to resort to mcode. A couple years ago I created an mcode script that finds all blobs of a specified color and their respective centroids. It scanned the entire screen at like 30fps. Would have to dig that up though.
Code: Select all
#NoEnv
#SingleInstance Force
SetBatchLines -1
recColor := 0xED1CF2
return
f7::
coordmode,pixel,screen
coordmode,mouse,screen
mousegetpos,sx,sy
tooltip % sx "`n" sy
PixelGetColor, clr, % sx, % sy,RGB
if(clr != recColor)
{
msgbox % "hover over the rectangle before starting.`nsearch color " . recColor . " color hovered " . clr
}
else
{
rec := findRectangle(recColor,sx,sy)
msgbox % "Upper left : " . rec.p1.x . "," . rec.p1.y . "`n" . "Bottom Right : " . rec.p2.x . "," . rec.p2.y
mousemove % rec.p1.x , % rec.p1.y,10
sleep 250
mousemove % rec.p2.x , % rec.p2.y,10
}
return
findRectangle(searchColor,guessX,guessY)
{
pos := findEdge(searchColor,guessX,guessY,-50,0) ;rough scan left 25 px step
leftEdge := findEdge(searchColor,pos.x ,pos.y,-1,0) ;precice scan to find edge from last know good pos
pos := findEdge(searchColor,guessX,guessY,50,0)
rightEdge := findEdge(searchColor,pos.x ,pos.y,1,0)
pos := findEdge(searchColor,guessX,guessY,0,-50)
topEdge := findEdge(searchColor,pos.x ,pos.y,0,-1)
pos := findEdge(searchColor,guessX,guessY,0,50)
bottomEdge := findEdge(searchColor,pos.x ,pos.y,0,1)
return {p1:{x:leftEdge.x,y:topEdge.y},p2:{x:rightEdge.x,y:bottomEdge.y}}
}
findEdge(searchClr,sx,sy,xstep,ystep)
{
lastSuccess := ""
Loop
{
s := a_index - 1
px := sx+s*xstep
py := sy+s*ystep
PixelGetColor, clr, % px, % py,RGB
if(clr != searchClr)
{
return lastSuccess
}
lastSuccess := {x:px,y:py}
}
}