DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • What Are Protocol Buffers?
  • How to do Memory Management for DataWeave
  • How Milvus Implements Dynamic Data Update and Query
  • Compile Protocol Buffers Using Maven

Trending

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • A Guide to Container Runtimes
  • Solid Testing Strategies for Salesforce Releases
  • Docker Model Runner: Streamlining AI Deployment for Developers
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Histogram Functions in Accelerate vImage

Histogram Functions in Accelerate vImage

How to use Apple's vImage API in Swift to manipulate the histogram of images programmatically.

By 
Simon Gladman user avatar
Simon Gladman
·
May. 10, 16 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.8K Views

Join the DZone community and get the full member experience.

Join For Free

Whilst Core Image has an amazing range of image filters, the iOS SDK Accelerate framework's vImage API includes some impressive histogram functions that CI lacks. These include histogram specification, pictured above, which applies a histogram (e.g. calculated from an image) to an image, and contrast stretch which normalizes the values of a histogram across the full range of intensity values.

vImage's API isn't super friendly to Swift developers, so this blog post demonstrates how to use these functions. These examples originate from a talk I gave at ProgSCon, and you can see the slideshow for the talk here.

Converting Images to vImage Buffers and Vice Versa

Much like Core Image has its own CIImage format, vImage uses its own format for image data: vImage_Buffer. Buffers can easily be created from a Core Graphics CGImage with a few steps. First of all, we need to define a format: this will be 8 bits per channel and four channels per pixel: red, green, blue and, lastly, alpha:

let bitmapInfo:CGBitmapInfo = CGBitmapInfo(
    rawValue: CGImageAlphaInfo.Last.rawValue)

var format = vImage_CGImageFormat(
    bitsPerComponent: 8,
    bitsPerPixel: 32,
    colorSpace: nil,
    bitmapInfo: bitmapInfo,
    version: 0,
    decode: nil,
    renderingIntent: .RenderingIntentDefault)

Given a UIImage, we can pass its CGImage to vImageBuffer_InitWithCGImage(). This method also needs an empty buffer which will be populated with the bitmap data of the image:

let sky = UIImage(named: "sky.jpg")!
var inBuffer = vImage_Buffer()
vImageBuffer_InitWithCGImage(
  &inBuffer,
  &format,
  nil,
  sky.CGImage!,
  UInt32(kvImageNoFlags))

Once we're done filtering, converting a vImage buffer back to a UIImage is just as simple. This code is nice to implement as an extension to UIImage as a convenience initializer. Here we can accept a buffer as an argument, create a mutable copy, and pass it to vImageCreateCGImageFromBuffer to populate a CGImage:

extension UIImage
{
  convenience init?(fromvImageOutBuffer outBuffer:vImage_Buffer)
  {
    var mutableBuffer = outBuffer
    var error = vImage_Error()

    let cgImage = vImageCreateCGImageFromBuffer(
      &mutableBuffer,
      &format,
      nil,
      nil,
      UInt32(kvImageNoFlags),
      &error)
    self.init(CGImage: cgImage.takeRetainedValue())
  }
}

Contrast Stretching

vImage filters generally accept a source image buffer and write the result to a destination buffer. The technique above will create the source buffer, but we'll need to create an empty buffer with the same dimensions as the source as a destination:

let sky = UIImage(named: "sky.jpg")!
let imageRef = sky.CGImage
let pixelBuffer = malloc(CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef))
var outBuffer = vImage_Buffer(
  data: pixelBuffer,
  height: UInt(CGImageGetHeight(imageRef)),
  width: UInt(CGImageGetWidth(imageRef)),
  rowBytes: CGImageGetBytesPerRow(imageRef))

With the inBuffer and outBuffer in place, executing the stretch filter is as simple as:

vImageContrastStretch_ARGB8888(
  &inBuffer,
  &outBuffer,
  UInt32(kvImageNoFlags))

...and we can now use the initializer above to create a UIImage from the outBuffer:

let outImage = UIImage(fromvImageOutBuffer: outBuffer)

Finally, the pixel buffer created to hold the bitmap data for outBuffer needs be freed:

free(pixelBuffer)

Contrast stretching can give some great results. This rather flat image:

Now looks like this:

Conclusion

vImage, although suffering from a rather unfriendly API, is a tremendously powerful framework. Along with a suite of histogram operations, it has functions for deconvolving images (e.g. de-blurring) and some interesting morphology functions (e.g. dilating with a kernel which can be used for lens effects such as starburst and bokeh simulation).

My Image Processing for iOS slide deck explores different filters and the companion Swift project contains demonstration code.

The code for this project is available in the companion project to my image processing talk. I've also wrapped up this vImage code in a Core Image filter wrapper which is available in my Filterpedia app.

It's worth noting that the Metal Performance Shaders framework also includes these histogram operations.

Addendum: Contrast Stretching With Core Image

Many thanks to Simonas Bastys of Pixelmator who pointed out that Core Image does indeed have contrast stretching through its auto-adjustment filters.

With the sky image, Core Image can create a tone curve filter with the necessary values using this code:

let sky = CIImage(image: UIImage(named: "sky.jpg")!)!
let toneCurve = sky
    .autoAdjustmentFiltersWithOptions(nil)
    .filter({ $0.name == "CIToneCurve"})
    .first

The points create a curve that looks like this:

Which, when applied to the sky image:

if let toneCurve = toneCurve
{
    toneCurve.setValue(sky, forKey: kCIInputImageKey)
    let final = toneCurve.outputImage
}

....yields this image:

Thanks again Simonas!

FYI, the histograms for the images look like this (thanks to Pixelmator). vImage stretches the most!

Original image:


vImage contrast stretch


Core Image contrast stretch

Buffer (application)

Published at DZone with permission of Simon Gladman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • What Are Protocol Buffers?
  • How to do Memory Management for DataWeave
  • How Milvus Implements Dynamic Data Update and Query
  • Compile Protocol Buffers Using Maven

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!