how to use voodoo camera tracker? - computer-vision

I have the voodoo camera tracker software which takes a video as an input and gives ouput in the following format:
# Text export
# created by voodoo camera tracker - www.digilab.uni-hannover.de
# Creation date: Mon Feb 28 18:41:56 2011
# The camera (one line per frame)
#
# Description of the CAHV camera model:
# -------------
# (Cx, Cy, Cz) : CameraPosition [mm]
# (Ax, Ay, Az) : RotationAxis2 [unit vector]
# (Hx, Hy, Hz) : RotationAxis0 [pel] (including FocalLength, PixelSizeX, and Principal Point offset)
# (Vx, Vy, Vz) : RotationAxis1 [pel] (including FocalLength, PixelSizeY, and Principal Point offset)
# (K3, K5) : Radialdistortion; K3 [1/(mm)^2] K5 [1/(mm)^4]
# (sx, sy) : PixelSize [mm/pel]
# (Width, Height) : ImageSize [pel]
# -------------
# (ppx, ppy) : Principal Point offset [pel]
# f : Focal Length [mm]
# fov : Horizontal Field of View [degree] = (2*atan(0.5*Width*sx/f)*180/PI;
# (H0x, H0y, H0z) : RotationAxis0 [unit vector]
# (V0x, V0y, V0z) : RotationAxis1 [unit vector]
# -------------
# (x, y) : image coordinates [pel]
# (X, Y, Z) : 3D coordinates [mm]
# -------------
# Projection of 3D coordinates in the camera image:
# [ x' ] = [ Hx Hy Hz ] [ 1 0 0 -Cx] [ X ]
# [ y' ] = [ Vx Vy Vz ] [ 0 1 0 -Cy] [ Y ]
# [ z' ] = [ Ax Ay Az ] [ 0 0 1 -Cz] [ Z ]
# [ 1 ]
# or
# [ x' ] = [f/sx 0 ppx] [ H0x H0y H0z ] [ 1 0 0 -Cx] [ X ]
# [ y' ] = [0 f/sy ppy] [ V0x V0y V0z ] [ 0 1 0 -Cy] [ Y ]
# [ z' ] = [0 0 1 ] [ Ax Ay Az ] [ 0 0 1 -Cz] [ Z ]
# [ 1 ]
# then x = x'/z' and y = y'/z' , if the origin of the image coordinates is in the center of the image
# or x = x'/z' + 0.5*(Width-1) and y = y'/z' + 0.5*(Height-1) , if the origin of the image coordinates is in the upper left corner
# -------------
# Cx Cy Cz Ax Ay Az Hx Hy Hz Vx Vy Vz K3 K5 sx sy Width Height ppx ppy f fov H0x H0y H0z V0x V0y V0z
Now this is a CAHV camera model which gives the values for every frame. I want to know how to extract the camera parameters like translation, rotation, zoom from this output??
Thanks in advance..

This link gives us the details how to use the tracker.
As you can see, the last step says:: Export the estimated camera parameters to the 3D animation package. So I exported into a Maya Script file, and then I used Notepad++ to open the file which gave the details regarding the camera parameters for each frame.

Related

Using nested if or ifelse in Netlogo to specify probabilities

I am having trouble using nested ifs in Netlogo to specify deer reproductive probabilities. Here is what I have come up with so far:
to reproduce-adult
let chance-fawn random-float 1.001
let chance-to-reproduce .9
let chance-for-one .3
let chance-for-two .4
let chance-for-three .02
if any? femadults [
ask femadults [
if random-float 1.001 < chance-to-reproduce [
if chance-fawn < chance-for-three
[set births (births + 3)
let new-offspring nobody
hatch-infants 3
[set new-offspring self set color red - 1 set size 1]
set offspring new-offspring]
if chance-fawn > chance-for-two
[set births (births + 2)
let new-offspring nobody
hatch-infants 2
[set new-offspring self set color red - 1 set size 1]
set offspring new-offspring]
if (chance-fawn > .02) and (chance-fawn < chance-for-one)
[set births (births + 1)
let new-offspring nobody
hatch-infants 1
[set new-offspring self set color red - 1 set size 1]
set offspring new-offspring]
]]]
end
Basically, the chance of a doe getting pregnant is 90%. So I want that if the doe gets pregnant, she either has 1, 2 or 3 fawns. The chance of having 1 fawn is 28%. The chance of having 2 fawns is 60%. The chance of having 3 fawns is 2%. The problem with my current code is that if "chance-fawn" is between .3 and .4, it is not accounted for in the if statements, when it should be part of the 60% of having 2 fawns. Is there a better way to do this, using either if statements or something else? Thank you!
You can do what you want with ifelse, but you may want to have a look at the rnd extension, as it simplifies this type of thing. Specifically, the weighted-one-of-list command allows you to make roulette wheel selection where you assign different weights to different options. For an example, look at this setup:
extensions [ rnd ]
globals [ fawn-probs ]
to setup
ca
crt 10 [
setxy random-xcor random-ycor
]
set fawn-probs [ [ 1 0.31 ] [ 2 0.67 ] [ 3 0.02 ] ]
set offspring-list-norm []
set offspring-list-alt []
reset-ticks
end
You've got a list called fawn-probs that groups the different probabilities to different birth events. Note that I've made them sum to 1 by dividing each by 0.9; as p._phidot_ pointed out, your original probabilities did not. Now, you can use rnd:weighted-one-of-list to have your turtles randomly choose the number of fawns, weighted appropriately, from the fawn-probs list.
to reproduce
ask turtles [
; If a spawning event occurs
if random-float 1 < 0.9 [
; Select the number of fawns to spawn based on the weighted
; probability list 'fawn-probs'
let num-to-spawn first rnd:weighted-one-of-list fawn-probs [ p -> last p ]
hatch num-to-spawn [
rt random 360
fd 1
]
]
]
end
Alternatively, if you want to have the 10% chance of no-birth bundled into the same list, you could skip the if random-float ... chunk and just do:
to reproduce-alternative
set fawn-probs [ [ 0 0.1 ] [ 1 0.28 ] [ 2 0.6 ] [ 3 0.02 ] ]
ask turtles [
let num-to-spawn first rnd:weighted-one-of-list fawn-probs [ p -> last p ]
hatch num-to-spawn [
rt random 360
fd 1
]
]
end
ok.. here's my drawing (not to scale) :
+---------+---------+---------+---------+---------+--------->
0.0 0.02 0.1 0.3 0.4 0.5
a number line.. after the 1st if, you covered :
+=========+---------+---------+---------+---------+--------->
0.0 0.02 0.2 0.3 0.4 0.5
on the 2nd if :
+=========+---------+---------+---------+=========+=========>
0.0 0.02 0.2 0.3 0.4 0.5
the 3rd if:
+=========+=========+=========+---------+=========+=========>
0.0 0.02 0.2 0.3 0.4 0.5
So, it is logical that your conditions DOESN'T covers 0.3 to 0.4 range. (:
Also note that if your random number generated is EXACTLY 0.02 for example.. the 1st if and the 3rd if will miss it too. unless you use something like <= or >=.
Hope that explains.. (:

Netlogo: replacing matrix elements by conditioning on other rows/columns

I am working in Netlogo on a series of models making heavy use matrices. Briefly, the models include a number of state-variables for different breeds, where the state-variables are often stock-like items. As a simple example, consider the model:
extensions [ matrix ]
globals
[
]
turtles-own
[
n-items
stock-list
]
to setup
clear-all
reset-ticks
create-turtles 2
ask turtles
[
setxy random-xcor random-ycor
set n-items 10
let n-vars 3
set stock-list matrix:make-constant n-items n-vars [0] ; empty matrix
let stock-item n-values n-items [i -> i]
let stock-cost n-values n-items [ random-normal 10 2 ]
let stock-age n-values n-items [ random 50 ]
matrix:set-column stock-list 0 stock-item
matrix:set-column stock-list 1 stock-cost
matrix:set-column stock-list 2 stock-age
]
end
Here, each turtle's matrix stock-list is initialised as an empty matrix and then its columns filled in depending on the variables stock-item (id for stock), stock-cost and stock-age.
Imagine a go procedure that increments the stock age by one each time-step:
to go
ask turtles
[
let current-age matrix:get-column stock-list 1
let new-age map [x -> x + 1] current-age
matrix:set-column stock-list 2 new-age
]
tick
end
What I would like to do is an operation on stock-cost only if the age is greater than some value, e.g. 10
;; pseudo-code
for( i = 1 to I = number of items )
{
if ( stock-age[i] > 10 )
{
stock-cost[i] - 1
}
}
I know how to change the list of stock-cost conditional on its own values, using the map primitive, e.g.:
to decrease-stock-value
ask turtles
[
let current-cost matrix:get-column stock-list 1
set current-cost map [[?] -> ifelse-value (? > 10) [? - 1][?]] current-cost
matrix:set-column stock-list 1 current-cost
]
But my efforts to generalise this to using values in a different list to condition upon have failed.
Thanks for your help! Also, any insight onto whether this is a good approach to modelling state variables such as stocks would be useful.
I think I sorted it out using:
to decrease-stock-value
ask turtles
[
let current-cost matrix:get-column stock-list 1
let current-age matrix:get-column stock-list 2
let new-cost ( map [ [ a b ] ->
ifelse-value ( a > 10 ) [ b - 1 ] [ b ] ]
current-age current-cost
)
matrix:set-column stock-list 1 new-cost
]
end

How does one achieve a square spiral like the the Ulam spiral in Netlogo?

I spent the morning trying to find an easy function (x,y) -> n that would number the patches like this
I was not successful. Do any of Y'all have any experience or suggestions?
Here is my take on it:
patches-own [ n ]
to setup
clear-all
resize-world -4 4 -4 4 ; so it looks better, but use any size you like...
create-turtles 1 [
set heading 180
foreach n-values count patches [ ? + 1 ] [
set n ?
if [ n = 0 ] of patch-left-and-ahead 90 1 [ left 90 ]
fd 1
]
die
]
ask patches [ set plabel n ]
end
Funny you should ask I also spent the morning doing the same thing. There is a function that uses the floor function but I remembered that this is netlogo
so I made a turtle do it for me.
with this procedure
to spin
let k 1
set t t + 1
repeat 2
[
lt 90
repeat t [fd 1 ask patch-here [set n k set k k + 1]]
]
end
and this code in the start up.
crt 1 [
set heading 0
repeat 41 [spin]
die
]
and of course
patches-own [n]
to call them in n order use
foreach sort-on [n] patches ask ? [ "the stuff you want them to do" ]

scipy.minimize 'SLSQP' appears to return sub optimal weights values

Im trying to run a minimization function for an ensemble of logloss values, however when using the scipy.minimize function it appears to return a sub optimal value.
The data comes in a pandas table:
click, prob1, prob2, prob3
0, 0.0023, 0.0024, 0.012
1, 0.89, 0.672, 0.78
0, 0.43, 0.023, 0.032
from scipy.optimize import minimize
from math import log
import numpy as np
import pandas as pd
def logloss(p, y):
p = max(min(p, 1 - 10e-15), 10e-15)
return -log(p) if y == 1 else -log(1 - p)
def ensemble_weights(weights, probs, y_true):
loss = 0
final_pred = []
prob_length = len(probs)
for i in range(prob_length):
w_sum = 0
for index, weight in enumerate(weights):
w_sum += probs[i][index] * weight
final_pred.append(w_sum)
for index, pred in enumerate(final_pred):
loss += logloss(pred, y_true[index])
print loss / prob_length, 'weights :=', weights
return loss / prob_length
## w0 is the initial guess for the minimum of function 'fun'
## This initial guess is that all weights are equal
w0 = [1/probs.shape[1]] * probs.shape[1]
# ## This sets the bounds on the weights, between 0 and 1
bnds = [(0,1)] * probs.shape[1]
## This sets the constraints on the weights, they must sum to 1
## Or, in other words, 1 - sum(w) = 0
cons = ({'type':'eq','fun':lambda w: 1 - np.sum(w)})
weights = minimize(
ensemble_weights,
w0,
(probs,y_true),
method='SLSQP',
bounds=bnds,
constraints=cons
)
## As a sanity check, make sure the weights do in fact sum to 1
print("Weights sum to %0.4f:" % weights['fun'])
print weights['x']
To help debug i've used a print statement in the function for this which returns the following.
0.0101326509533 weights := [ 1. 0. 0.]
0.0101326509533 weights := [ 1. 0. 0.]
0.0101326509702 weights := [ 1.00000001 0. 0. ]
0.0101292476389 weights := [ 1.00000000e+00 1.49011612e-08 0.00000000e+00]
0.0101326509678 weights := [ 1.00000000e+00 0.00000000e+00 1.49011612e-08]
0.0102904525781 weights := [ -4.44628778e-10 1.00000000e+00 -4.38298620e-10]
0.00938612854966 weights := [ 5.00000345e-01 4.99999655e-01 -2.19149158e-10]
0.00961930211064 weights := [ 7.49998538e-01 2.50001462e-01 -1.09575296e-10]
0.00979499597866 weights := [ 8.74998145e-01 1.25001855e-01 -5.47881403e-11]
0.00990978430231 weights := [ 9.37498333e-01 6.25016666e-02 -2.73943942e-11]
0.00998305685424 weights := [ 9.68748679e-01 3.12513212e-02 -1.36974109e-11]
0.0100300175342 weights := [ 9.84374012e-01 1.56259881e-02 -6.84884901e-12]
0.0100605546439 weights := [ 9.92186781e-01 7.81321874e-03 -3.42452299e-12]
0.0100807513117 weights := [ 9.96093233e-01 3.90676721e-03 -1.71233067e-12]
0.0100942930446 weights := [ 9.98046503e-01 1.95349723e-03 -8.56215139e-13]
0.0101034594634 weights := [ 9.99023167e-01 9.76832595e-04 -4.28144378e-13]
0.0101034594634 weights := [ 9.99023167e-01 9.76832595e-04 -4.28144378e-13]
0.0101034594804 weights := [ 9.99023182e-01 9.76832595e-04 -4.28144378e-13]
0.0101034593149 weights := [ 9.99023167e-01 9.76847497e-04 -4.28144378e-13]
0.010103459478 weights := [ 9.99023167e-01 9.76832595e-04 1.49007330e-08]
Weights sum to 0.0101:
[ 9.99023167e-01 9.76832595e-04 -4.28144378e-13]
My expectation would be that the optimal weights returned should be:
0.00938612854966 weights := [ 5.00000345e-01 4.99999655e-01 -2.19149158e-10]
Can anyone see a glaring issue?
FYI -> This code is really a hack of the kaggle otto script
https://www.kaggle.com/hsperr/otto-group-product-classification-challenge/finding-ensamble-weights
Solved
options = {'ftol':1e-9}
as part of the minimize function

Understanding OpenGL Matrices

I'm starting to learn about 3D rendering and I've been making good progress. I've picked up a lot regarding matrices and the general operations that can be performed on them.
One thing I'm still not quite following is OpenGL's use of matrices. I see this (and things like it) quite a lot:
x y z n
-------
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
So my best understanding, is that it is a normalized (no magnitude) 4 dimensional, column-major matrix. Also that this matrix in particular is called the "identity matrix".
Some questions:
What is the "nth" dimension?
How and when are these applied?
My biggest confusion arises from how OpenGL makes use of this kind of data.
In most 3D graphics a point is represented by a 4-component vector (x, y, z, w), where w = 1. Usual operations applied on a point include translation, scaling, rotation, reflection, skewing and combination of these.
These transformations can be represented by a mathematical object called "matrix". A matrix applies on a vector like this:
[ a b c tx ] [ x ] [ a*x + b*y + c*z + tx*w ]
| d e f ty | | y | = | d*x + e*y + f*z + ty*w |
| g h i tz | | z | | g*x + h*y + i*z + tz*w |
[ p q r s ] [ w ] [ p*x + q*y + r*z + s*w ]
For example, scaling is represented as
[ 2 . . . ] [ x ] [ 2x ]
| . 2 . . | | y | = | 2y |
| . . 2 . | | z | | 2z |
[ . . . 1 ] [ 1 ] [ 1 ]
and translation as
[ 1 . . dx ] [ x ] [ x + dx ]
| . 1 . dy | | y | = | y + dy |
| . . 1 dz | | z | | z + dz |
[ . . . 1 ] [ 1 ] [ 1 ]
One of the reason for the 4th component is to make a translation representable by a matrix.
The advantage of using a matrix is that multiple transformations can be combined into one via matrix multiplication.
Now, if the purpose is simply to bring translation on the table, then I'd say (x, y, z, 1) instead of (x, y, z, w) and make the last row of the matrix always [0 0 0 1], as done usually for 2D graphics. In fact, the 4-component vector will be mapped back to the normal 3-vector vector via this formula:
[ x(3D) ] [ x / w ]
| y(3D) ] = | y / w |
[ z(3D) ] [ z / w ]
This is called homogeneous coordinates. Allowing this makes the perspective projection expressible with a matrix too, which can again combine with all other transformations.
For example, since objects farther away should be smaller on screen, we transform the 3D coordinates into 2D using formula
x(2D) = x(3D) / (10 * z(3D))
y(2D) = y(3D) / (10 * z(3D))
Now if we apply the projection matrix
[ 1 . . . ] [ x ] [ x ]
| . 1 . . | | y | = | y |
| . . 1 . | | z | | z |
[ . . 10 . ] [ 1 ] [ 10*z ]
then the real 3D coordinates would become
x(3D) := x/w = x/10z
y(3D) := y/w = y/10z
z(3D) := z/w = 0.1
so we just need to chop the z-coordinate out to project to 2D.
The short answer that might help you get started is that the 'nth' dimension, as you call it, does not represent any visualizable quantity. It is added as a practical tool to enable matrix multiplications that cause translation and perspective projection. An intuitive 3x3 matrix cannot do those things.
A 3d value representing a point in space always gets 1 appended as the fourth value to make this trick work. A 3d value representing a direction (i.e. a normal, if you are familiar with that term) gets 0 appended in the fourth spot.