ios - 加速 - 降低对比度拉伸(stretch)的阈值

标签 ios swift image-processing accelerate-framework accelerate

我正在使用 Swift 和 Accelerate,并尝试使用 Accelerate 的 vImage 模块中提供的 vImageContrastStretch 方法对图像进行颜色校正。

当我尝试拉伸(stretch)直方图时,我得到的结果确实符合我的要求,但有点太放松了。生成的图像直方图的侧面仍然有不需要的空间。 enter image description here 正如您在图像中看到的,vImageContrastStretch(中间)操作的结果仍然有一些在直方图两侧(标记为红色)进行剪切的空间。我想要的结果在右侧 - 请注意直方图差异。

如何剪切直方图的红色部分?我是否必须编写自己的算法,或者是否有使用 Accelerate 的更简单的解决方案?

我用来生成结果的代码示例:

import UIKit
import PlaygroundSupport
import CoreImage
import Accelerate

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: CGColorRenderingIntent.defaultIntent)

extension CIImage
{
    convenience init?(fromvImageBuffer: vImage_Buffer)
    {
        var mutableBuffer = fromvImageBuffer
        var error = vImage_Error()

        let cgImage = vImageCreateCGImageFromBuffer(
            &mutableBuffer,
            &format,
            nil,
            nil,
            UInt32(kvImageNoFlags),
            &error)

        self.init(cgImage: cgImage!.takeRetainedValue())
    }
}

let inputImage: UIImage = UIImage(named: "IMG_2225.JPG")!

let ciImage = CIImage(cgImage: inputImage.cgImage!)

let imageRef = CIContext().createCGImage(
    ciImage,
    from: ciImage.extent)

var imageBuffer = vImage_Buffer()

vImageBuffer_InitWithCGImage(
    &imageBuffer,
    &format,
    nil,
    imageRef!,
    UInt32(kvImageNoFlags))

let pixelBuffer = malloc(imageRef!.bytesPerRow * imageRef!.height)

var outBuffer = vImage_Buffer(
    data: pixelBuffer,
    height: UInt(imageRef!.height),
    width: UInt(imageRef!.width),
    rowBytes: imageRef!.bytesPerRow)

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

let outImage = CIImage(fromvImageBuffer: outBuffer)
free(imageBuffer.data)
free(pixelBuffer)

outImage!

最佳答案

从描述来看,vImageEndsInContrastStretch_ARGB8888()提供您正在寻找的功能。来自 Histogram overview在文档中:

Ends-in contrast stretch is a more complex version of the contrast stretch operation. These types of functions are best used on images that have some pixels at or near the lowest and highest values of the intensity spectrum, but whose histogram is still mainly concentrated in one area. The ends-in contrast stretch functions map all intensities less than or equal to a certain level to 0; all intensities greater than or equal to a certain level to 255; and perform a contrast stretch on all the values in between. The low and high levels are not defined directly by two given intensity values, but by percentages: the ends-in contrast stretch operation must find intensity levels such that a certain percent of pixels are below one of the intensity values, and a certain percent are above the other intensity value.

关于ios - 加速 - 降低对比度拉伸(stretch)的阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50993741/

相关文章:

ios - Firebase 授权谷歌 + Facebook = 崩溃

ios - 几次滚动后 CollectionView 在 NSISEngine 中崩溃

ios - 由于从 api 解包值而崩溃(使用 Alamofire)

java RGB颜色空间到YCrCb颜色空间转换

ASP.net 和 MS Office Project 集成并将其导出为图像

ios - 列出设备 iOS swift 中的所有音频文件

ios - 钥匙串(keychain)上的分发证书中缺少私钥

ios - 警报完成未完全正常工作

swift - Apple TV 上带有 swift 的 .XZ 文件

java - 在 spring-rest API 中从数据库检索图像路径的理想方法是什么?