converting a disparity map to a depth map using given calibration file - computer-vision

I have a pair of stereo images and I have computed the disparity image for them. Now I need to convert this disparity map to a depth map. I have found that
depth = (baseline * focal length) / disparity)
but I don't know how to find the values for baseline, focal length and disparity.
This is the calibration file given.
calib_time: 09-Jan-2012 13:57:47
corner_dist: 9.950000e-02
S_00: 1.392000e+03 5.120000e+02
K_00: 9.842439e+02 0.000000e+00 6.900000e+02 0.000000e+00 9.808141e+02 2.331966e+02 0.000000e+00 0.000000e+00 1.000000e+00
D_00: -3.728755e-01 2.037299e-01 2.219027e-03 1.383707e-03 -7.233722e-02
R_00: 1.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00
T_00: 2.573699e-16 -1.059758e-16 1.614870e-16
S_rect_00: 1.242000e+03 3.750000e+02
R_rect_00: 9.999239e-01 9.837760e-03 -7.445048e-03 -9.869795e-03 9.999421e-01 -4.278459e-03 7.402527e-03 4.351614e-03 9.999631e-01
P_rect_00: 7.215377e+02 0.000000e+00 6.095593e+02 0.000000e+00 0.000000e+00 7.215377e+02 1.728540e+02 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00
S_01: 1.392000e+03 5.120000e+02
K_01: 9.895267e+02 0.000000e+00 7.020000e+02 0.000000e+00 9.878386e+02 2.455590e+02 0.000000e+00 0.000000e+00 1.000000e+00
D_01: -3.644661e-01 1.790019e-01 1.148107e-03 -6.298563e-04 -5.314062e-02
R_01: 9.993513e-01 1.860866e-02 -3.083487e-02 -1.887662e-02 9.997863e-01 -8.421873e-03 3.067156e-02 8.998467e-03 9.994890e-01
T_01: -5.370000e-01 4.822061e-03 -1.252488e-02
S_rect_01: 1.242000e+03 3.750000e+02
R_rect_01: 9.996878e-01 -8.976826e-03 2.331651e-02 8.876121e-03 9.999508e-01 4.418952e-03 -2.335503e-02 -4.210612e-03 9.997184e-01
P_rect_01: 7.215377e+02 0.000000e+00 6.095593e+02 -3.875744e+02 0.000000e+00 7.215377e+02 1.728540e+02 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00 0.000000e+00
S_02: 1.392000e+03 5.120000e+02
K_02: 9.597910e+02 0.000000e+00 6.960217e+02 0.000000e+00 9.569251e+02 2.241806e+02 0.000000e+00 0.000000e+00 1.000000e+00
D_02: -3.691481e-01 1.968681e-01 1.353473e-03 5.677587e-04 -6.770705e-02
R_02: 9.999758e-01 -5.267463e-03 -4.552439e-03 5.251945e-03 9.999804e-01 -3.413835e-03 4.570332e-03 3.389843e-03 9.999838e-01
T_02: 5.956621e-02 2.900141e-04 2.577209e-03
S_rect_02: 1.242000e+03 3.750000e+02
R_rect_02: 9.998817e-01 1.511453e-02 -2.841595e-03 -1.511724e-02 9.998853e-01 -9.338510e-04 2.827154e-03 9.766976e-04 9.999955e-01
P_rect_02: 7.215377e+02 0.000000e+00 6.095593e+02 4.485728e+01 0.000000e+00 7.215377e+02 1.728540e+02 2.163791e-01 0.000000e+00 0.000000e+00 1.000000e+00 2.745884e-03
S_03: 1.392000e+03 5.120000e+02
K_03: 9.037596e+02 0.000000e+00 6.957519e+02 0.000000e+00 9.019653e+02 2.242509e+02 0.000000e+00 0.000000e+00 1.000000e+00
D_03: -3.639558e-01 1.788651e-01 6.029694e-04 -3.922424e-04 -5.382460e-02
R_03: 9.995599e-01 1.699522e-02 -2.431313e-02 -1.704422e-02 9.998531e-01 -1.809756e-03 2.427880e-02 2.223358e-03 9.997028e-01
T_03: -4.731050e-01 5.551470e-03 -5.250882e-03
S_rect_03: 1.242000e+03 3.750000e+02
R_rect_03: 9.998321e-01 -7.193136e-03 1.685599e-02 7.232804e-03 9.999712e-01 -2.293585e-03 -1.683901e-02 2.415116e-03 9.998553e-01
P_rect_03: 7.215377e+02 0.000000e+00 6.095593e+02 -3.395242e+02 0.000000e+00 7.215377e+02 1.728540e+02 2.199936e+00 0.000000e+00 0.000000e+00 1.000000e+00 2.729905e-03

I'll give you two answers. The first one is the academic one.
In your data (not sure where it comes from), I distinguish each line from the first letter:
S = Image size
K = Intrinsic camera matrix (3x3)
D = Distortion coefficients
R = Rotation matrix (3x4)
T = Translation vector (3x1)
It seems that we have 4 cameras here, so not sure. That's up to you to understand. Anyway, given the data above you may calculate camera center in the space. For the first camera:
C_00 = -R_00 . T_00
where . stands for the dot product. Do the same for the other camera, then the baseline is simply the distance between the two camera centers.
Focal length is already there: element (0,0) of intrinsic matrix is fx focal length over the x axis. Element (1,1) is fy focal length over y axis.
Why two focal lengths??? You will see that they are very similar indeed, but the difference reflects the imperfection of the camera lenses.
So, how do you get the depth??? Here I go with the less academic answer.
In practice you have to follow this process:
Calibrate the two cameras (using metric units)
Take pictures
Undistort images (correct lens distortion using your D_00 coefficients)
Rectify images (what's rectification? You can check here and here). After this step the left and right images look like they were taken by the same camera only shifted along x.
Apply the formula to calculate depth. However, since you have fx and fy, but also the shear factor (element (0,1) of the camera matrix) and the principal point shift (elements (0,2) and (1,2) of the camera matrix), the formula is slightly complex that the one you suggested.
So I suggest to use OpenCV and use the function cv2.reprojectImageTo3D, calculating the Q matrix as done here.
You can find a working example to go from images to depth map and finally to depth (point cloud), here.
I hope this gives you a starting point.
Cheers.

Related

Calculating cumulative multivariate normal distribution

I have 1000 observations and 3 variables in Stata that are associated with 1000 people. Let's say the data looks something like this (I just make up the numbers)
Observation
B1
B2
B3
1
-3
5
3
2
2
-3
2
3
6
-2
5
4
5
3
3
...
..
...
...
1000
..
..
...
Which has a correlation matrix (again made up numbers)
R = (1, 0.5, 0.5
0.5, 1, 0.5
0.5, 0.5, 1
0.5, 0.5, 0.5)
I want to calculate the CDF of the multivariate normal distribution of variables B1, B2 and B3 for each of the 1000 persons, using the same correlation matrix. Basically, it is similar to Example 3 in this document: https://www.stata.com/manuals/m-5mvnormal.pdf, but with 3 variables, and rather than multiple limits, multiple correlation matrix, I will do multiple limits and single correlation matrix. So basically, I will have 1000 CDF values for 1000 people. I have tried mvnormal(U,R). Specifically, I wrote:
mkmat B1 B2 B3, matrix(U)
matrix define R = (1, 0.5, 0.5 \
0.5, 1, 0.5 \
0.5, 0.5, 1 \
0.5, 0.5, 0.5)
gen CDF = mvnormal(U,R)
But this doesn't work. Apparently this function is not on Stata anymore. I believe Stata has binormal for calculating the CDF of bivariate normal. But is it able to do the CDF of more than 2 variables?

How to pause Execution in SAS for milliseconds

How to pause an execution for 5 milliseconds in SAS?
Can I use "CALL SLEEP (0.005)"
I have checked the below link but its confusing
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/n12ppys43orawkn1q0oxep4cmdk6.htm
The below shall stop the execution for 5 milliseconds in SAS.
data _null_;
call sleep(5);
run;
You can use the optional unit argument to specify the unit of time in seconds, which is applied to n. Default is .001 (milliseconds). You can change it to seconds if you like
data _null_;
call sleep(0.005,1);
run;
The above is equivalent to the former.
Sometimes it is better to try it out than to google:
data try_to_get_some_sleep;
format n unit z5.3 evening morning expect measure time18.9 diff percent7.2;
do unit = 5, 1, .5, .1, .01, .001;
do n = .1, .9, 1.1, 1.9, 2 ;
expect = n * unit;
evening = time();
call sleep (n, unit);
morning = time();
measure = morning - evening;
diff = (measure - expect) / expect;
output;
end;
end;
run;
results in
n unit evening morning expect measure diff
0.100 5.000 10:45:05.983414888 10:45:06.483422041 0:00:00.500000000 0:00:00.500007153 0.00%
0.900 5.000 10:45:06.483437061 10:45:10.984707117 0:00:04.500000000 0:00:04.501270056 0.03%
1.100 5.000 10:45:10.984720945 10:45:16.485454082 0:00:05.500000000 0:00:05.500733137 0.01%
1.900 5.000 10:45:16.485466003 10:45:25.984838009 0:00:09.500000000 0:00:09.499372005 (0.01%)
2.000 5.000 10:45:25.984853983 10:45:35.988686085 0:00:10.000000000 0:00:10.003832102 0.04%
0.100 1.000 10:45:35.988715887 10:45:36.088612080 0:00:00.100000000 0:00:00.099896193 (0.10%)
0.900 1.000 10:45:36.088624954 10:45:36.988639116 0:00:00.900000000 0:00:00.900014162 0.00%
1.100 1.000 10:45:36.988765001 10:45:38.089132071 0:00:01.100000000 0:00:01.100367069 0.03%
1.900 1.000 10:45:38.089145899 10:45:39.989645004 0:00:01.900000000 0:00:01.900499105 0.03%
2.000 1.000 10:45:39.989659071 10:45:41.989659071 0:00:02.000000000 0:00:02.000000000 0.00%
0.100 0.500 10:45:41.989671946 10:45:42.038803101 0:00:00.050000000 0:00:00.049131155 (1.74%)
0.900 0.500 10:45:42.038815975 10:45:42.488348961 0:00:00.450000000 0:00:00.449532986 (0.10%)
1.100 0.500 10:45:42.488362074 10:45:43.038013935 0:00:00.550000000 0:00:00.549651861 (0.06%)
1.900 0.500 10:45:43.038027048 10:45:43.987673044 0:00:00.950000000 0:00:00.949645996 (0.04%)
2.000 0.500 10:45:43.987685919 10:45:44.987751007 0:00:01.000000000 0:00:01.000065088 0.01%
0.100 0.100 10:45:44.987765074 10:45:44.996871948 0:00:00.010000000 0:00:00.009106874 (8.93%)
0.900 0.100 10:45:44.996876955 10:45:45.085994005 0:00:00.090000000 0:00:00.089117050 (0.98%)
1.100 0.100 10:45:45.086005926 10:45:45.195319891 0:00:00.110000000 0:00:00.109313965 (0.62%)
1.900 0.100 10:45:45.195332050 10:45:45.384675980 0:00:00.190000000 0:00:00.189343929 (0.35%)
2.000 0.100 10:45:45.384690046 10:45:45.585688114 0:00:00.200000000 0:00:00.200998068 0.50%
0.100 0.010 10:45:45.585701942 10:45:45.585707903 0:00:00.001000000 0:00:00.000005960 (99.4%)
0.900 0.010 10:45:45.585709095 10:45:45.595653057 0:00:00.009000000 0:00:00.009943962 10.5%
1.100 0.010 10:45:45.595659971 10:45:45.607652903 0:00:00.011000000 0:00:00.011992931 9.03%
1.900 0.010 10:45:45.607661009 10:45:45.626678944 0:00:00.019000000 0:00:00.019017935 0.09%
2.000 0.010 10:45:45.626689911 10:45:45.646678925 0:00:00.020000000 0:00:00.019989014 (0.05%)
0.100 0.001 10:45:45.646688938 10:45:45.646688938 0:00:00.000100000 0:00:00.000000000 ( 100%)
0.900 0.001 10:45:45.646689892 10:45:45.646689892 0:00:00.000900000 0:00:00.000000000 ( 100%)
1.100 0.001 10:45:45.646691084 10:45:45.647506952 0:00:00.001100000 0:00:00.000815868 (25.8%)
1.900 0.001 10:45:45.647507906 10:45:45.647620916 0:00:00.001900000 0:00:00.000113010 (94.1%)
2.000 0.001 10:45:45.647623062 10:45:45.650509119 0:00:00.002000000 0:00:00.002886057 44.3%
You can directly suspend a SAS session between steps using %SYSCALL SLEEP(... or %SYSFUNC(SLEEP(... NOTE: When using %SYSCALL you need to pass macro variables as the arguments, not numeric literal text.
Example:
%let duration = 5;
%let unit = 0.001;
data one; set sashelp.class; run;
%syscall sleep(duration,unit);
data two; set sashelp.cars; run;
or
data one; set sashelp.class; run;
%let rc = %sysfunc ( sleep ( 5, 0.001 ));
data two; set sashelp.cars; run;

Stata: Combining multiple estpost results

I am trying to combine the cumpct results I get from estpost tabulate with the summary statistics that I obtain from estpost tabstat in one esttab output. However, I get a blank cumpct column by using the code below. I believe that the problem stems from the way I store the cumpct matrix but I, unfortunately, couldn't find a solution.
clear
input float A wage
1 100
3 450
2 180
2 190
1 70
4 880
3 65
5 40
1 144
4 28
5 110
end
* tabulation
estpost tabulate A
matrix cumpct=e(cumpct)
* Summary Stats
estpost tabstat wage, ///
statistics(mean sd p25 p50 p75) ///
columns(statistics) by(A)
* Esttab
esttab ., replace ///
cells("cumpct mean sd p25 p50 p75")
Result I get is the following:
------------------------------------------------------------------------------------------
(1)
cumpct mean sd p25 p50 p75
------------------------------------------------------------------------------------------
1 104.6667 37.22007 70 100 144
2 185 7.071068 180 185 190
3 257.5 272.2361 65 257.5 450
4 454 602.455 28 454 880
5 75 49.49747 40 75 110
Total 205.1818 252.3192 65 110 190
------------------------------------------------------------------------------------------
N 11
------------------------------------------------------------------------------------------
.
In your example after running:
estpost tabstat wage, ///
statistics(mean sd p25 p50 p75) ///
columns(statistics) by(A)
What is stored in e() is:
. ereturn list
scalars:
e(N) = 11
macros:
e(cmd) : "estpost"
e(subcmd) : "tabstat"
e(stats) : "mean sd p25 p50 p75"
e(vars) : "wage"
e(byvar) : "A"
matrices:
e(mean) : 1 x 6
e(sd) : 1 x 6
e(p25) : 1 x 6
e(p50) : 1 x 6
e(p75) : 1 x 6
So when you run
esttab ., replace ///
cells("cumpct mean sd p25 p50 p75")
cumpct is not found and hence will be missing.
It is possible to manually add the cumulative matrix to e() with a small helper program.
clear
input float A wage
1 100
3 450
2 180
2 190
1 70
4 880
3 65
5 40
1 144
4 28
5 110
end
// Helper program
cap program drop add_e
program add_e, eclass
args name matrix
ereturn matrix `name' = `matrix'
end
* tabulation
estpost tabulate A
matrix cumpct=e(cumpct)
* Summary Stats
estpost tabstat wage, ///
statistics(mean sd p25 p50 p75) ///
columns(statistics) by(A)
add_e "cumpct" cumpct
* Esttab
esttab ., replace ///
cells("cumpct mean sd p25 p50 p75")
Result:
------------------------------------------------------------------------------------------
(1)
cumpct mean sd p25 p50 p75
------------------------------------------------------------------------------------------
1 27.27273 104.6667 37.22007 70 100 144
2 45.45455 185 7.071068 180 185 190
3 63.63636 257.5 272.2361 65 257.5 450
4 81.81818 454 602.455 28 454 880
5 100 75 49.49747 40 75 110
Total 205.1818 252.3192 65 110 190
------------------------------------------------------------------------------------------
N 11
------------------------------------------------------------------------------------------

OpenGL : non rotated 4x4 matrix

Let's say that I have a 4x4 matrix like this:
a11 a12 a13 tx
a21 a22 a23 ty
a31 a32 a33 tz
a41 a42 a43 w
Is it true to say that the non rotated matrix is :
1 0 0 tx
0 1 0 ty
0 0 1 tz
0 0 0 w
?
In general, the statement is wrong. By removing the left 3 columns, also other effects than just the rotation are removed. For example, scaling factors as well as skewing is also stored in this region. Also, by maintaining the w value, the result could contain some other things than just translation.
If you assume that the original matrix was only composed by rotations and translations, then the assumption is true. Although in this case, removing only the upper 3x3 matrix would yield the same result since the last row will be [0, 0, 0, 1] anyway.

Differences in calculation for Naive net in WEKA

I tried out Naive net in weka 3.8 using weather dataset
Weka results
OUTLOOK
Sunny Yes = 0.238
No = 0.538
Overcast Yes = 0.429
No = 0.077
Rainy Yes = 0.333
No = 0.385
TEMPERATURE
Hot Yes = 0.238
No = 0.385
mild Yes = 0.429
No = 0.385
Cool Yes = 0.333
No = 0.231
But when I workedout it manually.My results are
OUTLOOK
Sunny Yes (2/9)= 0.22
No (3/5)= 0.60
Overcast Yes (4/9)= 0.44
No (0/5)= 0
Rainy Yes (3/9)= 0.333
No (2/5)= 0.40
TEMPERATURE
Hot Yes (2/9)= 0.222
No (2/5)= 0.40
mild Yes (4/9)= 0.444
No (2/5)= 0.40
Cool Yes (3/9)= 0.333
No (1/5)= 0.20
Why is this difference?
Whether my calculation went wrong somewhere?