【问题标题】:Colour correction of Photoshop LUT filter using CIFilter使用 CIFilter 对 Photoshop LUT 滤镜进行颜色校正
【发布时间】:2015-09-15 14:47:12
【问题描述】:

使用photoshop创建LUT滤镜,使用iOS CIFilter读取LUT图像,iOS创建的滤镜图像与photoshop创建的filer图像不对应。

如何追踪问题?

这是原图

这是我从 Photoshop 创建的带有滤镜的图像

这是我从 iPhone 创建的带有过滤器的图像

这是我正在使用的 LUT 图像

【问题讨论】:

  • 我不使用 CIFilter 但您的图像似乎是 Photoshop 和 iOS 将图像转换为灰度之间的区别(红色、绿色、蓝色的每个通道将显示为各种灰度等级)。此外,您的 PhotoShop 图像采用 PNG(4 个通道),但您的 iOS 图像采用 JPG(3 个通道)。仅保留一种格式(尝试 JPG)以进行 Original、PShop 和 iOS 比较。

标签: ios swift image-processing pixel core-image


【解决方案1】:

请试试这对我有用

public class LUTsHelper {

public static func applyLUTsFilter(lutImage: String, dimension: Int, colorSpace: CGColorSpace) -> CIFilter? {

    guard let image = UIImage(named: lutImage) else {
        return nil
    }

    guard let cgImage = image.cgImage else {
        return nil
    }

    guard let bitmap = createBitmap(image: cgImage, colorSpace: colorSpace) else {
        return nil
    }

    let width = cgImage.width
    let height = cgImage.height
    let rowNum = width / dimension
    let columnNum = height / dimension

    let dataSize = dimension * dimension * dimension * MemoryLayout<Float>.size * 4

    var array = Array<Float>(repeating: 0, count: dataSize)

    var bitmapOffest: Int = 0
    var z: Int = 0

    for _ in stride(from: 0, to: rowNum, by: 1) {
        for y in stride(from: 0, to: dimension, by: 1) {
            let tmp = z
            for _ in stride(from: 0, to: columnNum, by: 1) {
                for x in stride(from: 0, to: dimension, by: 1) {

                    let dataOffset = (z * dimension * dimension + y * dimension + x) * 4

                    let position = bitmap
                        .advanced(by: bitmapOffest)

                    array[dataOffset + 0] = Float(position
                        .advanced(by: 0)
                        .pointee) / 255

                    array[dataOffset + 1] = Float(position
                        .advanced(by: 1)
                        .pointee) / 255

                    array[dataOffset + 2] = Float(position
                        .advanced(by: 2)
                        .pointee) / 255

                    array[dataOffset + 3] = Float(position
                        .advanced(by: 3)
                        .pointee) / 255

                    bitmapOffest += 4

                }
                z += 1
            }
            z = tmp
        }
        z += columnNum
    }

    free(bitmap)

    let data = Data.init(bytes: array, count: dataSize)

    guard
    let cubeFilter = CIFilter(name: "CIColorCubeWithColorSpace")
    else {
        return nil
    }

    cubeFilter.setValue(dimension, forKey: "inputCubeDimension")
    cubeFilter.setValue(data, forKey: "inputCubeData")
    cubeFilter.setValue(colorSpace, forKey: "inputColorSpace")

    return cubeFilter

}

private static func createBitmap(image: CGImage, colorSpace: CGColorSpace) -> UnsafeMutablePointer<UInt8>? {

    let width = image.width
    let height = image.height

    let bitsPerComponent = 8
    let bytesPerRow = width * 4

    let bitmapSize = bytesPerRow * height

    guard let data = malloc(bitmapSize) else {
        return nil
    }

    guard let context = CGContext(
        data: data,
        width: width,
        height: height,
        bitsPerComponent: bitsPerComponent,
        bytesPerRow: bytesPerRow,
        space: colorSpace,
        bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue,
        releaseCallback: nil,
        releaseInfo: nil) else {
            return nil
    }

    context.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height))

    return data.bindMemory(to: UInt8.self, capacity: bitmapSize)
}}

现在我们这个班级

        let colorSpace: CGColorSpace = CGColorSpace.init(name: CGColorSpace.sRGB) ?? CGColorSpaceCreateDeviceRGB()
        let lutFilter = LUTsHelper.applyLUTsFilter(lutImage: "demo.png", dimension: 64, colorSpace: colorSpace)
        lutFilter?.setValue(outputImage, forKey: "inputImage")

        let lutOutputImage = lutFilter?.outputImage

        if let output = lutOutputImage {
            outputImage = output
        }

【讨论】:

    猜你喜欢
    • 2018-06-04
    • 1970-01-01
    • 2012-09-02
    • 2016-12-12
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 2020-09-22
    • 2023-01-27
    相关资源
    最近更新 更多