title: C++ Normalized Cross Correlation between two large arrays
body: <p>I am attempting to find how similar an array (Second) is to my base array (First) using NCC*. Both of these arrays are greyscale images of size 36*49 (1764). For simplicity and the purpose of this question I am just treating them as arrays of numbers rather than images.  </p>
  
  <p>From my own understanding of NCC, the closer the approximate means are the closer the two arrays are(?). The mean of my base array is 187.691 and the mean of my second array is 187.529. Therefore I would expect an NCC value close to 1, Implying the images are very similar. However, the output value is -0.23428 which means they are not similar at all. Now, is my code wrong? Or do I not understand how NCC actually works?</p>
  
  <p>*<a href="https://i.stack.imgur.com/k3vui.png" rel="nofollow noreferrer">NCC formula for those unfamiliar</a>   </p>
  
  <p>I understand that there is a stupid amount of numbers in the arrays, sorry if it makes it difficult to read. I just want to make sure others could copy it 100% exactly how I have done it.</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;math.h&gt;
  #include &lt;cmath&gt;
  
  using namespace std;
  
  int main() {
  
  int First[1764] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 52, 111, 219, 218, 218, 232, 169, 101, 251, 251, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 57, 92, 151, 166, 169, 189, 148, 100, 181, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 59, 90, 94, 69, 54, 65, 50, 48, 83,
                     220, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     112, 83, 61, 65, 108, 83, 56, 48, 40, 43, 45, 28, 48, 207, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 128, 137, 112, 69, 37, 85, 120, 91, 41, 39, 56, 40, 33, 63, 29, 80, 224, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 65, 76, 114, 110, 81, 112,
                     83, 24, 24, 40, 32, 34, 31, 24, 21, 144, 243, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 81, 135, 159, 132, 105, 86, 43, 66, 94, 58, 56, 98, 116, 62, 81, 185, 253, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 75, 107, 153, 116, 97, 57, 29, 145,
                     189, 107, 108, 104, 128, 128, 166, 166, 238, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 87, 77, 95, 112, 98, 24, 38, 133, 161, 129, 151, 117, 56, 160, 192, 144, 234, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 78, 83, 76, 65, 58, 109, 189,
                     186, 123, 85, 137, 137, 151, 226, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 66, 37, 93, 151, 103, 127, 151, 148, 130, 140, 144, 115, 166, 243, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 97, 71, 107, 176, 160, 151, 160, 146, 150,
                     156, 143, 111, 151, 249, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 128, 115, 104, 198, 187, 148, 127, 134, 108, 125, 240, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 166, 136, 92, 153, 203, 153, 127, 153, 123,
                     129, 242, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 247, 229, 151, 106, 204, 196, 152, 165, 134, 110, 230, 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 250, 251, 194, 74, 174, 215, 156, 154, 132, 53, 173, 247,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 113, 157, 229,
                     214, 163, 113, 149, 176, 205, 174, 125, 122, 90, 227, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 101, 121, 132, 105, 81, 135, 146, 110, 78, 78, 114, 137, 89, 201, 252, 251, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 198, 147, 117, 169, 217, 170, 113, 81, 96, 90, 56, 45, 85, 134,
                     121, 55, 166, 247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 183, 106, 106, 182,
                     209, 211, 213, 190, 139, 131, 132, 161, 209, 221, 179, 119, 131, 232, 254, 249, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 152, 105, 103, 113, 105, 129, 165, 186, 231, 237, 238, 237, 241, 238, 196, 141, 91, 199, 249, 250,
                     254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 107, 133, 134, 116, 110, 104, 92, 131, 168, 179,
                     191, 189, 178, 159, 123, 87, 66, 163, 241, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     119, 224, 232, 186, 146, 154, 136, 114, 107, 103, 105, 114, 109, 106, 110, 99, 77, 148, 236, 251, 252, 255, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 68, 89, 185, 223, 208, 149, 188, 223, 194, 171, 158, 134, 123, 123, 151, 180, 174,
                     104, 151, 238, 252, 249, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 86, 98, 117, 142, 103, 68, 134, 195,
                     179, 196, 206, 210, 211, 230, 236, 238, 220, 146, 129, 210, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     255, 128, 123, 120, 119, 99, 72, 57, 78, 111, 111, 119, 137, 148, 158, 192, 208, 208, 177, 121, 83, 161, 243, 251, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 192, 237, 224, 163, 89, 84, 98, 102, 102, 96, 101, 109, 112, 111, 122, 121,
                     122, 99, 99, 83, 125, 222, 248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 213, 237, 236, 157, 105, 112, 149,
                     206, 197, 152, 117, 107, 101, 100, 103, 92, 101, 92, 97, 146, 137, 212, 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
                     75, 84, 123, 138, 127, 127, 159, 141, 147, 241, 248, 229, 214, 190, 166, 156, 166, 170, 182, 163, 116, 179, 139, 211, 251, 255, 255,
                     255, 255, 255, 255, 255, 255, 255, 255, 255, 63, 80, 89, 102, 67, 98, 138, 117, 116, 201, 221, 239, 238, 241, 242, 231, 232, 237, 239,
                     210, 112, 122, 86, 176, 244, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 122, 154, 133, 101, 128, 142, 119, 88, 82, 125,
                     123, 143, 179, 207, 236, 238, 229, 220, 214, 177, 83, 82, 83, 141, 225, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 238,
                     203, 127, 85, 159, 237, 219, 143, 70, 87, 101, 99, 110, 122, 146, 150, 139, 125, 131, 108, 78, 125, 165, 149, 212, 255, 255, 255, 255,
                     255, 255, 255, 255, 255, 124, 169, 192, 134, 86, 91, 183, 251, 244, 165, 115, 149, 135, 95, 100, 82, 90, 80, 88, 89, 85, 92, 64, 186,
                     238, 206, 173, 247, 255, 255, 255, 255, 255, 255, 255, 255, 98, 101, 100, 75, 81, 139, 210, 248, 226, 164, 182, 228, 207, 197, 166,
                     151, 143, 134, 148, 166, 171, 131, 71, 152, 219, 184, 126, 175, 255, 255, 255, 255, 255, 255, 255, 255, 96, 85, 107, 91, 126, 220,
                     247, 249, 227, 150, 157, 221, 224, 237, 240, 239, 228, 220, 224, 239, 241, 176, 65, 82, 134, 132, 82, 84, 105, 255, 255, 255, 255,
                     255, 255, 255, 124, 156, 209, 143, 154, 247, 254, 248, 228, 132, 92, 143, 172, 176, 208, 226, 240, 241, 239, 239, 232, 165, 80, 51,
                     54, 88, 124, 105, 94, 112, 255, 255, 255, 255, 255, 255, 135, 207, 234, 143, 142, 242, 250, 253, 208, 109, 85, 86, 95, 102, 120, 141,
                     179, 186, 181, 164, 144, 104, 108, 106, 54, 79, 210, 222, 150, 111, 122, 255, 255, 255, 255, 255, 96, 160, 157, 102, 119, 230, 247,
                     253, 180, 103, 169, 151, 106, 113, 92, 82, 96, 93, 91, 89, 94, 84, 118, 198, 139, 98, 203, 243, 179, 104, 95, 111, 255, 255, 255,
                     255, 53, 84, 98, 95, 106, 200, 253, 252, 182, 111, 215, 233, 198, 175, 155, 132, 119, 117, 124, 144, 173, 142, 107, 239, 231, 149,
                     121, 154, 136, 94, 70, 109, 121, 255, 255, 255, 51, 75, 141, 146, 125, 191, 251, 244, 222, 100, 120, 204, 245, 237, 226, 217, 207,
                     211, 210, 215, 223, 147, 82, 222, 254, 205, 75, 69, 100, 104, 111, 127, 213, 255, 255, 255, 42, 133, 223, 238, 127, 154, 246, 249,
                     201, 103, 44, 80, 140, 151, 163, 210, 228, 228, 208, 151, 118, 68, 51, 181, 244, 249, 193, 76, 68, 166, 222, 197, 163, 198, 255,
                     255, 255, 161, 235, 207, 86, 72, 214, 214, 115, 55, 79, 79, 62, 52, 65, 65, 84, 86, 61, 47, 56, 86, 39, 129, 230, 254, 243, 168,
                     103, 182, 245, 204, 96, 119, 202, 237, 255, 121, 122, 100, 89, 96, 141, 162, 83, 63, 107, 89, 67, 70, 73, 81, 117, 103, 69, 102,
                     89, 115, 57, 128, 234, 253, 253, 242, 166, 167, 203, 150, 80, 90, 124, 175, 255, 74, 83, 139, 175, 187, 119, 113, 102, 255, 255,
                     255, 255, 75, 70, 147, 211, 177, 97, 124, 103, 134, 63, 164, 239, 250, 248, 254, 237, 142, 53, 98, 176, 197, 133, 94, 255, 149,
                     152, 202, 204, 207, 176, 90, 255, 255, 255, 255, 255, 255, 92, 127, 139, 152, 104, 108, 88, 126, 94, 218, 254, 254, 253, 246,
                     213, 95, 35, 126, 179, 133, 117, 119, 255, 205, 155, 175, 209, 206, 176, 88, 255, 255, 255, 255, 255, 255, 111, 94, 56, 110,
                     110, 100, 69, 65, 133, 241, 252, 244, 251, 188, 99, 42, 71, 138, 125, 27, 46, 123, 255, 229, 166, 143, 153, 131, 123, 130, 255,
                     255, 255, 255, 255, 255, 68, 95, 87, 116, 93, 97, 105, 79, 140, 244, 253, 245, 246, 160, 74, 68, 78, 136, 125, 55, 50, 97, 248,
                     252, 192, 92, 23, 46, 162, 221, 255, 255, 255, 255, 255, 255, 130, 143, 119, 108, 79, 115, 141, 80, 111, 237, 251, 243, 221, 131,
                     86, 80, 79, 148, 116, 83, 97, 93, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 189, 244, 219, 167, 92,
                     112, 136, 54, 113, 229, 252, 240, 220, 137, 81, 122, 161, 105, 49, 90, 116, 88};
  
  int Second[1764] = {196,189,187,184,178,180,187,190,201,138,120,228,251,251,254,253,253,253,252,252,252,250,248,217,120,220,216,97,107,123,124,130,120,116,
                      97,111,179,180,181,170,187,187,172,185,195,153,152,225,251,252,254,254,253,254,253,253,252,250,247,189,99,138,149,148,106,123,121,129,129,
                      122,138,113,183,176,182,185,185,183,184,192,191,143,153,228,253,254,254,254,254,253,253,254,255,246,239,153,85,111,119,180,103,131,127,128,
                      139,113,179,154,180,180,181,186,176,174,189,153,180,139,160,234,253,253,253,253,253,253,252,253,252,251,213,112,102,168,135,203,102,124,135,
                      130,137,92,194,215,182,192,181,181,177,196,178,100,152,111,189,249,253,253,253,253,253,253,252,253,247,250,171,109,136,209,146,218,132,101,
                      132,134,122,95,206,247,175,134,110,141,185,188,127,103,154,96,215,253,252,252,252,252,252,252,252,251,250,189,149,177,160,194,168,176,188,96,
                      123,132,106,147,233,244,154,74,37,74,146,119,82,148,165,140,236,248,252,252,252,252,252,252,252,250,232,146,186,242,171,163,209,125,190,109,119,
                      124,109,212,239,195,172,123,78,76,65,74,122,197,127,164,238,254,252,252,252,251,252,252,251,249,184,171,243,250,178,144,234,121,138,117,117,120,
                      125,241,221,127,177,190,135,139,164,175,195,187,102,201,253,252,252,250,253,253,247,254,250,203,153,219,241,251,221,125,189,159,82,94,132,109,171,
                      226,108,140,180,184,120,126,202,186,190,174,139,222,254,252,254,251,252,243,251,249,200,153,179,242,251,240,176,93,142,160,97,102,116,126,134,143,
                      96,127,188,183,115,136,185,183,198,141,150,223,254,254,254,246,248,251,249,220,178,182,251,248,243,158,117,147,120,107,131,109,114,123,104,130,138,
                      168,189,178,120,164,193,183,200,124,177,238,254,249,236,207,217,235,194,146,181,247,252,252,179,117,178,205,99,144,200,113,128,114,119,195,167,221,
                      182,167,118,180,194,176,189,123,199,244,253,251,205,128,108,110,138,194,228,252,243,204,118,188,251,200,129,238,242,112,126,112,157,236,153,232,184,
                      166,117,177,186,193,193,130,213,253,248,241,161,150,191,139,159,248,244,246,233,147,172,252,250,207,147,244,244,133,116,100,166,243,156,217,189,175,
                      118,160,189,200,163,130,226,244,177,161,146,131,173,146,156,236,239,228,153,168,246,252,253,237,137,244,244,156,116,97,162,247,175,189,186,179,116,
                      144,189,192,149,167,248,203,102,133,160,90,100,152,141,153,183,144,184,236,248,251,254,210,147,227,246,142,108,113,176,252,184,182,190,176,145,121,
                      185,198,142,190,242,237,158,108,108,139,156,173,134,81,139,230,243,254,254,252,252,167,123,191,243,162,113,106,195,248,220,149,183,181,161,119,176,
                      184,130,204,249,248,230,114,124,173,163,139,112,144,235,248,255,251,245,254,224,153,161,165,207,204,88,103,237,250,246,175,177,188,183,124,140,176,
                      121,187,250,221,186,143,120,141,125,140,139,162,245,254,249,254,252,253,229,150,176,224,143,168,97,80,206,248,250,244,178,187,187,124,125,176,130,
                      125,236,220,149,173,124,152,116,91,115,178,249,254,251,254,254,247,247,182,134,169,193,140,111,75,169,247,234,223,185,180,175,120,141,182,177,117,
                      170,218,155,106,112,138,121,74,113,215,252,246,255,250,253,248,253,247,199,143,125,116,105,82,109,130,161,160,193,181,173,125,152,179,190,149,115,
                      154,197,199,146,110,143,177,207,249,253,254,252,253,250,251,250,247,249,182,91,117,102,100,171,186,203,209,194,184,178,127,146,182,183,186,132,100,
                      133,200,219,188,224,251,253,254,250,252,254,254,243,253,254,248,248,208,110,124,132,111,199,247,243,248,190,182,177,117,151,188,168,180,181,161,98,
                      103,139,179,231,252,252,250,250,252,253,244,253,249,248,254,253,226,125,126,138,102,148,214,224,195,193,175,181,101,113,191,173,177,192,198,157,57,
                      70,106,162,242,254,252,254,245,252,253,253,251,254,255,250,240,131,120,141,128,104,126,129,116,159,195,183,95,49,130,183,174,185,194,104,45,116,123,
                      140,217,254,252,253,255,251,253,253,252,254,254,251,243,145,126,136,118,99,117,119,115,133,193,181,134,57,67,145,195,191,136,48,64,136,134,126,201,
                      253,254,249,255,254,254,253,252,254,254,251,245,147,120,130,119,109,124,133,136,102,174,176,168,106,75,91,159,145,73,55,69,121,131,127,202,253,255,
                      251,251,254,254,253,253,254,254,251,246,152,108,127,126,107,112,127,129,70,147,176,176,128,156,103,66,71,99,128,73,120,126,125,206,251,251,253,253,
                      254,253,252,252,252,252,251,248,176,107,130,135,105,100,122,122,65,128,185,178,132,202,188,104,135,202,172,99,133,127,116,203,252,249,253,254,254,
                      254,251,252,253,251,252,250,205,113,126,135,111,101,128,132,72,126,191,177,142,170,252,228,251,250,151,105,136,139,107,176,245,253,253,251,255,253,
                      252,253,255,252,252,253,231,124,116,124,118,100,115,124,73,119,187,186,169,134,248,254,253,244,130,96,131,153,100,140,223,254,254,247,255,253,253,
                      254,254,251,252,254,247,140,117,124,137,111,111,123,106,77,162,189,185,131,234,249,254,228,125,112,137,150,125,107,198,254,248,253,254,254,255,254,
                      255,254,254,254,247,201,116,119,144,111,111,124,123,69,122,181,159,114,229,246,251,237,119,107,141,145,125,118,197,252,254,254,254,254,254,254,255,
                      254,255,255,253,244,178,108,121,126,104,128,133,97,58,92,85,64,196,251,246,232,121,125,140,154,123,116,199,250,255,250,254,254,255,255,254,253,253,
                      254,249,252,228,152,109,117,110,117,115,127,73,57,78,63,141,240,252,245,147,100,127,159,115,110,213,253,252,252,254,255,255,254,253,253,253,254,249,
                      255,247,233,172,117,108,114,98,110,132,124,130,116,111,217,248,252,205,111,109,127,101,127,229,252,250,254,254,255,255,254,253,253,253,254,249,253,
                      248,253,241,157,85,108,143,80,105,155,160,124,93,211,246,247,252,198,121,104,116,173,240,248,253,254,254,254,254,254,254,254,254,254,254,248,250,251,
                      251,221,108,98,223,128,79,122,146,95,107,223,254,251,254,246,234,204,223,248,253,254,255,251,255,255,255,255,255,255,255,255,254,244,252,252,248,249,
                      160,101,249,232,140,86,88,86,176,249,253,254,253,254,253,246,253,249,252,254,253,252,255,255,255,255,254,254,254,255,246,254,251,251,254,250,164,101,
                      247,253,244,195,165,215,249,254,250,254,254,247,252,254,252,254,255,252,252,254,255,253,254,254,253,249,251,254,250,251,253,251,251,249,185,103,252,
                      253,253,249,250,253,250,255,255,245,247,254,251,249,254,248,246,255,255,251,248,252,253,251,230,236,254,250,248,255,247,253,254,247,198,106,254,251,
                      255,253,254,255,249,253,255,254,251,255,254,247,255,252,254,254,242,254,252,247,245,184,88,133,248,254,251,254,249,246,253,249,200,98,250,249,255,251,
                      250,255,254,252,250,252,254,253,254,251,250,254,250,193,161,198,215,119,85,82,47,69,156,215,252,245,254,253,254,252,182,84,254,254,246,254,252,247,254,
                      254,250,247,251,254,253,251,251,250,183,83,63,91,88,57,54,63,71,49,55,144,251,251,255,254,254,243,193,89,254,254,253,253,254,250,254,254,254,254,254,253,
                      254,251,253,211,94,32,53,58,46,64,87,73,83,100,75,72,178,242,251,245,252,251,242,103,244,252,254,249,254,255,253,248,253,251,243,254,254,252,249,155,51,
                      58,85,80,75,66,77,84,73,82,86,58,66,170,249,252,253,252,234,99,254,254,244,254,250,252,253,253,246,182,149,203,250,250,241,116,49,89,81,78,89,90,43,43,67,
                      71,87,80,55,109,246,251,246,218,154,92,255,255,253,254,249,252,242,183,118,68,55,91,168,221,200,90,59,90,83,87,85,67,62,69,61,59,81,84,83,73,179,181,163,134,166,198};
  
  float sum1 = 0;
  float temp = 0;
  float temp2 = 0;
  float sqsum1 = 0;
  float sqsum2 = 0;
  float Fm = 0;
  float Sm = 0;
  int size = 1764;
  
  for (int i = 0; i&lt;size; i++){
      temp += First[i];
  }
  for (int i = 0; i&lt;size; i++){
      temp2 += Second[i];
  }
  
  Fm = temp/size;
  Sm = temp2/size;
  
  cout &lt;&lt; "Mean of First array = "&lt;&lt; Fm &lt;&lt; endl;
  cout &lt;&lt; "Mean of Second array = "&lt;&lt; Sm &lt;&lt; endl;
  
  
  for (int i=0; i&lt;size; i++) {
      sum1 += (First[i] - Fm) * (Second[i] - Sm);
      sqsum1 += pow((First[i] - Fm), 2);
      sqsum2 += pow((Second[i] - Sm), 2);
  }
  cout &lt;&lt; "NCC Second &amp; First : ";
  float result = (sum1/(sqrt(sqsum1*sqsum2)));
  cout &lt;&lt; result &lt;&lt; endl;
  
  return 0;
  }
  </code></pre>