Retrieve pixel positions of pixels with specific color

Sorry you’re right! I was using a ChopTo Dat to look at the data coming out but obviously this is not something you used, my apologies.

I am trying to extract the location of some lights from a technical drawing of a custom lighting panel. I want to convert that information into a DAT, then feed that DAT into a Topto CHOP to do some pixel mapping. My intention is that I can swap different shaped panel designs into my network, have touchdesigner identify the location of the lights automatically, then use those locations to pixel map animations and patterns through the lights. In my prototype system I inputted all of the light locations into a DAT manually, but that took a long time and the next iteration will be much larger. I thought if I could get touchdesigner to locate all pixels of one colour in an image, then I could get the person making the technical drawings to put a coloured dot into the drawing at each light, and then touchdesigner can locate that dot in 2d space.

When I found your solution above and thought I was in luck but I don’t understand the values that appear in the tx, ty, and tz channels of your delete1 chop. My data appears visually similar to what you have demonstrated, but my numbers are for example: tx=-2.4438103e-7, ty=-4.371139e-8, tz=1.5, index=9, r=1. Index and r make sense, but the numbers for tx, ty, and are so small.

I wondered if I was not using the delete chop correctly. But any help you might have towards understanding this problem would be appreciated!

Hi @AMAV,

I moved this into a new topic as it’s worthy of it’s own question.

I’m guessing that the lights are not just indicated by single pixels, so this makes the TOP based approach a bit more difficult. Hence I would suggest converting the keyed out pixels into 3d primitives via the Trace SOP and then retrieve their centers with a Script CHOP.

TO do this first append a Chroma Key TOP to your Movie File In TOP holding the image. djust the parameters here to nicely separate out the pixels of interest. This pass into a Trace SOP which should result in a nice geometric representation of the lights. Each of the generated primitives has a .center attribute which we can get to using a Script CHOP.

The Script CHOP has some prepared callbacks: First the onSetupParameters which we will use to create a SOP type parameter to reference the Trace SOP. The other callback of interest is the onCook callback which is used to generate the data in the CHOP.

For the onSetupParameters we add this script:

# press 'Setup Parameters' in the OP to call this function to re-create the parameters.
def onSetupParameters(scriptOp):
	page = scriptOp.appendCustomPage('Custom')
	p = page.appendSOP('Sop', label='SOP')
	return

This instructs the Script CHOP to create a SOP type parameter after the Setup Parameters pulse parameter on the Script CHOP’s “Script” parameter page is pressed.

Next for the onCook callback we add a script like this:

def onCook(scriptOp):
	scriptOp.clear()
	# create two channels
	tx = scriptOp.appendChan('tx')
	ty = scriptOp.appendChan('ty')
	
	# create two empty lists to collect the coordinates
	txVals = []
	tyVals = []
	
	# loop through all primitives and add their centers
	# to the two coordinate lists
	for i in scriptOp.par.Sop.eval().prims:
		center = i.center
		txVals.append(center.x)
		tyVals.append(center.y)
	
	# assign each list of posittions to the corresponding channel
	tx.vals = txVals
	ty.vals = tyVals
	
	# set the length of the CHOP to the number of coordinates stored in the list
	scriptOp.numSamples = len(txVals) 
	return

I hope the comments in the code make sense.

With this you now have a CHOP with the coordinates - you might want to scale them by the image resolution using a Math CHOP to get to the pixel coordinates in your input image.

Hope this helps
cheers
Markus
base_pixelCoords.tox (1.7 KB)

1 Like

I assume you might be using a technical drawing (rendered in high res color) and RGB camera could be usefull too.

Markus’ solutions above and in previous thread are pretty elegant, but there are more ways to solve this (which is great in Touchdesigner). First is just a simple rgb / chroma key with blob detecion TOP - this should work pretty well, but processing of large image might take a while. If you want to have precise locations od hi res points, you may want to use key and TOP to CHOP method and delete the unnecessary stuff. In a quick excercise, here’s the network:

It includes point repack by alpha for better performance

Hope it helps,
Boros

color finder.6.toe (11.0 KB)

1 Like