title: How to get luminosity of Mat image from opencv?
body: <p>I'm doing an image processing on iOS using OpenCV in c++. Sometime the ambient light is not enough and the image cannot be processed properly. I did not found any suitable way to detect the ambient light so trying to detect the luminosity of the image instead.</p>
  
  <p><a href="https://stackoverflow.com/a/4876528/453428">This answer</a> suggests to use the luma value or Y in YUV format. So I follow <a href="https://stackoverflow.com/a/7903042/453428">this answer</a> to access the pixel in formation from Mat image after convert the image to YUV format.</p>
  
  <pre><code>- (void)processImage:(Mat&amp;)image
  {    
      Mat frame_yuv;
      cvtColor( image, frame_yuv, CV_BGR2YUV );
  
      uint8_t* pixelPtr = (uint8_t*)frame_yuv.data;
      int cn = frame_yuv.channels();
      for(int i = 0; i &lt; frame_yuv.rows; i+=50)
      {
          for(int j = 0; j &lt; frame_yuv.cols; j += 50)
          {
              Scalar_&lt;uint8_t&gt; yuvPixel;
              yuvPixel.val[0] = pixelPtr[i*frame_yuv.cols*cn + j*cn + 0]; // Y
              yuvPixel.val[1] = pixelPtr[i*frame_yuv.cols*cn + j*cn + 1]; // U
              yuvPixel.val[2] = pixelPtr[i*frame_yuv.cols*cn + j*cn + 2]; // V
  
              // do something with YUV values...
              std::cout &lt;&lt; "Y: " &lt;&lt; yuvPixel.val[0] &lt;&lt; endl;
              std::cout &lt;&lt; "U: " &lt;&lt; yuvPixel.val[1] &lt;&lt; endl;
              std::cout &lt;&lt; "V: " &lt;&lt; yuvPixel.val[2] &lt;&lt; endl;
          }
      }
  }
  </code></pre>
  
  <p>Here are some lines of log which contains some weird results to me and the values of "Y", "U", or "V" should not be this much in my understanding. Please suggest me what I missed or what did wrong? Thank you very much.</p>
  
  <pre><code>Y: P
  U: \204
  V: \206
  Y: Q
  U: \201
  V: \207
  Y: K
  U: \205
  V: \211
  Y: H
  U: \203
  V: \210
  Y: G
  U: \202
  V: \210
  Y: H
  U: \201
  V: \210
  Y: H
  U: \202
  V: \211
  Y: \326
  U: \200
  V: \204
  Y: \377
  U: \200
  V: \200
  Y: \377
  U: \200
  V: \200
  Y: \377
  U: \200
  V: \200
  Y: \376
  U: |
  V: \201
  Y: \313
  U: x
  V: \210
  Y: \231
  U: ~
  V: \204
  Y: \214
  U: ~
  V: \204
  Y: \205
  U: |
  V: \204
  Y: \221
  U: 
  V: \202
  </code></pre>