specifyHistogram(_:destination:)
Performs a histogram specification operation on an 8-bit-per-channel, 4-channel interleaved pixel buffer.
Declaration
func specifyHistogram(_ histogram: vImage.PixelBuffer<Format>.Histogram8888, destination: vImage.PixelBuffer<Format>)Parameters
- histogram:
The histogram.
- destination:
The destination pixel buffer.
Discussion
Histogram specification is a technique that allows you to calculate the histogram of a reference image and apply it to an input image.
The example below shows a source image (bottom left) and a histogram reference image (top left), with the histogram specification output on the right:
[Image]
The code below initializes the source and reference buffers from the CGImage instances sourceImage and referenceImage respectively:
var cgImageFormat = vImage_CGImageFormat(
bitsPerComponent: 8,
bitsPerPixel: 8 * 4,
colorSpace: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipFirst.rawValue))!
let sourceBuffer = try vImage.PixelBuffer(
cgImage: sourceImage,
cgImageFormat: &cgImageFormat,
pixelFormat: vImage.Interleaved8x4.self)
let referenceBuffer = try vImage.PixelBuffer(
cgImage: referenceImage,
cgImageFormat: &cgImageFormat,
pixelFormat: vImage.Interleaved8x4.self)The following code calls histogram() to calculate the histogram of the reference image and passes the histogram to specifyHistogram(_:destination:) to generate the specification result. This function works in place, that is, the input and output can share the same underlying memory.
let histogram = referenceBuffer.histogram()
sourceBuffer.specifyHistogram(histogram,
destination: sourceBuffer)