Get pixel brigthness from raw image data










0















I am trying to get an average of photo luminance/brightness by using AVFoundation framework. What I am doing is taking pixel buffer (which comes with RAW capture only) from an AVCapturePhoto object, accessing its pixels by making UnsafeMutablePointer and retrieving luminance value at a specific point. The average value I expect to get should be between 528 and 4095 (black and white levels). I am not sure what kind of value do I get from this buffer and if I access its pixels in a right way. I am also not sure if this is the best approach, but the green value that I get from the image itself seems to be a compressed one as it never goes higher than 255.



Below is the code that I am using:



 // The pixel buffer stores an image in main memory.
let pixelBuffer = photo.pixelBuffer!

// Locks the BaseAddress of the PixelBuffer to ensure that the memory is accessible.
CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))

// Buffer width and height
let bufferWidth = CVPixelBufferGetWidth(pixelBuffer) // 4032
let bufferHeight = CVPixelBufferGetHeight(pixelBuffer) // 3024

/*
Returns the base address of the PixelBuffer.
Retrieving the base address for a PixelBuffer requires that the buffer base address be locked via a successful call to CVPixelBufferLockBaseAddress.
*/

let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer)
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer) // 8064

/*
Returns the bits of the given instance, interpreted as having the specified type.
Use this function only to convert the instance passed as x to a layout-compatible type when conversion through other means is not possible.
*/

let pointer = unsafeBitCast(pixelBuffer, to: UnsafeMutablePointer<UInt16>.self) // what data type to use?
//let pointer = baseAddress?.bindMemory(to: UInt16.self, capacity: CVPixelBufferGetDataSize(pixelBuffer))
var lumaSum: Double = 0.0

for y in 0..<bufferHeight
for x in 0..<bufferWidth
let index = y * bufferWidth + x // or y * bytesPerRow + x
let luma = Double(pointer[index])
lumaSum += luma



let average = lumaSum / Double(bufferWidth * bufferHeight)
print(average)

CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))


Here is some information that might be useful:



Pixel format: kCVPixelFormatType_14Bayer_RGGB = 'rgg4', /* Bayer 14-bit Little-Endian, packed in 16-bits, ordered R G R G... alternating with G B G B... */



Buffer data size: 24385600



Contains RGB: true



Bits per component: 8



Bytes per row: 8064



Bits per block: 16










share|improve this question


























    0















    I am trying to get an average of photo luminance/brightness by using AVFoundation framework. What I am doing is taking pixel buffer (which comes with RAW capture only) from an AVCapturePhoto object, accessing its pixels by making UnsafeMutablePointer and retrieving luminance value at a specific point. The average value I expect to get should be between 528 and 4095 (black and white levels). I am not sure what kind of value do I get from this buffer and if I access its pixels in a right way. I am also not sure if this is the best approach, but the green value that I get from the image itself seems to be a compressed one as it never goes higher than 255.



    Below is the code that I am using:



     // The pixel buffer stores an image in main memory.
    let pixelBuffer = photo.pixelBuffer!

    // Locks the BaseAddress of the PixelBuffer to ensure that the memory is accessible.
    CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))

    // Buffer width and height
    let bufferWidth = CVPixelBufferGetWidth(pixelBuffer) // 4032
    let bufferHeight = CVPixelBufferGetHeight(pixelBuffer) // 3024

    /*
    Returns the base address of the PixelBuffer.
    Retrieving the base address for a PixelBuffer requires that the buffer base address be locked via a successful call to CVPixelBufferLockBaseAddress.
    */

    let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer)
    let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer) // 8064

    /*
    Returns the bits of the given instance, interpreted as having the specified type.
    Use this function only to convert the instance passed as x to a layout-compatible type when conversion through other means is not possible.
    */

    let pointer = unsafeBitCast(pixelBuffer, to: UnsafeMutablePointer<UInt16>.self) // what data type to use?
    //let pointer = baseAddress?.bindMemory(to: UInt16.self, capacity: CVPixelBufferGetDataSize(pixelBuffer))
    var lumaSum: Double = 0.0

    for y in 0..<bufferHeight
    for x in 0..<bufferWidth
    let index = y * bufferWidth + x // or y * bytesPerRow + x
    let luma = Double(pointer[index])
    lumaSum += luma



    let average = lumaSum / Double(bufferWidth * bufferHeight)
    print(average)

    CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))


    Here is some information that might be useful:



    Pixel format: kCVPixelFormatType_14Bayer_RGGB = 'rgg4', /* Bayer 14-bit Little-Endian, packed in 16-bits, ordered R G R G... alternating with G B G B... */



    Buffer data size: 24385600



    Contains RGB: true



    Bits per component: 8



    Bytes per row: 8064



    Bits per block: 16










    share|improve this question
























      0












      0








      0


      1






      I am trying to get an average of photo luminance/brightness by using AVFoundation framework. What I am doing is taking pixel buffer (which comes with RAW capture only) from an AVCapturePhoto object, accessing its pixels by making UnsafeMutablePointer and retrieving luminance value at a specific point. The average value I expect to get should be between 528 and 4095 (black and white levels). I am not sure what kind of value do I get from this buffer and if I access its pixels in a right way. I am also not sure if this is the best approach, but the green value that I get from the image itself seems to be a compressed one as it never goes higher than 255.



      Below is the code that I am using:



       // The pixel buffer stores an image in main memory.
      let pixelBuffer = photo.pixelBuffer!

      // Locks the BaseAddress of the PixelBuffer to ensure that the memory is accessible.
      CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))

      // Buffer width and height
      let bufferWidth = CVPixelBufferGetWidth(pixelBuffer) // 4032
      let bufferHeight = CVPixelBufferGetHeight(pixelBuffer) // 3024

      /*
      Returns the base address of the PixelBuffer.
      Retrieving the base address for a PixelBuffer requires that the buffer base address be locked via a successful call to CVPixelBufferLockBaseAddress.
      */

      let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer)
      let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer) // 8064

      /*
      Returns the bits of the given instance, interpreted as having the specified type.
      Use this function only to convert the instance passed as x to a layout-compatible type when conversion through other means is not possible.
      */

      let pointer = unsafeBitCast(pixelBuffer, to: UnsafeMutablePointer<UInt16>.self) // what data type to use?
      //let pointer = baseAddress?.bindMemory(to: UInt16.self, capacity: CVPixelBufferGetDataSize(pixelBuffer))
      var lumaSum: Double = 0.0

      for y in 0..<bufferHeight
      for x in 0..<bufferWidth
      let index = y * bufferWidth + x // or y * bytesPerRow + x
      let luma = Double(pointer[index])
      lumaSum += luma



      let average = lumaSum / Double(bufferWidth * bufferHeight)
      print(average)

      CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))


      Here is some information that might be useful:



      Pixel format: kCVPixelFormatType_14Bayer_RGGB = 'rgg4', /* Bayer 14-bit Little-Endian, packed in 16-bits, ordered R G R G... alternating with G B G B... */



      Buffer data size: 24385600



      Contains RGB: true



      Bits per component: 8



      Bytes per row: 8064



      Bits per block: 16










      share|improve this question














      I am trying to get an average of photo luminance/brightness by using AVFoundation framework. What I am doing is taking pixel buffer (which comes with RAW capture only) from an AVCapturePhoto object, accessing its pixels by making UnsafeMutablePointer and retrieving luminance value at a specific point. The average value I expect to get should be between 528 and 4095 (black and white levels). I am not sure what kind of value do I get from this buffer and if I access its pixels in a right way. I am also not sure if this is the best approach, but the green value that I get from the image itself seems to be a compressed one as it never goes higher than 255.



      Below is the code that I am using:



       // The pixel buffer stores an image in main memory.
      let pixelBuffer = photo.pixelBuffer!

      // Locks the BaseAddress of the PixelBuffer to ensure that the memory is accessible.
      CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))

      // Buffer width and height
      let bufferWidth = CVPixelBufferGetWidth(pixelBuffer) // 4032
      let bufferHeight = CVPixelBufferGetHeight(pixelBuffer) // 3024

      /*
      Returns the base address of the PixelBuffer.
      Retrieving the base address for a PixelBuffer requires that the buffer base address be locked via a successful call to CVPixelBufferLockBaseAddress.
      */

      let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer)
      let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer) // 8064

      /*
      Returns the bits of the given instance, interpreted as having the specified type.
      Use this function only to convert the instance passed as x to a layout-compatible type when conversion through other means is not possible.
      */

      let pointer = unsafeBitCast(pixelBuffer, to: UnsafeMutablePointer<UInt16>.self) // what data type to use?
      //let pointer = baseAddress?.bindMemory(to: UInt16.self, capacity: CVPixelBufferGetDataSize(pixelBuffer))
      var lumaSum: Double = 0.0

      for y in 0..<bufferHeight
      for x in 0..<bufferWidth
      let index = y * bufferWidth + x // or y * bytesPerRow + x
      let luma = Double(pointer[index])
      lumaSum += luma



      let average = lumaSum / Double(bufferWidth * bufferHeight)
      print(average)

      CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))


      Here is some information that might be useful:



      Pixel format: kCVPixelFormatType_14Bayer_RGGB = 'rgg4', /* Bayer 14-bit Little-Endian, packed in 16-bits, ordered R G R G... alternating with G B G B... */



      Buffer data size: 24385600



      Contains RGB: true



      Bits per component: 8



      Bytes per row: 8064



      Bits per block: 16







      swift avfoundation rgb pixel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 10:16









      Aleksei MoskaljukAleksei Moskaljuk

      113




      113






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278686%2fget-pixel-brigthness-from-raw-image-data%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278686%2fget-pixel-brigthness-from-raw-image-data%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          What does pagestruct do in Eviews?

          Dutch intervention in Lombok and Karangasem

          Channel Islands