point trajectory from X/Y coordinates

779 views
Skip to first unread message

acm...@columbia.edu

unread,
Jun 12, 2017, 7:27:34 PM6/12/17
to Bonsai Users
Hello Bonsai community,
I am new to Bonsai but have enjoyed figuring it out with the great workshop videos and forum posts. However, I ran into a problem that I couldn't resolve but feel like it should be rather trivial. I am using Bonsai together with Arduino to read in two analog inputs. What I want to do is plot the trajectory of a point that uses the analog values as its X and Y coordinates. Basically using analog1 as x value and analog2 as y value of a point that is drawn in a visualizer online. I found a python script that draws the centroid trajectory of a detected object from a video input. But it wants point2f input, which I understand needs floating numbers, and my zip of X/Y is a tuple of doubles. I couldn't figure out how to change the code to work with my input data, or change my datatype to feed into the code.

Thank you in advance for your help!

Below you find my Bonsai tree and the Python Transform I tried to use (from this forum post: https://groups.google.com/forum/#!searchin/bonsai-users/trajectories%7Csort:relevance/bonsai-users/CxsCtVjk2IE/YKTjtbCtGwAJ)

cheers,
Alice


import clr
clr
.AddReference("OpenCV.Net")
from OpenCV.Net import *

# Accumulators for keeping history
trajectory 
= None
prevpoint 
= None
color 
= Scalar.Rgb(255,0,0)
thickness 
= 3

@returns(IplImage)
def process(value):
  
global trajectory, prevpoint
  centroid 
= value.Item1
  image 
= value.Item2

  
# Initialize trajectory image accumulator if needed
  
if trajectory is None:
    trajectory 
= IplImage(image.Size, image.Depth, 3)
    trajectory
.SetZero()

  
# Draw point or line depending on whether there is a past history
  
if prevpoint is None:
    CV
.Circle(trajectory,Point(centroid),thickness,color,-1)
  
else:
    CV
.Line(trajectory,Point(prevpoint),Point(centroid),color,thickness)

  
# Draw trajectory on top of image
  output 
= image.Clone()
  mask 
= IplImage(image.Size, image.Depth, 1)
  CV
.CvtColor(trajectory,mask,ColorConversion.Bgr2Gray)
  CV
.Threshold(mask,mask,0,255,ThresholdTypes.Binary)
  CV
.Copy(trajectory,output,mask)

  
# Update history for next drawing
  prevpoint 
= centroid
  
return output

# Reset accumulators
def unload():
  
global trajectory, prevpoint
  trajectory 
= None
  prevpoint 
= None
AnalogPlot.bonsai
AnalogPlot.bonsai.layout

Gonçalo Lopes

unread,
Jun 13, 2017, 10:18:20 AM6/13/17
to acm...@columbia.edu, Bonsai Users
Hi Alice and welcome to the forums!

From looking at the code you may be able to pass your custom centroid values directly to the Point constructor. Basically replace the calls:

Point(centroid)

with:

Point(centroid.Item1, centroid.Item2)

Hope this helps.



--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/7b534e46-ed58-4f78-a865-741032c20014%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

acm...@columbia.edu

unread,
Jun 13, 2017, 5:17:24 PM6/13/17
to Bonsai Users, acm...@columbia.edu
Hi Goncalo,
thank you for your very quick response!
a view changes with the Point values did the trick and it works nicely now.

-Alice





To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages