contrastStretch(binCount:destination:)
Stretches the histogram of a 32-bit planar pixel buffer.
Declaration
func contrastStretch(binCount: Int, destination: vImage.PixelBuffer<vImage.PlanarF>)Parameters
- binCount:
The number of histogram entries for each channel.
- destination:
The destination pixel buffer.
Discussion
Use this function to evenly distributes a histogram’s pixel values across the full range of available pixel values.
For example, the following code stretches the contrast of an image:
let srcImage = imageLiteral(resourceName: "Landscape_28_lowContrast.jpg").cgImage(
forProposedRect: nil,
context: nil,
hints: nil)!
var cgImageFormat = vImage_CGImageFormat(
bitsPerComponent: 32,
bitsPerPixel: 32 * 1,
colorSpace: CGColorSpaceCreateDeviceGray(),
bitmapInfo: CGBitmapInfo(rawValue: kCGBitmapByteOrder32Host.rawValue |
CGBitmapInfo.floatComponents.rawValue |
CGImageAlphaInfo.none.rawValue))!
let buffer = try vImage.PixelBuffer(
cgImage: srcImage,
cgImageFormat: &cgImageFormat,
pixelFormat: vImage.PlanarF.self)
buffer.contrastStretch(binCount: 1024,
destination: buffer)
let outputImage = buffer.makeCGImage(cgImageFormat: cgImageFormat)