0

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

Jonathan Dodds
  • 2,654
  • 1
  • 10
  • 14
Tsu'Na
  • 13
  • 4
  • 2
    `[] !== []` in Javascript unless they are the same instance, that's why "2 identical frames" is never executed even though many frames are probably identical – mousetail Jun 12 '23 at 14:19
  • In other words, you cannot compare arrays the way your code is written; they will never be equal. – Pointy Jun 12 '23 at 14:21
  • Like mentioned above arrays are object, 2 objects are never equal. Looking at your code though, a simple fix might be to `toString` the arrays and compare -> `if(Video[t].toString() === Video[t-1].toString())` – Keith Jun 12 '23 at 14:22
  • Ha yeah ! I just checked [this article](https://www.freecodecamp.org/news/how-to-compare-arrays-in-javascript/) right after I published the question. I applied the the method suggested and, it seems lines of my big matrix are the same. So I've to go back to the ` getVideo() ` function. thanks ! – Tsu'Na Jun 12 '23 at 14:25
  • Yes it does, basically what's in the article I linked. – Tsu'Na Jun 12 '23 at 15:07

0 Answers0