I'm trying to write a function that count the number of differences between consecutive lines of a matrix. (Final objective : writing a function that calc the motion rate between consecutive frames of a video.) For example:
1 2 3 4 5 6 7 8 9
_ _ _ _ _ _ _ _ _
1 | 1 5 3 4 5 6 7 8 9
2 | 1 2 8 4 5 6 7 8 9
3 | 1 2 3 4 5 6 1 8 9
4 | 1 2 3 4 5 6 7 8 9
5 | 1 2 3 4 5 6 7 8 9
6 | 1 2 3 4 5 6 7 8 9
7 | 1 2 3 4 5 6 7 8 9
8 | 1 2 3 4 5 6 7 8 9
9 | 1 2 3 4 5 6 7 8 9
The result should be 5, cause line 2 differs from line 1 on cols 2 and 3 9=(result += 2) then line 3 differs from line 2 on cols 3 and 7, then line 4 differs from line 3 on col 7.
Here is the function I wrote with an example matrix :
function calcMotion(Video){
var numFrames = Video.length;
var numPixels = Video[0].length;
var count = 0;
for (var t = 1; t < numFrames; t++) {
if(Video[t] === Video[t-1]){console.warn("2 identical frames")}
for(var i = 0; i < numPixels; i++){
if(Video[t][i] != Video[t-1][i]){
count++;
}
}
}
console.log("final : ", count);
}
var Video = [
[1, 5, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 8, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 1, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
];
calcMotion(Video);
Result in console :
final : 5
As expected ! So I guess the algorithm is good.
I tried with a matrix I'm working on, not an example :
var Video = getVideo();
console.log(Video); // [10 * 230400] matrix
calcMotion(Video);
result :
final : 0
I saw the video, frames are not identical. Moreover, the instruction if(Video[t] === Video[t-1]){console.warn("2 identical frames")}
is never executed, so it means frames (== matrix lines) actually are different. It should be at least on difference per line to have each frames different. So why is the result 0 ? I'm just increasing an int, so I don´t think it's about a 0 approximation...
I tried to count equals values, by modifying the function this way :
if(Video[t][i] == Video[t-1][i]){
decompte++;
}
and now the result is 230400*9 = 2073600 ! so the lines are the same ? but they're not the same ?
Is it possible that the error come from length of the lines ?
Thanks