Finding the center of an Object Using its Border Color

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cleetz
Posts: 30
Joined: 04 Feb 2023, 15:09

Finding the center of an Object Using its Border Color

Post by cleetz » 02 Feb 2024, 04:22

Hello. I'm looking for a code that will find the center of an object using pixelsearches. I am able to fully color the object in so that its a solid mass of the same color pixel if that helps at all to find the center in a different way, or just the border around the object could be the same color without it being filled in. I'm currently using this code, however its very inaccurate and often doesnt correctly find the center of objects. Looking for something that can get right towards the middle everytime.

Code: Select all

PixelSearch, TopX, TopY, 275, 192, 380, 285, 0xED1CF2, 2, Fast
sleep 5

; Second Pixelsearch for the bottom left
PixelSearch, BottomX, BottomY, 272, 295, 399, 207, 0xED1CF2, 2, Fast
sleep 5

; Third Pixelsearch for the bottom right
PixelSearch, LeftX, LeftY, 385, 288, 284, 193, 0xED1CF2, 2, Fast
sleep 5

; Fourth Pixelsearch for the top right
PixelSearch, RightX, RightY, 390, 211, 285, 295, 0xED1CF2, 2, Fast
sleep 5

; Calculate the weighted middle
MidX := (LeftX + RightX + 2 * TopX + 2 * BottomX) // 6
MidY := (TopY + BottomY + 2 * LeftY + 2 * RightY) // 6
look forward to hearing response if anybody has the code to do so. would highly appreciate it. Much blessings.

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Finding the center of an Object Using its Border Color

Post by mikeyww » 02 Feb 2024, 07:26

I suppose that you do not necessarily use a rectangle. An approach to debugging your script would be to examine the values of every variable in the script. You can then determine whether the search "failed" (found the wrong pixel) or whether your formula is "incorrect" such that you decide to change it.

colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Finding the center of an Object Using its Border Color

Post by colt » 02 Feb 2024, 10:25

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

Post Reply

Return to “Ask for Help (v1)”